Andy Tran

Salesforce URL Hacking in Lightning

by | 10 Apr, 2023 | Salesforce | 4 comments

Salesforce URL Hacking in Lightning

A Guide to Salesforce URL Hacking in Lightning

Initially, URL Hacking was not supported in Lightning Experience And was replaced by Custom Actions. However, in the Spring ’20 release in February, URL Hacks were reintroduced with some enhancements. This is great news for Administrators as they can once again utilize this wonderful feature, Even in Lightning Experience.

What is URL Hacking?

URL hacking has been around in the Salesforce world for a while now and is the process of pre-populating data in a new record using static or dynamic fields based on the record you’re currently on. It is an easy way of creating records with specific fields populated automatically, rather than setting up a Flow or involving Apex code. This method also allows us to utilise the Standard Page Layout rather than a custom solution.

For instance, if we want to prepopulate a field on the contact record while creation, we can build a custom button and prepopulate those fields from the parent Account record.

What changed in New URL Hacking?

After switching to the Lightning Experience from Salesforce Classic, the old URL Hacks had to be replaced by Custom Actions, however, new URL Hacks support both Lightning and Classic.

The old URL Hacking method involved the use of field ids in the URL, Which is now changed to the use of field API Names instead.

Note:
New URL Hacks are not available in Community, Salesforce Mobile App, and Lightning Out. (Hopefully, There will be an update to this soon.)

Let’s have some hands on, Shall we?

By using an example, We’ll show you how to create a URL hack in Salesforce Lightning. We guarantee that your experience will be engrossing, So Continue Reading!

Example

We’re going to create a Contact record from an Account and populate some of the Contact field values.
Fields we are gonna populate are :
AccountId
OwnerId
LastName
Active__c
LeadSource
Phone
MailingStreet
MailingCity
MailingState
MailingPostalCode
MailingCountry.

Steps to build a URL Hack

Steps to use Custom Buttons and create records by pre-populating some values :

  1. Create the custom button.
  2. Define values for the new record’s fields while creating the button. (Whether It is static or dynamic)
  3. Of Course, Add the button to your layout.

Step 1: Create the custom button

In order to do this, we will navigate to our Parent object in the Object manager (Here, Account object). This is the object on which we will be placing our button. Here in this example, we will select the ‘Detail Page Button’ value in the Display Type picklist.

New Button or Link

Note:
Here, you can’t choose the display type of button as ‘List Button’
Because there is a different URL syntax for it and in this article, we didn’t cover that.

Step 2 : Define values for the new record’s fields, By adding the Below URL In Blank space.


/lightning/o/Contact/new?defaultFieldValues=OwnerId={!Account.OwnerId},
AccountId={!Account.Id},MailingStreet={!URLENCODE(Account.ShippingStreet)},MailingCity={!URLENCODE(Account.ShippingCity)},MailingState={!URLENCODE(Account.ShippingState)},MailingPostalCode={!URLENCODE(Account.ShippingPostalCode)},MailingCountry={!URLENCODE(Account.ShippingCountry)},LeadSource=Contact+Using+Hack,LastName=Andy+Young,Active__c={!IF(Account.Active__c,true,false)},Phone={!IF(Account.Active__c ,"(785)241-6200","(785)265-5350")}

To make the above URL more readable, I’ll add some space to it.


/lightning/o/Contact/new?defaultFieldValues=OwnerId={!Account.OwnerId} , 
AccountId={!Account.Id} , 
MailingStreet={!URLENCODE(Account.ShippingStreet)} , 
MailingCity={!URLENCODE(Account.ShippingCity)} , 
MailingState={!URLENCODE(Account.ShippingState)} , 
MailingPostalCode={!URLENCODE(Account.ShippingPostalCode)} , 
MailingCountry={!URLENCODE(Account.ShippingCountry)} , 
LeadSource=Contact+Using+Hack , 
LastName=Andy+Young , 
Active__c={!IF(Account.Active__c,true,false)} , 
Phone={!IF(Account.Active__c ,"(785)241-6200", "(785)265-5350")}

Note:
We Can’t have space or line breaks in our URL. As I stated earlier, Space or line breaks were added in your URL to make it readable.

Let’s dig deeper into our URL

To learn the API Field Name of a field on the Child Object (Here, Contact Object) – This is the object for which a record is going to be created. Navigate to that object in Object Manager and refer to the Field Name column – These are the field API Names, For which we are populating values. We will insert merge field values of an object on which the button is placed and whose values will be populated to our child object, It means values of our Parent object.

Field & Relationships

Let’s look at the First part of our URL :


/lightning/o/Contact/new?defaultFieldValues=

Salesforce URL Hacking is only supported inside Salesforce. The button we’re creating already presumes that we want to launch information inside of Salesforce, So there is no need to write the Salesforce org instance part before the above line and we can directly begin with ‘ / ’ after our instance.

For Example, If your first line with the org instance looks like “https://wise-shark-8khduk-dev-ed.trailblaze.lightning.force.com/lightning/o/Contact/new?defaultFieldValues=”, you can directly put a URL line without an instance of your Salesforce org. It means your URL line can look like this, “/lightning/o/Contact/new?defaultFieldValues=”

