Pages

Wednesday, November 14, 2012

Salesforce: Custom Button to pre-populate Fields

Out-of-the-box, when we create a new record from a parent object related list, Salesforce.com provide parent name pre-populated in child object. This is efficient and save time, user do not need to look for the parent record again.





This is good, but, often we need more fields to be pre-populated for some scenario and make our (read: Salesforce users) life easier :)
We can make this happen by create new custom button whether in related list or in page layout. In this sample, we'll use Account as parent and Opportunity as child.

NOTE: This is considered a URL hack and it is not supported by Salesforce. Using undocumented query strings is absolutely discouraged as Salesforce can change them at any time without notice.

Custom button in page layout
Go to Account object in Setup menu and choose Buttons and Links. Click New button and select option as screenshot below. Once button created, you need to add the button into Account page layout.


Custom button in related list
Go to Opportunity object in Setup menu and choose Buttons and Links. Click New button and select option as screenshot below. Once button created, you also need to add the button in Opportunity related list in Account page layout.





OK, now we understand how to create the custom button, but what is the URL??? Here we go...

If you click Opportunity button in related list, see the URL
https://na1.salesforce.com/006/e?retURL=%2F0019000000DjwR5&accid=0019000000DjwR5&RecordType=01290000000NLfk&ent=Opportunity

So, what is the parameters and values? Let's parse it:
https://{instance}.salesforce.com/{object_prefix}/e?retURL=/{!Object.Id}&accid={!Object.Id}&RecordType={RecordTypeId}&ent={object_name}

In this sample there is record type in Opportunity and we want to skip Record Type selection page when user click new Opportunity by populating Record Type Id in the URL. So, this would be the URL:
/006/e?retURL=/{!Account.Id}&accid={!Account.Id}&RecordType=01290000000NLfk&ent=Opportunity


Now, we want to pre-populate other field from Account. Here we go... you need to find out the Id of textbox from web browser, in page edit the record and right click at textbox then select Inspect element

Once you get the id, add it into the URL:
/006/e?retURL=/{!Account.Id}&accid={!Account.Id}&RecordType=01290000000NLfk&00N90000000iQg4={!Account.Hello__c}&ent=Opportunity


Custom object
Parameter for pre-populate for lookup to an custom object id different, see sample below:
https://na1.salesforce.com/a0E/e?CF00N900000022p5z=Object+2A1&CF00N900000022p5z_lkid=a0D90000001OPvT&saveURL=%2Fa0D90000001OPvT&retURL=%2Fa0D90000001OPvT

Let's parse it:
https://na1.salesforce.com/{object_prefix}/e?CF00N900000022p5z={parent_object_name}&CF00N900000022p5z_lkid={parent_object_id}&saveURL=/{parent_object_id}&retURL=/{parent_object_id}

Notice that parent name is CF00N900000022p5z and parent id is CF00N900000022p5z_lkid and a new parameter saveURL to redirect the page after user hit Save button.


Considerations
Before you add more and more fields pre-populated, please consider this:
  • Sometime, if field in child object is not edit-able, consider to use formula field in child object
  • If the field do not need to copy from parent, you can hardcode it into the URL
  • To pre-populate data with some logic, you need visualforce page with apex code.

Video
Video created by 'Christian Deckert' is simply good to guide you step-by-step in creating URL for your custom button.


Auto-save
Some of you may looking for a way to autosave the record, Salesforce used to support parameter save=1, but for security reason to prevent Cross-Site Request Forgery (CSRF), this parameter has been removed.
You may look for AJAX toolkit to update + save record from a button.

4 comments:

  1. hi Johan - thanks for this useful guide. In your considerations section you state that to pre-populate with logic you need a visualforce page and apex code. I want to prepopulate fields, but only if the field in the Parent is populated. (At the moment it performs some action that results in error for that field saying: "Error: You must enter a value"). A minor issue - but one that might confuse my users. As a mostly-non-coder can you point me to any ready made code for this scenario? Thanks

    ReplyDelete
    Replies
    1. Hi Andrew, I just test using URL, when pre-populate from parent record field is blank, the value in child new record will be blank as well.

      sample:
      /006/e?retURL=/{!Account.Id}&RecordType=01290000000NLfp&opp3={!TODAY()}&opp4_lkid={!Account.Id}&opp4_lkold={!Account.Name}&00N90000000iQg4={!Account.Pulau__c}

      If Account.Pulau__c is blank, value in new record creation for 00N90000000iQg4 field will blank as well.

      Cheers.

      Delete
    2. Hi Johan,

      I came across a similar blog that talked through the same method for creating an opportunity from a contact record (http://www.opfocus.com/2012/12/use-a-custom-button-to-pre-populate-opportunity-fields/). I keep getting an error: The value of the "conid" parameter contains a character that is not allowed or the value exceeds the maximum allowed length. Remove the character from the parameter value or reduce the value length and resubmit. Here's my URL...do you see anything that looks funky with how conid is used? Thanks!

      006/e?lookupcmpgn=1&retURL=%2F{!Contact.Id}&ent=Opportunity&conid={!Contact.Id}&opp6={!Contact.LeadSource}&00Nd0000007Jbhw={!Contact.Lead_Details__c}

      Delete
    3. Trask, the URL is seems to create new opportunity from Contact page. How/where do you call that button / link from?

      Delete

Page-level ad