[SalesForce] Strange behavior when passing parameters in retUrl and saveUrl

I'm using a VF page to show Contacts. I have a group of categories (related lists) which are checked or unchecked. When I perform an action on the page, e.g. View, Edit, New, I want SF to remember which categories are checked, and check them again when the user clicks the back button or saves the record.

To do this, I'm setting retUrl and saveUrl where necessary, adding a parameter "rels" onto both which holds the keyPrefixes of the checked related lists. E.g. /apex/MyApexPage?id=003xxxxxxxxxx&rels=a09,a0B,a0K

Here is the code I'm using for this:

<apex:outputLink value="/{!fvv.recordId}/e?retURL={!URLENCODE('/apex/Codi?id='& contact.Id & '&rels='& preSelectedRelListsKeyPrefix &'')}&saveURL={!URLENCODE('/apex/Codi?id='& contact.Id & '&rels='& preSelectedRelListsKeyPrefix &'')}">Edit</apex:outputLink>

When I hover over the links I see the correct retURL and saveURL. This means that preSelectedRelListsKeyPrefix is populating correctly.

In the next page I see the Encoded URLs, i.e.

https://na1.salesforce.com/a0GU0000005nFJRMA2/e?retURL=%2Fapex%2FCodi%3Fid%3D003U000000PL9LNIA1%26rels%3Da0G&saveURL=%2Fapex%2FCodi%3Fid%3D003U000000PL9LNIA1%26rels%3Da0G

When I click the back button, I look for the rels parameter in my controller. However, this is null. In the URL rels is missing as well.

However, the weird thing is that it works if I perform an action in the page. Let's say I edit an object in the a0G related List. So the URL I generate would be the same as above.

. Now, if I edit the record, the rels parameter is correctly set, and the salesforce 'newid' parameter is attached, like so: https://na1.visual.force.com/apex/Codi?id=003U000000PL9LNIA1&rels=a0G&newid=a0GU0000005nFJR . But if I click the back button without editing, it doesn't work. And yet I checked for differences between my saveUrl code and my retUrl code and they are identical.

Likewise, the "New" and "View" buttons are not passing the parameter properly. I'm kind of stuck what to do here, since I'm passing everything correctly from my side and it seems to be salesforce that doesn't attach the parameter on the way back.

Best Answer

I managed to make this work by using the Cookie class. Instead of outputLinks, I used a commandLink and constructed a Page Reference in the controller like so.

PageReference pr = new PageReference('/'+recordToView+'/e?retURL=/apex/Codi?id='+contact.Id+'&rels='+preSelectedRelListsKeyPrefix);

Cookie relCookie = new Cookie('Checked Related Lists', preSelectedRelListsKeyPrefix, null, 1440, false);

pr.setCookies(new List<System.Cookie>{relCookie});
return pr;

Then in my constructor if the url parameter is empty I check for the Cookie

        Cookie rels = ApexPages.currentPage().getCookies().get('Checked Related Lists');

The result - the checkbox state is maintained even if the user clicks the Browser back button.