What we’re doing in this line is specifying the object for which we want to create a record – Contact – we are also describing that we want to create a new record using the “new” keyword. We are preparing to populate the ‘default field values’ parameter with static or dynamic values.

Each field will be comma-separated and part of the URL parameter. Where parameters are in a key=value format. So for this example, our key is defaultFieldValues and our values are a combination of Keys and values for the field and their values.

Let’s take a look at the next parameters now:


OwnerId={!Account.OwnerId} , AccountId={!Account.Id} ,

Here, We are indicating that the Owner of the Account is also a Owner of the newly created Contact record. We are also ensuring that the Contact is related to the existing Account.


MailingStreet={!URLENCODE(Account.ShippingStreet)} , 
MailingCity={!URLENCODE(Account.ShippingCity)} , 
MailingState={!URLENCODE(Account.ShippingState)} , 
MailingPostalCode={!URLENCODE(Account.ShippingPostalCode)} ,
MailingCountry={!URLENCODE(Account.ShippingCountry)} , 

Here we are quoting that MailingAddress’ values of Contact Object are Same as ShippingAddress’ values of Account Object. We are also using the URLENCODE() function, And Encoding these values in URL format.

For Dynamic values (Values which are populated using merge fields) If those fields have any special characters or spaces in it, We have to use the URLENCODE() function. Otherwise, Salesforce wouldn’t allow special characters or spaces included in these values. But for Static values, Salesforce provides a feature to encode those static values into URL.


LeadSource=Contact+Using+Hack , LastName=Andy+Young ,

Here, we are indicating to put Static values for LeadSource and LastName fields. For Static string values, Plus(+) signs between two strings are considered as a single space between those strings.

For me while testing, These plus(+) signs didn’t cause any trouble, But Salesforce doesn’t suggest space between two strings (As it is a URL) and prefers to put (+) sign where space is required between two strings.


Active__c={!IF(Account.Active__c,true,false)} , 
Phone={!IF(Account.Active__c ,"(785)241-6200", "(785)265-5350")}

In the last two lines, We are stating that the value of Active__c ( On Contact Object) is the same as Account.Active__c ( On Account object). It means if the value of Active__c on Account object is checked/true then value of Active__c on contact object will also be checked/true, vice-versa.

In this case, Salesforce does not provide a feature to populate the value of a checkbox directly, Thus we are populating the value using IF() function.

Step 3: Add the button to your layout.

Don’t forget to Check, Syntax of the URL and make sure there are no errors in it. Now we can save the button. Then we must add the button in the page layout to use it.

Go to the page layout of our Parent object and you can see our new button in Mobile & Lightning Actions. Add the button to your layout and save the layout.

Testing !

  • Now, Go to the record page of Account object which supports our layout.

New Contact Using Hack

  • Press the “New Contact Using Hack” button. 
  • Congratulations !! you can see values of fields, you stated in the URL are pre-populated using a custom button.

Contact Information

Salesforce URL

Important Notes:

  1. You can’t add line breaks/carriage returns and space in the URL.
  2. For using URL Hacks with recordTypes, First we must include recordTypeId before defaultFieldValues in the URL. In this case, our URL will look something like this :
    “/lightning/o/Contact/new?recordTypeId={Enter recordTypeId Here}&defaultFieldValues=”
  3. If there is no ‘=’ sign between the field API name and its value, Salesforce doesn’t show any syntax error but the value is not populated.
  4. Dynamic values are suggested to be URL Encoded, Or the URL will not take any special character or space from the field’s value.
  5. Date fields must be in proper format like it is in SOQL, Otherwise Date fields will not get Populated.
  6. Checkbox fields can’t have other field’s values directly.
  7. When there is field dependency between two fields, If you want to populate values of Dependent fields then you also have to populate values of Controlling fields (It is a basic rule), And don’t forget that in field dependency, Those values should be included In the same column.

Some Interesting Facts

  • With URL Hack, We can also create records for the same object on which our button is placed. It means we can clone records also.
  • URL Hack supports both, Creating and Editing of a record.
  • The reason these are considered as “Hacks” is because Salesforce does not support them, But Salesforce do support pre-populating of the field values using merge fields. That is what we are doing here, we are prefilling new record’s values with URLs by adding merge fields as a parameter in URLs.

Also Read:  What is Salesforce and Why does Your Business need it?

Thank you for checking out our blog! If you’re seeking Salesforce-related services for your company, your search ends here. Erudite Works, a leading Salesforce consulting partner, specializes in expert strategies, implementation, and customization of Salesforce CRM to enhance your business automation and reporting needs. Our certified team ensures a seamless CRM solution and top-notch consulting services. Contact Us to discover more about our services.

Share this article...

4 Comments

  1. Somprakash Pradhan

    I was looking for how to do URL hacking in Lightning for a while as I wanted to implement it but was unable to find the correct description of the same. Finally, I have understood URL hacks clearly and implemented it through your blog. Thank you!!

    Reply
    • Nikhil Raycha

      Great Somprakash, Thank you for reading.

      Reply
  2. Shailesh Patil

    Great content, I’ve read many articles on how to implement URL Hacking in Lightning but from this article
    I’ve got a simple and clear explanation.

    Reply
    • Nikhil Raycha

      Thank you, Shailesh Patil.

      Reply

Submit a Comment

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.