Sunday, May 26, 2019

Prepopulate Field values through URL Hacking in Salesforce

One of the common use cases in Salesforce is to prepopulate some field values while creating records without using Apex or any custom Visualforce page. The values can be static or may be coming from parent record etc. We can easily achieve this by customizing the URL of the new record page and we can launch the customized URL by creating a custom button.

Let’s start by getting a simple understandings that how Salesforce URL works by looking at an example. Navigate to any Account record in your Salesforce instance and go to the Contacts related list. Now click on the New Contact button to create a new contact record. The format of the URL is similar like the following:

https://{yourinstance}.salesforce.com/003/e?retURL=%2F0017F00000HOxcx&accid=0017F00000HOxcx

Let’s briefly discuss about each of the parts of the URL.


003/e
This 3 letters ‘003’ denoting that we are looking  at a particular object record(Contacts for our case), and ‘e’ signifies that we are editing the page to create a new record of the object..
retURL
When you click the ‘Cancel’ button in a new record page, Salesforce redirects to that link mentioned in this parameter.
accid=0017F00000HOxcx
This is the parent record Id(Account Id in this case) passed as a parameter and it is used to prepopulate the Account Name in Contact record.



Let’s start the process step by step:

Step 1: Create a Custom Button:

To Customize the link, we need to create a custom list button that will replace the standard New Contact button in related list of Account. Please check the screen shot to create the same. For now we are using the standard link in URL section.



Step 2: Add the button to the layout:


Let’s add this button to the Contact related list of Account which can be found in Account page layout and remove the Standard New button first, then we need to put the customized link on the newly created button. Navigate to Account page layout, click Related list on the palette section, Go the Contact related list, click on the Edit properties, click on buttons, uncheck standard New button and select the custom New Contact button as follows:




Step 3 : Customize button URL to Prepopulate Fields with simple values:


To prepopulate values, we need to two things, i.e. the Id of the field for which we are prepopulating values and the value itself. For example we are taking First Name of Contact to be auto populated. To get the Id, use Inspect Element of Chrome.



With a static value binding with the Id ‘name_firstcon2', we need to put the Id value combination in the custom button link as a parameter.




And Here is the output: Salesforce auto populates the value of First Name on the Contact record.



Also we can populate values from the parent object by Merge Fields.

Step 4 : Prepopulate custom look up fields:

There are 2 parameters that we need to auto populate custom look up fields. The first part is used to populate the name of the record we are populating. To do the same, we need the Id of the field as previous, but this time we need the prefix “CF” to be added with the Id. This is required when we are working with a custom look up field. 
After adding the parameter we can expect the button parameter should look like this:

/003/e?CF00N7F00000RoX7i= {!Opportunity.Name}

The next step is to grab the Id of the Opportunity record we wish to populate. This time we have to add a parameter on the URL with the look up field ID suffixed with “_lkId”. The suffix “_lkId”  is denoting that this is a look up field. 
So after adding the same, the URL should look like the following:

/003/e?CF00N7F00000RoX7i= {!Opportunity.Name}&CF00N7F00000RoX7i_lkId= {!Opportunity.Id}





No comments:

Post a Comment