+ Start a Discussion
verynewbieverynewbie 

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:pageBlockButtons>
                <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}"/>
            </apex:pageBlockSection>  
        </apex:pageBlock>
        </apex:form>
</apex:page>

Then created another button:

 

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

</apex:page>

 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 

https://c.na14.visual.force.com/apex/1stpagevfbutton?AccountId=001d000002Gsjvx

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

Please help Or any ideas??

 

 

liron169liron169

Hello,

 

In controllerone you should put in the field.

 

accID=ApexPages.currentPage().getParameters().get('AccountId');

CO__c.Account__c = accID;

souvik9086souvik9086

See it if this can help you

http://boards.developerforce.com/t5/Apex-Code-Development/How-to-populate-default-look-up-value/m-p/639863#M118371

 

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.

Thanks