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
bathybathy 

How do I pass a lookup field value to a Visualforce page

Hi Guys,

Any idea of how to pass a value to a loookup field on a visual force page. I tried passing it through URL and then pass it to the page using controller's getObject Method. Here is the code

My Url to Pass the Id of contact

apex/Fact_Find_Page_1?retURL=%2Fa0q%2Fo&save_new=1&sfdc.override=1?cid={!Contact.Id}
Here is my controller code:
public Fact_Find__c getFf() {

	     if(ff == null) ff = new Fact_Find__c();
     ff.Contact__c = ApexPages.currentPage().getParameters().get('cid');

	      return ff;

	    }
But this is not prefilling  the contact lookup field on the visual force page.

Can anyone let me know what is wrong in my code?
Thanks so much
 
Best Answer chosen by bathy
Anupam RastogiAnupam Rastogi
Hi Bathy,

I have used newFF to show the new record. Therefore it should be inserted in the Save function.

Importantly, as I do not know the purpose of ff, therefore if it is not needed now (because we have newFF), remove it.

So basically you should modify the class code to the following (assuming that ff is not needed now) - 
public class newFactFindController {

    public Fact_Find__c newFF {get;set;}
    public Life_Style_and_Financial_goals__c newLF {get;set;}    
    
    public newFactFindController(ApexPages.StandardController controller) {
        Contact c = (Contact)controller.getRecord();
        newFF = new Fact_Find__c();
        newFF.Contact__c = c.Id;  
        newLF = new Life_Style_and_Financial_goals__c();
    }
    public PageReference step1() {
        return Page.Fact_Find_Page_1;
    }
    public PageReference step2() {
        return Page.Fact_Find_Page2;
    }
    public PageReference step3() {
        return Page.FactFindPage3;
    }
    public PageReference cancel() {
        PageReference ffPage = new PageReference('/a0q/o');
        ffPage.setRedirect(true);
        return ffPage;
    }
    public PageReference save() {
        insert newFF;
        newLF.Fact_Find__c = newFF.id;
        insert newLF;
        PageReference ffPage = new ApexPages.StandardController(newFF).view();
        ffPage.setRedirect(true);
        return ffPage;
    }
}

This code creates two new records when the VF Page is loaded - one for Fact_Find__c object and other for Life_Style_and_Financial_goals__c object.

When you click the Save button, it should save both these records.

Thanks
AR

If this solves your problem then please mark it as best answer.
 

All Answers

Anupam RastogiAnupam Rastogi
Hi,

Did the issue resolve? Or is it that the value is not getting passed from the URL itself?

Thanks
AR
bathybathy
Hi Anupam,

The issue isn't resolved. I have custmised the new button of  the custom Object to include the URL with Contact's Id  Value. When this new button is clicked from a contact page the URL is getting the Contact Id but it is not being passed to the Visualforce page. Not sure what is wrong with my code.
Anupam RastogiAnupam Rastogi
Did you try the line of code that I shared with you?

Share the VF Code that is sending the contact Id.
bathybathy
yes I tried.

This is my URL /apex/Fact_Find_Page_1?retURL=%2Fa0q%2Fo&save_new=1&sfdc.override=1?cid={!Contact.Id}
and this is my Extension code. Sorry I wasn't clear. i am using standard controller and this is my xtension class
public Fact_Find__c getFf() {


	         if(ff == null) ff = new Fact_Find__c();

	     ff.Contact__c = ApexPages.currentPage().getParameters().get('cid');

	          return ff;

	        }

 
Anupam RastogiAnupam Rastogi
I am confused about how you are making a call, what is the flow.

Because as I know when you try to customize an existing button like New you just have option to choose a VF Page. There is NO option to mention a URL over there.
 
bathybathy
Anupam,

I created a custom button with URL mnetioned above which calls my visualforce page and also sends contact id with the variable 'cid' and placed this custom button in the related list of Contact page as this Fact Find is the child object for a contact. Am I doing it in  a wrong way? If yes, Can you please suggest me of how to proceed with this? I am new to Visual force pages and controllers

Thanks,
Bathy.
Anupam RastogiAnupam Rastogi
Hi Bathy,

For ease, find below the configurations I have done as a test which are working. 

1. Here I have a custom button on a Custom Object (Service Request) which is child of Contact.
2. On click of this button a URL is invoked that directs to a VF Page based on the Standard Controller 'Contact' having an extension class. This extension class can be used for any further logic implementation. And consider this class similar to what you must have built (in which you are having the 'public Fact_Find__c getFf()' function).
3. I am able to pass the contact Id to the VF Page and inturn to the class. You can check the Logs that the id is coming.

