What exactly is a Salesforce URL Hack?
In essence, a URL Hack is a customized button designed to initiate the creation of a new record while predefining certain default values using either static or dynamic fields based on the current record context.
For instance, suppose I have an Account record open on my screen. By clicking on my custom ‘New Contact’ button, I can automatically populate some of the Contact’s details based on those of the parent Account (such as Account Id, Phone, and Address).
URL Hacks serve as a convenient means of generating records with specific fields pre-filled, as opposed to applying a default at the field level universally across all new records. Moreover, this approach allows for the utilization of the standard Page Layout rather than necessitating a custom solution.
Distinguishing Between Lightning URL Hacks and Actions, and Classic URL Hacks
What sets apart the latest URL Hacks from the previous ones, or Custom Actions? The former Classic URL Hacks exclusively operated within Salesforce Classic and had to be substituted with Custom Actions upon transitioning to the Lightning Experience
Additionally, the prior URL Hacks relied on field Ids (notably cumbersome), whereas the new ones utilize API names, significantly enhancing readability should modifications to existing buttons become necessary.
Are there any constraints?
Regrettably, the new URL Hacks come with rather stringent limitations. These buttons cannot be utilized within a Community or in the Salesforce Mobile App. Furthermore, they are incompatible outside of the Lightning environment.
How to Construct a URL Hack? To initiate the creation of records using a Custom Button, you must:
- Create the button.
- Add the button into your layout for testing purposes.
- Specify values for the fields of the newly generated record – whether they are static or dynamic based on the record context. Further details on this will be provided shortly.
Let’s delve into an illustrative example. Suppose we aim to establish a Contact record derived from an Account while prefilling certain default fields. The objective is to make the button accessible from the Account (within a Business Account Page Layout) and have it automatically populate fields such as Owner, Lead Source, Mailing Address, and establish a relation between the Contact and the Account.
Step 1: Create the Custom Button
To accomplish this, we must access our Master Object in the Object Manager – this is the object where we intend to position our button. In our scenario, the Account serves as the Master Object.
While proceeding with the button creation steps, ensure to choose the ‘Detail Page Button’ option from the Display Type picklist; this guarantees its availability as a standard button, akin to a Custom Action.
Step 2: Add in the URL
Next, we’re going to add in our URL. Below is the completed code example.
/lightning/o/Contact/new?defaultFieldValues=
OwnerId={!Account.OwnerId}, AccountId={!Account.Id},
MailingStreet={!Account.ShippingStreet},
MailingCity={!Account.ShippingCity},
MailingState={!Account.ShippingState},
MailingPostalCode={!Account.ShippingPostalCode},
MailingCountry={!Account.ShippingCountry},
LeadSource=Custom+Contact+Button
What does that all mean? Let’s go through it!