the url that I'm putting into the pagereference constructor isn't the one that's popping out. it's stripping off the last &retURL that's part of my &save_new_URL.

what's going on and how do i fix it?


String url = '/setup/ui/recordtypeselect.jsp?ent=Task';
url+= '&retURL=/' + this.who_Id; 
url+='&save_new_URL=/apex/taskOverRide?who_id=' + this.who_Id + '&retURL=/' +this.who_id ;
system.debug('URL: ' + url);
PageReference p = new Pagereference(url);
system.debug('URL: ' + p.getUrl());
return p;

string URL output:

URL: /setup/ui/recordtypeselect.jsp?ent=Task&retURL=/003e0000004owon&save_new_URL=/apex/taskOverRide?who_id=003e0000004owon&retURL=/003e0000004owon

output from the constructor


1 Answer 1


You are calling the parameter retUrl twice, and I think pageReference notices that and only uses one of the retUrls.

When you add the parameters to the URL, certain characters are encoded. Check out this resource: http://en.wikipedia.org/wiki/Percent-encoding

Try encoding the second retUrl yourself, and then it shouldn't be confused and parsed out.

string saveUrl='&save_new_URL=/apex/taskOverRide?who_id=' + this.who_Id + '&retURL=/' +this.who_id ;
//encode the URL
saveUrl=saveUrl.replace('&','%26').repalce('=','%3D').replace('/','%2F'); //.replace(otherNecessaryCharacters,accordingly)

  • 2
    Why not just use EncodingUtil.urlEncode and EncodingUtil.urlDecode? salesforce.com/us/developer/docs/apexcode/Content/…
    – drakored
    Commented Jun 6, 2013 at 19:18
  • saveUrl = EncodingUtil.urlEncode(saveUrl,'UTF-8'); url += saveUrl
    – drakored
    Commented Jun 6, 2013 at 19:19
  • 1
    Yup, that'll do it all too in one step. And it's nice to know what's actually being encoded. That's a useful class for converting to Base64 and back! Commented Jun 6, 2013 at 20:58

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