[SalesForce] URLFOR() not preserving retURL through Record Type selection

I'm trying to add an <apex:outputLink /> tag to a VF page and use the URLFOR() function to direct the user to create a new Case. I want it so that when they save or cancel on the new Case creation page they are redirected to the detail page of a custom object. My code is below.

<apex:outputLink value="{!URLFOR($Action.Case.NewCase, null, ['retURL'=URLFOR($Action.Custom_Object__c.View, 'a0A00000000IDID')], true)}" >Select</apex:outputLink>

My problem is that some users are first taken to the Record Type selection screen. Pressing cancel from this screen takes the User to my Custom Object detail page, but if they continue to the new Case screen and THEN hit cancel they are redirected to the VF page, or if they press save they are taken to the Case detail page. I've tried adding cancelURL and saveURL but they don't preserve through the Record Type selection screen.

How can I preserve the retURL through the Record Type selection screen? I must allow users to select a Record Type so hard coding this is not an option.

Best Answer

I focused on getting the Cancel action from new Sobject page to return to a page other than from whence I started. Apologies for not using Cases

  • Foo__c has record types
  • a09/e is the key prefix for Foo__c
  • ID is the ID of the record of the VF page where the outputLink exists - I used a standardController to test this; you could use something different here
  • someNonFooId represents your intent - go to a page other than where you came if user clicks Cancel on the new Foo__c page after selecting a recordtype. Your VF page should know what to put here.

Code

<apex:outputLink value="{!URLFOR($Action.Foo__c.New, null, 
                                 ['retURL'='/' & ID, 
                                  'save_new_url' = '/a09/e?retURL=/' & someNonFooId],
                                  true)}" >
    Create New Foo
</apex:outputLink>

The trick was to tell the record type selection page to use a provided parameter save_new_url as otherwise, the recordtype selection page will generate a save_new_url for itself with a retURL that goes back to whence it came (the page with the outputlink). Note the save_new_url parameter value contains a parameter retURL - this is what gets passed to the New Foo__c page and is used by the new Foo__c page's OOB cancel button. I probably could have made the value of save_new_url a URLFOR itself but ran out of time.

Of course, URL hacking the record type selection process is subject to SFDC change