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
anurajanuraj 

Lookup

 

Hi

Tell me how to create a Lookup field in a VF page.

I have an object name contact. I want to create a lookup for it.

Thanks

Anuraj

Best Answer chosen by Admin (Salesforce Developers) 
osamanosaman

Yes you can do that

 

//Controller class

public class myClass

{

   public assigned_consultant__c aCons {get; set;}

}

 

//Visualforce Page

 

<apex:page Controller="myClass">
<apex:form>
  <apex:inputField value="{!aCons .Contact__c}"/>
</apex:form> </apex:page>

All Answers

osamanosaman

You create a look up using

 

<apex:inputField value="{!objectName.LookupField}"/>

 

Thanks

anurajanuraj

Hi

I am new to salesforce. Can u tell me is this the way to write.

My code. I am getting error

 

Error: Unknown property 'ContactStandardController.assigned_consultant__c'

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

 thanks

Anuraj

osamanosaman

Change 

 

<apex:page standardController="Contact">

 

 

to

 

<apex:page standardController="assigned_consultant__c">

 

 

anurajanuraj

Hi

I want to use Controller. Can I do it using Controller.

Thanks that was successful. 

Thanks 

Anuraj

osamanosaman

Yes you can do that

 

//Controller class

public class myClass

{

   public assigned_consultant__c aCons {get; set;}

}

 

//Visualforce Page

 

<apex:page Controller="myClass">
<apex:form>
  <apex:inputField value="{!aCons .Contact__c}"/>
</apex:form> </apex:page>
This was selected as the best answer
anurajanuraj

 

Thank you very much.

anurajanuraj

Hi 

Can u tell me 1 thing more. How to capture the value of the lookup from the code.

I wrote like this.

public with sharing class assigned_consultant 
{

    public String selectItem { get; set; }

    public PageReference savedata() 
    {
        System.debug('++++++++++++++++++++' );
        return null;
    }


    public assigned_consultant__C getAssg() 
    {
        return null;
    }

 Thanks

Anuraj

osamanosaman

Can you further explain your question please?

anurajanuraj

I have an object assigned_consultant__C . That I have created the lookup for the Contact__c. When we select the lookup the value which is selected by the lookup. I want that value for the quarry execution. 

My question is how to get the selected value of the lookup field. In the Controller.

 

osamanosaman

You can use as it is 

 

Seletc Id from Object where Field = : aCons.Contact__c

 

For better results put a null check on that fields before executing the query

anurajanuraj

I have don like this but their is error

Error: assigned_consultant Compile Error: Variable does not exist: assg.Contact__c at line 17 column 114

 

<apex:page Controller="assigned_consultant">
<apex:form >
  Select Contact :
  <apex:inputField value="{!assg.Contact__c}" id="lookup"/><br/><br/>
  Select Month :
  <apex:selectList id="Select" size="1" value="{!selectItem}" >
            <apex:selectOption itemvalue="-Select-"></apex:selectOption>
            <apex:selectOption itemvalue="jan" itemLabel="January"></apex:selectOption>
            <apex:selectOption itemvalue="feb" itemLabel="February"></apex:selectOption>
            <apex:selectOption itemvalue="mar" itemLabel="March"></apex:selectOption>
            <apex:selectOption itemvalue="apr" itemLabel="April"></apex:selectOption>
            <apex:selectOption itemvalue="may" itemLabel="May"></apex:selectOption>
            <apex:selectOption itemvalue="jun" itemLabel="June"></apex:selectOption>
            <apex:selectOption itemvalue="jul" itemLabel="July"></apex:selectOption>
            <apex:selectOption itemvalue="aug" itemLabel="Agust"></apex:selectOption>
            <apex:selectOption itemvalue="sep" itemLabel="September"></apex:selectOption>
            <apex:selectOption itemvalue="oct" itemLabel="October"></apex:selectOption>
            <apex:selectOption itemvalue="nov" itemLabel="November"></apex:selectOption>
            <apex:selectOption itemvalue="dec" itemLabel="December"></apex:selectOption>
  </apex:selectList><br/><br/>
  <apex:commandButton action="{!savedata}" value="Check"/> 
</apex:form>
</apex:page>

 

public with sharing class assigned_consultant 
{
   // public assigned_consultant__C  sample;
   // String contact = null;
    list<string> contact = new list<contact>();
    public String selectItem { get; set; }
    public assigned_consultant__C getAssg()
    {
       // sample = [select End_date__c, Project__c, Start_date__c from assigned_consultant__c where Id = :ApexPages.currentPage().getParameters().get('Assg')];
        return null;
    }
  
    
    public void savedata() 
    {
    
        contact = [select End_date__c, Project__c, Start_date__c from assigned_consultant__c where Contact__c = :assg.Contact__c];
        System.debug('++++++++++++++++++++');
        
    }

 what changes should i make in it.

osamanosaman

public assigned_consultant__C getAssg() should be changed to

 

public assigned_consultant__C assg(get{//query here}; set;)

 

it should work then

anurajanuraj

It is showing the 


Error: assigned_consultant Compile Error: unexpected token: '{' at line 7 column 44

public assigned_consultant__C  assg (get{contact = [select End_date__c, Project__c, Start_date__c from assigned_consultant__c where Contact__c = :assg.Contact__c]}; set;)