Search code examples
salesforceapex-codevisualforce

retURL in URLFOR not redirecting correctly


I have a VF page that is embedded inline on a custom object page layout section. In the VF page, I used a command button to redirect to another custom object using the URLFOR New Action.

<apex:commandButton onclick="window.parent.location.replace('{!URLFOR($Action.Design__c.New)}');" value="Add Design" rendered="{!IF(designId == null, true, false)}" />

This works and opens the custom object in the create 'New' record mode. However, if I click 'Cancel' the retURL opens my VF page directly and not inline on the custom object page layout, which is very bad. I displays the VF page with no way to get back to the Salesforce UI without hitting the back button.

How can I set the retURL probably in the URLFOR method so when the user clicks cancel that it will redirect them back to the correct page?


Solution

  • You should be able to pass in a value for retURL (as well as any other additional URL parameters) along with URLFOR by doing something like:

    {!URLFOR($Action.Design__c.New, null, [retURL=SOMEOBJECTID])}
    

    You'll obviously have to replace "SOMEOBJECTID" with the Id of the custom object record you were originally viewing.

    Update:

    Additional information on the full usage of URLFOR can be found on this blog entry.