Custom Button Configuration - 
URL Value = /apex/DisplayUserInfo?retURL={!Contact.Id}& (/apex/DisplayUserInfo?retURL=001U000001RpCTX&id={!Contact.Id})cId (/apex/DisplayUserInfo?retURL=001U000001RpCTX&id={!Contact.Id})={!Contact.Id} (/apex/DisplayUserInfo?retURL=001U000001RpCTX&id={!Contact.Id})

VF Page Sample - 
<apex:page standardController="Contact" extensions="testclass" title="DisplayUserInfo">
    <apex:outputText value="{! cId }" />
    <apex:form>
    	<apex:commandButton action="{! Cancel }" value="Cancel" />
    </apex:form>
</apex:page>

Controller Extension Class Sample - 
public class testclass {
    
    //--- Constructor of the Class
    public testclass(ApexPages.StandardController controller) {
         
        //--- This outputs the Contact Id into the logs.
        System.debug('Contact Id = ' + ApexPages.currentPage().getParameters().get('cId'));
    }
}

Thanks
AR

If this reply solves your problem then please mark it as best answer.
Anupam RastogiAnupam Rastogi
Some issue with the URL Value above. Use the below mentioned - 

URL Value = /apex/DisplayUserInfo?retURL={!Contact.Id}&cId={!Contact.Id}
bathybathy
HI Anupam,

I changed the standard controller to Contact and added the line ff.Contact__c = ApexPages.currentPage().getParameters().get('cid'); in the  public Fact_Find__c getFf() Function. It is still not showing up in the contact Lookup field. It is just empty. Not sure where I went wrong. I am pasting my extension class here for your reference. I tried to put this line of code in the public newFactFindController(ApexPages.StandardController controller) Function but it is giving the error System.NullPointerException: Attempt to de-reference a null object. SO I placed in getFf Function.
.
public class newFactFindController {
 
    public newFactFindController(ApexPages.StandardController controller) {
   
    }


   // These member variables maintain the state of the wizard.
   // When users enter data into the wizard, their input is stored
   // in these variables. 
  Fact_Find__c ff;
  
  Life_Style_and_Financial_goals__c lf;
  

   // The next four methods return one of each of the four member
   // variables. If this is the first time the method is called,
   // it creates an empty record for the variable.
  

  

     public Fact_Find__c getFf() {
     if(ff == null) ff = new Fact_Find__c();
     
     ff.Contact__c = ApexPages.currentPage().getParameters().get('cid');
    
      return ff;
    }

    public Life_Style_and_Financial_goals__c getLf() {
      if(lf== null) lf = new Life_Style_and_Financial_goals__c();
      
       return lf;
    }


   // The next three methods control navigation through
   // the wizard. Each returns a PageReference for one of the three pages
   // in the wizard. Note that the redirect attribute does not need to
   // be set on the PageReference because the URL does not need to change
   // when users move from page to page.
    public PageReference step1() {
      return Page.Fact_Find_Page_1;
     }

      public PageReference step2() {
       return Page.Fact_Find_Page2;
    }

    public PageReference step3() {
       return Page.FactFindPage3;
    }


   
       public PageReference cancel() {
            PageReference ffPage = new PageReference('/a0q/o');
            ffPage.setRedirect(true);
           return ffPage; 
    }

   
   public PageReference save() {

      insert ff;

       lf.Fact_Find__c = ff.id;
        insert lf;

      


       PageReference ffPage = new ApexPages.StandardController(ff).view();
         ffPage.setRedirect(true);

     return ffPage;
     }

}

This is my Fact Find Visual Force page code. I am just pasting the two lines important
<apex:page  standardcontroller="Contact" extensions="newFactFindController"  title="New Fact Find">

<apex:form  title="New Fact Find"> 
  <script>
  function confirmCancel() {
      var isCancel = confirm("Are you sure you wish to cancel?");
      if (isCancel) return true;
  
     return false;
  }  
  </script>
  
  <apex:pageblock id="FF" mode="edit" title="New Fact Find">
    <apex:pageBlockButtons >
          <apex:commandButton action="{!step2}" value="Next"/>
          <apex:commandButton action="{!cancel}" value="Cancel" 
                              onclick="return confirmCancel()" immediate="true"/>
        </apex:pageBlockButtons><p/>
   <apex:pageBlockSection title="Client Details">
   <apex:inputfield value="{!ff.Name}"/>
   <apex:inputField value="{!ff.Contact__c}"/>
Here is the resullt an empty lookup field
User-added image

Thanks,
 
Anupam RastogiAnupam Rastogi
Hi Bathy,

The controller code needs some modification. I have added the lines of code. You can use it to test.

The VF Page should remain on Contact itself because it is the parent. The URL behind the custom button should continue to send the Contact Id as a parameter.
The Controller class needs to be modified - 
 
