1

This button code overrides the new button on a custom object. It sends the user to the record type selector, and then passes in a couple of params. When the user gets to the new record edit page, and THEN clicks cancel, he goes back to https://cs16.salesforce.com/home/home.jsp not the accountId page as I want. Can someone help me with the syntax?

       Schema.DescribeSObjectResult dsr = customObject__c.SObjectType.getDescribe();
        Account a = [select id, name from Account where id = :this.AcctId];

        url = '/setup/ui/recordtypeselect.jsp?';
        url += 'ent=' + '01I30000001ITcu&';
        url += 'retURL=/' + a.id ;
        url += '&nooverride=1';

        url += '&save_new_url=/' + dsr.getKeyPrefix() + '/e?';
        url += 'CF00N3000000689TM=' + EncodingUtil.urlEncode(a.name, 'UTF-8') ; 
        url += '&CF00N3000000689TM_lkid=' + a.id;           
        url += '&retURL=/'+ a.id; 

1 Answer 1

3

The issue here is that the save_new_url value needs to be encoded so that those parameters don't replace the parameters with the same names in the recordtypeselect.jsp URL. Note that I used the encoded %2F in place of the / in the saveNewUrl string.

I typically use a PageReference to do this as it automatically handles the URL Encoding and is a lot less fragile and error-prone than building a string by hand.

PageReference pageRef = new PageReference('/setup/ui/recordtypeselect.jsp');
pageRef.getParameters().put('ent', '01I30000001ITcu');
pageRef.getParameters().put('retURL', '/' + a.Id);
pageRef.getParameters().put('nooverride', '1');

// using string.format for parameter substitution pattern
String saveNewURL = String.format('/{0}/e?CF00N3000000689TM={1}&CF00N3000000689TM_lkid={2}&retURL=%2F{3}', new List<String> { dsr.getKeyPrefix(), EncodingUtil.urlEncode(a.name, 'UTF-8'), a.Id, a.Id });
pageRef.getParameters().put('save_new_url', saveNewURL);

// output the generated, properly-encoded URL
system.debug(pageRef.getURL());

Example debug output, with the second retURL value now double-encoded:

|DEBUG|/setup/ui/recordtypeselect.jsp?ent=01I30000001ITcu&nooverride=1&retURL=%2Fa.Id&save_new_url=%2F001%2Fe%3FCF00N3000000689TM%3Da.name%26CF00N3000000689TM_lkid%3Dabc123%26retURL%3D%252Fabc123
3
  • Thanks, that worked Mark, and I learned about the String.format() method! can you give some more details about this: The issue here is that the save_new_url value needs to be encoded so that those parameters don't replace the parameters with the same names in the recordtypeselect.jsp URL. Note that I used the encoded %2F in place of the / in the saveNewUrl string. Commented May 15, 2014 at 17:38
  • 1
    In your original markup, you had two values for retURL directly exposed in the URL for the recordtypeselect.jsp page. What you intended was have one of these apply to the recordtypeselect.jsp page and one for the following page within the save_new_url parameter value. Because the second retURL was not encoded, it was 'visible' to the recordtype page and used by that page. If you were to look at the URL after you selected the recordtype, you would have seen that there was no retURL value there. This allows you to pass the second url value through as an encoded parameter.
    – Mark Pond
    Commented May 15, 2014 at 18:58
  • @MarkPond Hi Mark, I am also facing the same kind of issue here, and unable to solve from long time - will you look into this salesforce.stackexchange.com/questions/101901/… Commented Dec 9, 2015 at 10:55

You must log in to answer this question.

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