Lisa
Lisa

Reputation: 2139

Displaying values related to a lookup field

I'm very new to working with Visualforce and Salesforce, so please bear with me on this...I'm teaching myself how to build visualforce pages by replicating a form that our sales rep use to document orders received. I'm having a really hard time figuring out how to do some (probably) very basic things, and I'm hoping someone can help point me in the right direction. I've found a lot of instructions on how to do things that are either part of what I want or similar but not what I'm looking for, and it's made me very very confused about what needs to be done to make this particular goal work.

Here's what I've done so far...

I've set up a Lookup field on my page that allows the user to populate who the sales rep is (using the User object). I'm kind of confused as to why I'm presented with a textbox and a search button, and not just presented with a drop-down box of Reps to select from, but I guess that's neither here nor there...

<apex:page standardController="Order__c" extensions="OrderNewExtension">
<apex:actionRegion >
    <apex:inputField value="{!Order__c.User__c}" required="true" id="userID">
        <apex:actionSupport event="onchange" reRender="salesRepDetails" />
    </apex:inputField>
</apex:actionRegion>

<apex:outputPanel id="salesRepDetails" rendered="true">
    <table border="0" cellspacing="0" cellpadding="5" width="75%" style="border: 1px solid magenta;">
        <tr>
            <td colspan="2" width="50%">&nbsp;</td>
            <td><apex:outputLabel value="Phone: " for="repPhone" /></td>
            <td class="data">***Something needs to go here***</td>
        </tr>
        ...
    </table>
</apex:outputPanel>

I'd like to do two things with this -

1) After the user chooses the sales rep is, I want the chosen rep's email and phone number to display. 2) (sorta optional) I'd like this field to be pre-populated with the current user's name and data, that way the only time the rep would have to be looked up is when one of our support staff is entering something in for someone else. Does this make sense?

I'm not sure what else is needed for anyone to help me, but if it's within my power to get that info on here, I will do so.

Upvotes: 1

Views: 15662

Answers (2)

Tanumay Das
Tanumay Das

Reputation: 19

I think you want somthing like a predicting lookup field. I have created something that might help http://tweaksalesforce.blogspot.in/2015/06/simple-lookup-field-with-drop-down-list.html?m=1

Upvotes: -2

mast0r
mast0r

Reputation: 820

Your Controller:

public with sharing class yourController
{
    // Defining objects
    public User selectedUser { get; set; }
    public Order__c order { get; set; }

    // Constructor where the user data will be pre-populated
    public yourController()
    {
        order = new Order__c();
        order.User__c = UserInfo.getUserId();
        selectedUser = [ Select Id, Email, Phone From User Where Id = :UserInfo.getUserId() ];
    }

    // Method for reading selected user data
    public pageReference readUser()
    {
        selectedUser = [ Select Id, Email, Phone From User Where Id = :order.User__c ];
        return null;
    } 
}

Visualforce Page:

<apex:outputPanel id="userDetails">
    <apex:pageBlock>
        <apex:pageBlockSection columns="1">
            <apex:inputField value="{!order.User__c}">
                <apex:actionSupport event="onchange" action="{!readUser}" reRender="userDetails"/>
            </apex:inputField>
            <apex:outputField value="{!selectedUser.email}"/>
            <apex:outputField value="{!selectedUser.phone}"/>
        </apex:pageBlockSection>
    </apex:pageBlock>    
</apex:outputPanel>

Upvotes: 4

Related Questions