public class newFactFindController {
 

    //---These member variables maintain the state of the wizard. When users enter data into the wizard, their input is stored in these variables. 
    Fact_Find__c ff; 
    Life_Style_and_Financial_goals__c lf;

    //--- Added the following lines
    public Fact_Find__c newFF {get;set;}
    public newFactFindController(ApexPages.StandardController controller) {
        Contact c = (Contact)controller.getRecord();
        newFF = new Fact_Find__c();
        newFF.Contact__c = c.Id;   
    }

   // The next four methods return one of each of the four member
   // variables. If this is the first time the method is called,
   // it creates an empty record for the variable.
     public Fact_Find__c getFf() {
     if(ff == null) ff = new Fact_Find__c();
     ff.Contact__c = ApexPages.currentPage().getParameters().get('cid');
    
      return ff;
    }

    public Life_Style_and_Financial_goals__c getLf() {
      if(lf== null) lf = new Life_Style_and_Financial_goals__c();
      
       return lf;
    }


   // The next three methods control navigation through
   // the wizard. Each returns a PageReference for one of the three pages
   // in the wizard. Note that the redirect attribute does not need to
   // be set on the PageReference because the URL does not need to change
   // when users move from page to page.
    public PageReference step1() {
      return Page.Fact_Find_Page_1;
     }

      public PageReference step2() {
       return Page.Fact_Find_Page2;
    }

    public PageReference step3() {
       return Page.FactFindPage3;
    }
    public PageReference cancel() {
            PageReference ffPage = new PageReference('/a0q/o');
            ffPage.setRedirect(true);
           return ffPage; 
    }
   public PageReference save() {
      insert ff;
       lf.Fact_Find__c = ff.id;
        insert lf;
       PageReference ffPage = new ApexPages.StandardController(ff).view();
         ffPage.setRedirect(true);

     return ffPage;
     }

}

Thanks
AR

