5

I have a totally custom visualforce page, with a number of elements on it, backed by controller instance variables, and I'm using apex:inputField to successfully render them all perfectly.

I now wish to allow the user to "select" a contact, and then I have an Apex function to do some stuff to the chosen contact.

I have created a Contact instance variable (as per all the others) like so:

public Contact contactLookup { get; set; }

but then, on the VisualForce page, if I try to render the input via:

<apex:inputField value="{!contactLookup}" />

I get an error message:

    Error: Could not resolve the entity from <apex:inputField> value binding '{!contactLookup}'. <apex:inputField> can only be used with SObjects, or objects that are Visualforce field component resolvable.  

Which is fair enough "Contact" is not associated with an input type. So my question is, how do I get the good old "Look up" box to appear on the page?

As seen here, from another standard page, for Account: Account lookup

I'm fully aware of how to do this when using a standard controller/custom field - but not when the lookup is merely backed by an instance variable like this?

1

1 Answer 1

5

You should not reference a Sobject, reference its field instead. Correct way is:

public Account contactLookup { get; set; }

<apex:inputField value="{!contactLookup.contact}" />

<apex:inputField> binds with sObject's field not the whole sObject. If it is a lookup then it will have lookup style.

5
  • This is very interesting, and might lead to what I'm after, but the trouble is I'm purely after a Contact, not necessarily in the context of an Account or any parent object. Commented Sep 13, 2013 at 11:10
  • This done by self lookup on sObject. Contact don't have self lookup like Account. You have to create a self lookup on Contact to achieve this. @SimonLawrence
    – Ashwani
    Commented Sep 13, 2013 at 11:24
  • @responsive Contacts do have a self-lookup. What do you think Contact.ReportsToId is?
    – sfdcfox
    Commented Sep 13, 2013 at 14:19
  • yes, apex:inputfield and standard field will work. <apex:inputField value="{!Contact.ReportsToId}"/>. That has been just slipped for memory... Api 2.5 i think. Thanks!
    – Ashwani
    Commented Sep 13, 2013 at 17:09
  • Using Contact.ReportsToId and then hooking into the selection for my own purposes works nicely (I hope) thank you guys. Commented Sep 16, 2013 at 15:23

You must log in to answer this question.

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