I have two Visualforce Pages, one for Account and another for Opportunity. On the Account page there is a button named New Opportunity. I want to pre-populate some data from Account fields to the Opportunity page.

What I have done so far is I have created a button with the following url:


When I click on this button it redirects to the Opportunity page (correct), but does not populate the fields (incorrect).

I have checked the redirecting URL which is


What I found it is not taking account id after redirecting.

3 Answers 3


Based on the resulting url, your {!account.Id} value is null. This indicates the record has not yet been inserted. So the core problem has nothing to do with parameters, but rather order of operations. You should use a commandButton that calls an action something like:

public static final String ID_PARAM = 'Id';
public static final String ACCOUNT_PARAM = 'accid';
public static final String RETURL_PARAM = 'retURL';

public PageReference save()
        insert myAccount;
    catch (DmlException dmx)
        return null;

    PageReference retUrl = Page.AccountView;
    retUrl.getParameters().put(ID_PARAM, myAccount.Id);

    PageReference redirect = Page.OpportunityNew;
    redirect.getParameters().put(ACCOUNT_PARAM, myAccount.Id);
    redirect.getParameters().put(RETURL_PARAM, retUrl.getUrl());
    return redirect;

Note that because your parameter names are all public, you can now reference them from your other controller/extension to make sure you get an exact match. Your markup would look something like:

<apex:pageMessages id="msgs" />
    <apex:commandButton value="Save" action="{!save}" rerender="msgs" />

In order to prepopulate field, you need to identify the html element id of the input element that needs to be prepopulated. So your url should look like this


Just check the html markup for identifying the html element id of the input element.

  • Not working at all , It is not giving blank while rendering accountid
    – Cloud Arch
    Jul 30, 2015 at 11:48
  • could you just try this url /apex/Oppurtunitynew?[html-element-id]={!account.name}&[html-element-id]={!Account.Id}
    – anmrk
    Jul 30, 2015 at 12:01
  • again it is coming as /apex/Oppurtunitynew?[Accountnew:form:pageblock1:section1:Amount]= Again it is not taking account id once it takes account id we are done
    – Cloud Arch
    Jul 30, 2015 at 12:20
  • my opportunity page is also a VF page.
    – Cloud Arch
    Jul 30, 2015 at 13:01
  • in that case you need to use ApexPages.currentPage().getParameters().get('parametername') in the contructor of opportunity page controller for accessing the fields
    – anmrk
    Jul 30, 2015 at 13:08

If it the Opportunity page is a custom VF page, rather create an instance on controller side for Opportunity which you will bind on VF page.

Just send the Account Id and then populate the Opportunity instance on controller constructor based on Account details, that will give all values on load of Opportunity page.

Also this is not ideal to send all values through URL. as it can increase very quickly.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .