function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
boihueboihue 

Lookup button in Visualforce Page

Hi,

I would like to ask if any expert know how to develop the Lookup button in the Visualforce Page. Thanks!

Best regards

 

Best Answer chosen by Admin (Salesforce Developers) 
dev_forcedev_force

Check out "apex:inputField" which you can use to map to a lookup field on the underlying object.

 

 

For example (not tested):

<apex:page standardController="Contact"> <apex:inputField value="{!contact.account}"/> </apex:page>

 

All Answers

dev_forcedev_force

Check out "apex:inputField" which you can use to map to a lookup field on the underlying object.

 

 

For example (not tested):

<apex:page standardController="Contact"> <apex:inputField value="{!contact.account}"/> </apex:page>

 

This was selected as the best answer
boihueboihue

I don't think that the inputField will let me having the Lookup button beside this Input location, I didn't see any attribute allowing setup this option.

Thanks!

SteveAnderson41SteveAnderson41

Lookup buttons should display on any lookup field that is included as an inputField.  The example above was close, but was missing a couple of things.  Try this instead:

 

<apex:page standardController="Contact">
  <apex:form>
    <apex:inputField value="{!contact.AccountID}"/>
  </apex:form>
</apex:page>

DrawloopSupportDrawloopSupport

I'm having a problem getting my lookup icon to work. I'm using a custom controller.

 

visualforce

 

<apex:pageBlockSectionItem >
<apex:outputLabel for="test" value="Test" />
<apex:inputField id="test" value="{!t.id}" />
</apex:pageBlockSectionItem>

 

apex

public Contact sample;
public Contact getT() {
if (sample == null && ApexPages.currentPage().getParameters().get('t') != null)
sample = [SELECT Name, Id FROM Contact WHERE Id = :ApexPages.currentPage().getParameters().get('t')];
return sample;
}
public void setT(Contact value) {
sample = value;
}

 

If I use apex:inputText instead of inputField, it looks editable, otherwise, it is just uneditable text. In both cases (inputField / inputText), there is no lookup icon. Any ideas?

 

 

Message Edited by DrawloopSupport on 04-14-2009 03:59 PM
kprkpr

Hi, were you able to find a solution to this issue? I tried the samples provided in this thread and didn't work.

 

Please let me know if you found a workaround for this.

 

Thanks,

 

SteveAnderson41SteveAnderson41

<apex:pageBlockSectionItem > <apex:outputLabel for="test" value="Test" /> <apex:inputField id="test" value="{!t.id}" /> </apex:pageBlockSectionItem>

 

Isn't valid.  The inputField must be in an apex:form element.
kprkpr
Even if it is in the <apex:form > tag, it doesn't work.
SteveAnderson41SteveAnderson41

It's hard to know what's not working on your page without more details.  Let me show you an example custom controller and a working page.  Put those in your org and see if the lookup works.  If it does, use these examples to make your own content work.

 

First, the controller

 

public class contactController {

// Return a list of the ten most recently modified contacts
public List<Contact> getContacts() {
return [SELECT Id, Name, Account.Name, Phone, Email
FROM Contact
ORDER BY LastModifiedDate DESC LIMIT 10];
}

// Get the 'id' query parameter from the URL of the page.
// If it's not specified, return an empty contact.
// Otherwise, issue a SOQL query to return the contact from the
// database.
public Contact getContact() {
Id id = System.currentPageReference().getParameters().get('id');
return id == null ? new Contact() : [SELECT Id, Name
FROM Contact
WHERE Id = :id];
}

}

 

Now the page

 

 

<apex:page controller="contactController">
<apex:form >
<apex:pageBlock title="My Content">
<apex:pageBlockSection title="My Content Section" columns="2">
<apex:pageBlockSectionItem>
<apex:outputLabel value="Lookup to an Account" for="theLookup"/>
<apex:inputField id="theLookup" value="{!contact.AccountId}"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

While the controller will take a param, you don't need to specify it.  Just visit the page and you should see a single field with a lookup on it.

 

 

 

 

 

kprkpr

What I wanted is the functionality DrawloopSupport was trying to achieve--querying a field in the Controller and displaying the field as a lookup in the visualforce page.

 

