When Lightning Experience first launched, it didn’t include the URL hacking functionality that was available in Salesforce Classic. However, with the Spring ’20 release, URL hacks made a comeback in Salesforce. Let’s dive into how you can use Lightning URL hack in Salesforce to create new records with default field values and navigational parameters.
Salesforce URL Hack
A URL hack is a customized button that pre-populates particular fields with data from the current record to automate the generation of new records. This removes the need for repetitive data entry by hand, enabling efficient data entry.
For example, You have an open Opportunity record. Clicking a custom “Configure Quote” button creates a new Quote record. The new Quote’s record’s Opportunity ID and Account ID fields are automatically filled with the data from the Opportunity record.
Let’s see, how we can build a URL hack to create a Quote record with some per-populated default field values while creating from an Opportunity object.
1. Creating a Custom Button with a URL in Salesforce
- Go to Setup > Object Manager.
- Click on the ‘Opportunity’ object.
- Click on Buttons, Links, and Actions.
- Click on the New Button or Link.
- Label: Enter a descriptive name for the button – “Configure the Quote.”
- Display Button: Choose “Detail Page Button.”
- Select behavior from the drop-down – ‘Display in a new window’.
- Provide a formula for pre-populating fields of Quote as per Opportunity –
“/lightning/o/Quote/new?
defaultFieldValues=
OppotunityId={!Opportunity.Id},
Name={!Opportunity.Name}
AccountId= {!Opportunity.Account}”
“
- Click on Save.
2. Salesforce Lightning URL parameters
The URL structure allows you to create a new Quote record and populate specific fields with default values directly from the current record. (ex – Opportunity Record)
- Base URL: /lightning/o/Quote/new? – This part specifies that we’re creating a new Quote record.
- Default Field Values: – defaultFieldValues= This parameter lets us pre-fill fields on the new Quote record.
- Dynamic Values: The default values follow the equal sign (=) and are comma-separated. These values are dynamic because they reference fields from the current record (For ex- Opportunity Record) using Salesforce field merge syntax ({!Field Name}).
Pre-filling Quote Details from Opportunity
- OppotunityId={!Opportunity. Id}, : This ensures the newly created Quote is linked to the current Opportunity.
- Name={!Opportunity.Name},: This line copies the
Name
field from the current Opportunity and sets it as the Name of the new Quote. - AccountId= {!Opportunity.Account},: This populates the “Account” field on the Quote with the Account associated with the current Opportunity.
3. Add on Page Layout
- Go to Setup > Object Manager.
- Select Opportunity.
- Under Page Layout, click on Opportunity Page Layout.
- Click on ‘Mobile and Lightning Actions’.
- Drag the ‘Configure the Quote’ button on the ‘Salesforce Mobile and Lightning Experience Actions’ section.
- Now, save the changes.
4. Test in the UI
Steps –
- Create an Opportunity: This will be the parent record of the Quote you’ll create.
- Navigate to the Opportunity: Open the Opportunity record you created.
- Click the Button: Locate the “Configure the Quote” button on the Opportunity page layout and click it.
- Create a New Quote from the button “Configure the Quote”.
5. Use of Navigation Location Salesforce
Navigation location in Salesforce – This attribute specifies the page or location to return to once the save operation is complete.
Scenario –
We’ve created a custom “Create New Contact” button on the Account page. After saving a new Contact, we want users to return to the same Account page where they created the Contact. The newly created Contact should then be visible in the related list on the Account page.
How we can achieve this –
To achieve this, we can add &navigationLocation=RELATED_LIST the parameter to the URL formula in the “Create New Contact” button. This will redirect the user back to the Account page after saving the new Contact, and the Contact will be automatically visible in the related list.
/lightning/o/Contact/new?
defaultFieldValues=
LastName={!Account.Name},
Phone={!Account.Phone},
AccountId={!Account.Id}
&navigationLocation=RELATED_LIST
- Create a new Contact using the custom button on the Account Record page, and ensure that after saving the new Contact, the user is redirected back to the Account Record page rather than the Contact list view.
See how a Lightning URL hack with default values and navigation location works on UI below:
Also, Learn about 100 + Visual Studio Code Shortcuts
FAQs
1. What is Lightning URL?
Lightning URLs are web addresses designed for navigating to specific records, pages, or actions within Salesforce Lightning Experience.
Example of base URL – https://d5i000009pjbueag-dev-ed.lightning.force.com
2. What is a URL hack?
Conclusion
In this blog, we’ve covered how URL hacking works in Salesforce Lightning and shared some important tips to keep in mind when using it. We also walked through how to create a custom button that takes advantage of Lightning URL hacking.