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....


retURL=URLFOR($Action.my_object__c.View ,my_object__c.Id),
who_id= my_object__c.my_object_ContactId__c, 
what_id= my_object__c.Id 

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

, 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.

  • Awesome this works great! Thanks Ralph! Quick question, can you give some more detail on why construcing the save_new_url like that fixes the issue? I want to make sure I get what's going on here – PartOfTheOhana May 7 '13 at 14:24
  • If you don't have a record type you end up directly at the url for your button, but if you have record types you go first to the record type page and then to whatever is in save_new_url. Any variables you passed into the original URL aren't automatically transferred so you have to put them in the save_new_url as well. It's not ideal, unfortunately URLFOR wasn't designed all that well for this use case. – Ralph Callaway May 7 '13 at 20:12

The button needs to have the record type hardcoded into it to avoid this problem. Here's an example taken directly from a button code dialogue page in an org I recently did some work in:

Button code of URL prams to create a calendar event and fill the description with instructions. 
&evt5="Event:"+ {!Opportunity.Name}+"at"+{!Opportunity.Event_Start_Time__c}+"for"+{!Opportunity.Duration__c}+"Hrs". 
&evt6="Your Event ID is "+{!Opportunity.Id}+" "+"{!Opportunity.OwnerFullName}"+" has scheduled an event at "+{!Opportunity.Event_Start_Time__c}+{!Opportunity.CloseDate}+{!Opportunity.Account}+"Address"+{!Opportunity.Account_Address__c}+", Phone "+{!Opportunity.Account_Phone__c}+" estimated to last "+{!Opportunity.Duration__c}+" hours. 

BTW, this was for a new event, but the concept would still be the same.

  • I need the RT selector. Otherwise, I'll have to do VF/Apex – PartOfTheOhana May 6 '13 at 19:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.