1

I am creating a VF form with account information input fields as well as other custom fields. I would like to create a lookup field for the user to select the account to work in. I am able to create a drop-down list of accounts, but the preference is a lookup field rather than the account list. Is this possible, if so can someone give me an example. Any help would be much appreciated.

1 Answer 1

1

You can use a non-persisted instance of an object that has a lookup field to an Account to provide the field:

public with sharing class YourController {
    public Contact c {
        get {
            if (c == null) c = new Contact();
            return c;
        }
        private set;
    }
}

and then Visualforce will render a lookup field:

<apex:pageBlockSectionItem>
    <apex:outputLabel value="Whatever Label You Want to Use"/>
    <apex:inputField value="{!c.AccountId}"/>
</apex:pageBlockSectionItem>

and in later logic in the controller you can then use the c.AccountId value that is selected.

2
  • 2
    @swifttest1 -- N.B. this is a powerful approach that can be leveraged into other use cases, for example, to provide the SFDC date picker to non-Sobject field inputs
    – cropredy
    Apr 12, 2016 at 1:53
  • Thanks for the help, I ended up using a similar approach as Keith. I created an account in the controller strictly for lookup, and used the ParentID of that account to query and pull in the account the end user is working in.
    – swiftest1
    Apr 21, 2016 at 18:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .