0

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:

/apex/Oppurtunitynew?retURL=%2Fapex%2Faccountview%3Fid%3D{!Account.Id}%26sfdc.override%3D1%26core.apexpages.devmode.url%3D1&OIP87654543={!account.name}accid={!Account.Id}&sfdc.override=1

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

https://c.cs5.visual.force.com/apex/Oppurtunitynew?retURL=%2Fapex%2Faccountview%3Fid%3D%26sfdc.override%3D1%26core.apexpages.devmode.url%3D1&accid=&sfdc.override=1

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

3 Answers 3

1

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()
{
    try
    {
        insert myAccount;
    }
    catch (DmlException dmx)
    {
        ApexPages.addMessages(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:form>
    <apex:commandButton value="Save" action="{!save}" rerender="msgs" />
</apex:form>
0

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

/apex/Oppurtunitynew?retURL=%2Fapex%2Faccountview%3Fid%3D{!Account.Id}%26sfdc.override%3D1%26core.apexpages.devmode.url%3D1&[html-element-id]={!account.name}&[html-element-id]={!Account.Id}&sfdc.override=1

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

7
  • Not working at all , It is not giving blank while rendering accountid
    – Cloud Arch
    Commented 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
    Commented 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
    Commented Jul 30, 2015 at 12:20
  • my opportunity page is also a VF page.
    – Cloud Arch
    Commented 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
    Commented Jul 30, 2015 at 13:08
0

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 .