+ Start a Discussion

Populating look up on VF page

Hi Friends,


I have a visual force page which will have a lookup field and needs to be prepopulated with the parents id when ever the page is called , We are able to do it by passind the parents(lookup) id , name as query string with the lookups field id .


But the problem is when we move the code to other org, the field id wont be the same and the lookup is not getting prepopulated.  Is there any better approach for this.?


Just sharing my Approach..any new or easy Approach ideas are requested.


My approach:I created two buttons one with standard controller =Account another with standard controller =Custom object.


1st button-Vf page:


<apex:page standardController="CO__c" tabStyle="CO__c" title="C Edit" extensions="controllerone">
<apex:form >
    <apex:sectionHeader title="C Edit" subtitle="New C"/>
        <apex:pageBlock title="C Edit" mode="Edit">
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}"/>
                <apex:commandButton value="Cancel" action="!cancel}"/>
                <apex:pageBlockSection title="Information" collapsible="false" columns="2">
                <apex:inputField value="{!CO__c.First_Name__c}"/>
                <apex:inputField value="{!CO__c.Account__c}"/>
                <apex:inputField value="!Co__c.Last_Name__c}"/>
                <apex:inputField value="{!CO__c.Email__c}"/>
                <apex:inputField value="{!CO__c.Street__c}"/>
                <apex:inputField value="{!CO__c.SSN__c}"/>
                <apex:inputField value="{!CO__c.Phone__c}"/>

Then created another button:


<apex:page standardController="Account" extensions="controllertwo" action="{!testing}">


 Controllertwo code: which calls first 1st button Vf page:


Public class controllertwo{
   Account account;
   Account acc;
    public myoppcontroller(ApexPages.StandardController controller) {
    acc = (account)controller.getRecord();

    Public PageReference testing(){
    PageReference pageRef = new PageReference('/apex/1stbuttonvfpage');
    pageRef.getParameters().put('AccountId', acc.Id);
    return pageRef;


 Above code works fine and gets URL as 


But the problem is i am not able to get the Account lookup field autopopulated.

Please help Or any ideas??






In controllerone you should put in the field.



CO__c.Account__c = accID;


See it if this can help you



You do the controller.getRecord(); in the controller one and use that variable in the VF page.


If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.