[SalesForce] Passing a paramater to make a new task from a button when multiple record types are available

I'm trying to create a custom button to create a task from an object's related list by using the URLFOR function. It will populate the who, what and owner Ids. The owner and whatId come in natively, and the save and cancel buttons aren't working as intended.

It's intended to work like this:

The user will click the button, goes to the record type selector (if appropriate), enters in the info into the task edit page. When they click save, i want the user to go to the new task. When they click cancel, I want them to go back to the referring link.

PROBLEM
It's fine for the case where there is no recordtype selector, but when a selector is appropriate, they are going back to the recordtype selector when they click cancel or save….

Code:

{!URLFOR 
( 
$Action.Task.NewTask, 
null, 
[ 
retURL=URLFOR($Action.my_object__c.View ,my_object__c.Id),
save__new_URL=THE_NEW_TASK_CREATED
who_id= my_object__c.my_object_ContactId__c, 
what_id= my_object__c.Id 
] 
) 
}

Best Answer

Dealing with record type selection in custom new buttons can be tricky. Even if a user has access to multiple record types they can set a default record type in which case they bypass the record type selection. So you'll need your button to work in both cases.

You've already got the general case handle, the key to making it work with the selection page is passing in the same url of the task creation page with your with values pre-populated and the return url into the save_new_url. After the record type is selected the user is sent to that page and a new paramater RecordType=XXXXXX is added. For example

{!URLFOR(
  $Action.Task.NewTask
, null
, [retURL= '/' + My_Object__c.Id
    ,  save_new_url = '/00T/e?who_id=' + My_Object__c.My_Object_ContactId__c 
    + '&what_id=' + My_Object__c.id
    + '&retURL=/' + My_Object__c.id
    , who_id = My_Object__c.My_Object_ContactId__c
    , what_id = My_Object__c.id
  ]
)}

Personally I've found it simpler to use /<object_id> for returning the view url in this case as using URLFOR seems to behave strangely in these scenarios. You also have to do this for the task edit url (/OOT/e?) since URLFOR will return the record type selection page. Unfortunately there isn't a good way to tell URLFOR to return the edit page vs. record type selection page.

For troubleshooting you'll want to make use of a standard button to learn how it works along with a url decoder for helping to see what values are in the resulting url.