Following to my previous question - Merge Accounts based on Account Number.

I have written a Visualforce page with a selectable table list of Accounts. When selecting the accounts and pressing the action Button I've added - it's calling a PageReference Method that direct the user to the standard Merge Account Page link and will transfer the Id's of the selected accounts as parameters.

This is nice since I extended the standard Merge page Criterias with my Queries..and I don't need to find duplicates based only on the Account Name.

So Basically I want to populate the following link with selected account Id's.


Problem is that when I'm building the URL String and hit the button - the URL get messed up.

For Example :

public PageReference mergeAccounts ()     {

    String mergeURL = '/merge/accmergewizard.jsp?';
    String pageURL ='goNext=+Next+';
    String accIdvar = '&' + cid + '=';
    String URLAccount1 = accIdvar + selectedAccounts[0].Id;
    String URLAccount2 = accIdvar + selectedAccounts[1].Id;

    String URL = mergeURL + pageURL + URLAccount1 + URLAccount2;

    PageReference pageRef = new PageReference(URL.escapeHtml4());
    return pageRef;


The result URL when using this method is :


So I need to encode it somehow and escape the '&' and the '=' - those Special chars thats seems to be the ones that creates the problem - the link is backward and has been cut off.

Suggestions anyone? escape the '&' without messing up the URL?


2 Answers 2


Don't escape the URL that you pass into the PageReference constructor. This will result in Bad Things happening. Also, you shouldn't use escapeHtml* methods, because that's for a different purpose (encoding HTML so it doesn't render as HTML when viewed as HTML). Instead, you would use EncodingUtil.urlEncode if you needed to escape parameters, which you don't, because you're using normal ID values.

However, all of the information I've just provided is useless in this case, because PageReference is going to mangle your URL anyways-- you can't have duplicate parameters in a PageReference query string. You'll need to invent a different way of redirecting the user without using a PageReference, such as by depositing the URL into a plain string that's later injected into a script that performs the redirect:

    window.top.location.href = '{!newLocation}';

I have figured out the way to do that.. Problem with reDirecting to the URL from PageReference method on Server side is that it's generating it with Ascii codes such you can see in the example link - 'amp%3B' - and its messing up the parameters and therefore page cannot be shown.

enCoding/Decoding the URL String didn't work..even Tried to manipulate the string in many ways to make it work - but no luck..

Solution is to send the URL from server side (controller) to Client side (Visualforce with JavaScript) and handle redirect from Visualforce Page.

So I have build the URL in the controller, then sent it as massage to JS function on page that know to reDirect the user to the new URL location if records were selected - WORKS GREAT!!! - credit for guidelines goes to : http://blog.jeffdouglas.com/2011/01/17/passing-values-to-javascript-from-apex-controller-method/

key line on visualforce script is :

<apex:actionFunction name="SendValuesToController" action="{!processRedirectURL}" rerender="jspanel"/>
   <apex:pageBlockButtons >
        <apex:commandButton onclick="SendValuesToController();" oncomplete="redirectToControllerReturnMSG()" value="Merge Accounts"/>

so this calls an Apex method (processRedirectURL) that knows how to build the URL according to records selected - if URL build was successful it will build a message with it according to number of selected records - oncomplete a different JS function (redirectToControllerReturnMSG) inside an outputPanel (id="jspanel") which will use the message as follows:

window.top.location.href ='{!message}'; }

Until Salesforce will give the option to customize the merging page - This is a great workAround to achieve a tool that knows to merge Accounts :

  1. Based on Criteria's other than Name field.
  2. Merge more than 3 Accounts at the same time
  3. Displaying Similar accounts in Custom table allows you to modify the columns as you wish - and display the address fields as well (make it really easier to identify which accounts need to be merged.

Vote here for a Salesforce standard merging tool : https://success.salesforce.com/ideaView?id=08730000000Dno4AAC

  • @Yuval.I am having the same issue and I used your concept.that still dint work.How to achieve? Aug 19, 2016 at 16:11
  • Bit old but Happy to help If you will show some of your code - trick is to mange the redirect from client side with JS and not apex page reference.
    – sfdx bomb
    Mar 28, 2018 at 7:14

You must log in to answer this question.

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