What you did is to generate a lookup field using the standard relationship, which can be simply done using the Contact standardController, as you pointed out earlier.

What I want to do (and what the guys in this thread have said is possible), is to be able to generate a lookup field to a custom field (Say there is a custom field 'Contact_type2' on Account, I want to generate a lookup field by which I could lookup for this field on Account through Contact)

 

Here is my visualforce page:

 

<apex:page standardController="SFL5_Projects__c" extensions="lookupFieldTest" > <apex:form > <apex:pageBlock > <apex:pageBlockSection > <apex:inputField value="{!testvalue.Project_Manager__c}"/> <apex:inputField value="{!accountfieldlkp.BillingState}"/> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>

 Here is the extension I am using:

 

public class lookupFieldTest { public lookupFieldTest(ApexPages.StandardController controller) { } Account localAcc; public SFL5_Projects__c getTestValue(){ return [select id,Project_Manager__c from SFL5_Projects__c limit 1]; } public Account getAccountfieldlkp(){ localAcc = [select BillingState from Account limit 1]; return localAcc; } public void setAccountfieldlkp(Account acc){ localAcc = acc; } }

 

 

 

 Thanks

 

UmapenUmapen

Example is for Account lookup not for contact lookup.

I tried changing the object  to {!account.ContactId} instead of {!contact.AccountId}

 

I get error the  inpufield can only be used with SObject fields.

 

any suggestions on how I can have contact lookup instead of account lookup in the above example

DrawloopSupportDrawloopSupport

Umapen,

Does {!Contact.Id} work? 

UmapenUmapen
I tried using {!contact.Id}. I do not see  input text box or lookup button.
DrawloopSupportDrawloopSupport

I think I've figured out this issue. In order to create a lookup icon in a visualforce page, you will need to reference an actual lookup field. Therefore, !Contact.Id will not work since it is not actually a lookup field. If you were to create a reference to (for example) an OpportunityContactRole record (this record need not exist nor be inserted), you could use !OpportunityContactRole.ContactId. (I have not tested this specific example, but the idea is still the same.) Other examples could be !Case.ContactId, !Contract.ContactId, or any other object that has a lookup field for the contact object. Again, the record need not exist in Salesforce, but the field does need to be a real lookup field.

 

Hope this helps everyone. 

Message Edited by DrawloopSupport on 01-22-2010 03:42 PM
jsessford_rcijsessford_rci

This is an old thread, but here's one solution that hasn't been addressed.

 

We were seeing a similar issue: If a value in the lookup field existed, the inputField component was rendered as read-only, but if the value was null, an editable field would be rendered.

 

Turns out that this was because the relationship defined for the lookup was a master-detail type.  Changing it to a Lookup solved the issue.

happytummyhappytummy

I'm trying to display a lookup field from a master detail relationship between two different custom objects (Recipe & Rating). Basically, the user should be able to look up the Recipe Name from a lookup field on a form and assign a rating.

 

Everything shows up fine on Salesforce but when I try and implement it on the public webpage, it doesn't show up as a lookup field?

 

I'd appreciate any answer. Thank you!

DrawloopSupportDrawloopSupport

happytummy,

 

I would guess that the public/guest user profile for that site does not have read access to Recipies (or whatever rights are necessary).

 

Hope this helps.

happytummyhappytummy

Thanks for your reply Drawloop.

 

But I checked that already. Everything is read/write and visible under security controls.

 

I'm actually having an issue right now were none of the textfields for input aren't showing up; just the labels.

 

Have you ever heard of this issue before?

 

Thanks again!

 

DrawloopSupportDrawloopSupport

Since it works for you as a sys admin, the only thing that makes sense is that it is a permissions issue. Are the objects "Deployed" or still "In Development"? There could be some other permission getting in the way. You could always open a case with Salesforce if you still can't figure it out.

Anand PatelAnand Patel

How do you modify getContact and getContacts if you wantto user for {!Case.ContactId}

BerginBergin

I have two custom objects Student & Award. Award can have one or more students.

 

I have a Student_Award_Relationship object that has Student__c and Award__c as fields which is Master Detail relationship to Student & Award objects respectively.

 

I'm trying to create a custom lookup page and I want to add lookup field to look for student to bind the student to an Award.

I'm not able to get the lookup field for Student. please help.