If this reply solves your problem then please mark it as best answer.
bathybathy
I copied your code Anupam but still not working :(
Anupam RastogiAnupam Rastogi
Just paste the following codes in the class and VF Page for testing purpose.

Controller Class
public class newFactFindController {
 
    //--- Added the following lines
    public Fact_Find__c newFF {get;set;}

    public newFactFindController(ApexPages.StandardController controller) {
        Contact c = (Contact)controller.getRecord();
        newFF = new Fact_Find__c();
        newFF.Contact__c = c.Id;   
    }
}

VF Page
<apex:page standardController="Contact" extensions="testclass" title="DisplayUserInfo">
    <apex:form>
        <apex:pageBlock >
            <apex:pageBlockTable value="{! newFF }" var="n" >
                <apex:column headerValue="Name" >
                    <apex:inputField value="{! n.Name}"/>     
                </apex:column>
                <apex:column headerValue="Contact" >
                    <apex:inputField value="{! n.Contact__c}" />
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlock>     
    </apex:form>
</apex:page>

Thanks
AR
 
bathybathy
hi Anupam,

I just copied the code. here is the result User-added image
Anupam RastogiAnupam Rastogi
You also need to modify the URL. Try first with a simple one like

apex/Fact_Find_Page_1?id={!Contact.Id}

 
bathybathy
Anupam,

It just worked. it is displaying the contact name in the lookup field
User-added image
Anupam RastogiAnupam Rastogi
Great... 

So now the issue should be resolved. In your original VF Page within the <apex:pageBlockSection> component, you can use the newFF function to get the values into the inputfield components.

<apex:inputfield value="{!newFF.Name}"/>

Something similar to the following - 
<apex:page  standardcontroller="Contact" extensions="newFactFindController"  title="New Fact Find">

<apex:form  title="New Fact Find"> 
  <script>
  function confirmCancel() {
      var isCancel = confirm("Are you sure you wish to cancel?");
      if (isCancel) return true;
  
     return false;
  }  
  </script>
  
  <apex:pageblock id="FF" mode="edit" title="New Fact Find">
    <apex:pageBlockButtons >
          <apex:commandButton action="{!step2}" value="Next"/>
          <apex:commandButton action="{!cancel}" value="Cancel" 
                              onclick="return confirmCancel()" immediate="true"/>
        </apex:pageBlockButtons><p/>
   <apex:pageBlockSection title="Client Details">
   <apex:inputfield value="{!newFF.Name}"/>
   <apex:inputField value="{!newFF.Contact__c}"/>

Thanks
AR

If this solves your problem then please mark it as best answer.
bathybathy
Anupam,

that worked on the visualforce page but when i create the record and save it didnot have the contact in the detail page.User-added image
Anupam RastogiAnupam Rastogi
Share the entire class code
bathybathy
public class newFactFindController {

      

     

        //---These member variables maintain the state of the wizard. When users enter data into the wizard, their input is stored in these variables.

        Fact_Find__c ff;

        Life_Style_and_Financial_goals__c lf;

     

        //--- Added the following lines

       public Fact_Find__c newFF {get;set;}

     

        public newFactFindController(ApexPages.StandardController controller) {

            Contact c = (Contact)controller.getRecord();

            newFF = new Fact_Find__c();
        newFF.Contact__c = c.Id;  

        }

     

       // The next four methods return one of each of the four member

       // variables. If this is the first time the method is called,

       // it creates an empty record for the variable.

         public Fact_Find__c getFf() {

         if(ff == null) ff = new Fact_Find__c();

         

         

          return ff;

        }

     

        public Life_Style_and_Financial_goals__c getLf() {

          if(lf== null) lf = new Life_Style_and_Financial_goals__c();

           

           return lf;

        }
   

       // The next three methods control navigation through

       // the wizard. Each returns a PageReference for one of the three pages

       // in the wizard. Note that the redirect attribute does not need to

       // be set on the PageReference because the URL does not need to change

       // when users move from page to page.

        public PageReference step1() {

          return Page.Fact_Find_Page_1;

         }

     

          public PageReference step2() {

           return Page.Fact_Find_Page2;

        }

     

        public PageReference step3() {

           return Page.FactFindPage3;

        }

        public PageReference cancel() {

                PageReference ffPage = new PageReference('/a0q/o');

                ffPage.setRedirect(true);

               return ffPage;

        }

       public PageReference save() {

          insert ff;

           lf.Fact_Find__c = ff.id;

            insert lf;

           PageReference ffPage = new ApexPages.StandardController(ff).view();

             ffPage.setRedirect(true);

     

         return ffPage;

         }

     

    }

                                                

 	
Download
Should we add the code in save function to save ff.contact__c = id?
 
Anupam RastogiAnupam Rastogi
Hi Bathy,

I have used newFF to show the new record. Therefore it should be inserted in the Save function.

Importantly, as I do not know the purpose of ff, therefore if it is not needed now (because we have newFF), remove it.

So basically you should modify the class code to the following (assuming that ff is not needed now) - 
public class newFactFindController {

    public Fact_Find__c newFF {get;set;}
    public Life_Style_and_Financial_goals__c newLF {get;set;}    
    
    public newFactFindController(ApexPages.StandardController controller) {
        Contact c = (Contact)controller.getRecord();
        newFF = new Fact_Find__c();
        newFF.Contact__c = c.Id;  
        newLF = new Life_Style_and_Financial_goals__c();
    }
    public PageReference step1() {
        return Page.Fact_Find_Page_1;
    }
    public PageReference step2() {
        return Page.Fact_Find_Page2;
    }
    public PageReference step3() {
        return Page.FactFindPage3;
    }
    public PageReference cancel() {
        PageReference ffPage = new PageReference('/a0q/o');
        ffPage.setRedirect(true);
        return ffPage;
    }
    public PageReference save() {
        insert newFF;
        newLF.Fact_Find__c = newFF.id;
        insert newLF;
        PageReference ffPage = new ApexPages.StandardController(newFF).view();
        ffPage.setRedirect(true);
        return ffPage;
    }
}

This code creates two new records when the VF Page is loaded - one for Fact_Find__c object and other for Life_Style_and_Financial_goals__c object.

When you click the Save button, it should save both these records.

Thanks
AR

If this solves your problem then please mark it as best answer.
 
This was selected as the best answer
bathybathy
Hi Anupam,

I have used ff and lf in all my Visualforce pages for the input fields. If I remove them I have to change all my pages. Can I replace newFF with ff and newLF with lf ?

And when I save the above class I got an error message Error: Compile Error: The method Life_Style_and_Financial_goals__c getLf() is referenced by Visualforce Page (FactFindPage3) in salesforce.com. Remove the usage and try again. at line 52 column 50

FactFindPage3 is the last page in my wizard and it uses the Save() Function.
bathybathy
Hi Anupam,

I used ff and lf in my visualforce pages in input fields as ff.Name and lf.Name etc. Should I replace ff and lf with newFF and newLF in all the input fields in my VF pages?  I cannot remove them now. can you suggest how should I proceed with this?
 
Anupam RastogiAnupam Rastogi
Yes, you can replace newFF with ff and newLF with lf.

And for the error you can put back the required method.
bathybathy
hi Anupam,

I just replaced them and recreated the pages. No issues now contact is prefilled and Fact find is saved with contact. Thank you so much for being patient with me and helping me out.

Regards,
Bathy
Anupam RastogiAnupam Rastogi
Great...