[SalesForce] Prepopulate fields on visualforce page from a button on another VF page

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.

Best Answer

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>