Prepopulate Values in Salesforce

Tzhe’ela Trooper
4 min readFeb 10, 2022

To keep clicks to a minimum and provide a better user experience, it is important to prepopulate values whenever possible on the platform.

Salesforce provides this option with quick actions for STANDARD objects . In the example below, I’m prepopulating the Name field based on the name of the context user. I trust you to find a use case that will actually make sense.

When working with CUSTOM objects however, the above option is no longer relevant and there is a need for some creativity.

Prepopulate values through the URL

Salesforce provided a way for us to pass values to the URL of a screen action \ quick action in the form of key value pairs.

Official documentation - Launching Record Create Page with Default Field Values

Suggestion from Salesforce’s documentation:

/lightning/o/Account/new?defaultFieldValues=
Name={!URLENCODE(Account.Name)},
OwnerId={!Account.OwnerId},
AccountNumber={!URLENCODE(Account.AccountNumber)},
NumberOfEmployees=35000,
CustomCheckbox__c={!IF(Account.SomeCheckbox__c, true, false)}

If you copy the above example and test it out in your org, you will notice that the background behind the popup modal is white and no longer displays the page you were on. To fix that, add the attribute backgroundContext to the end of the URL. For example:

&backgroundContext=%2Flightning%2Fo%2FAccount%2Flist%3FfilterName%3DRecent

Unfortunately, I’m not aware of a magic variable to capture your current URL params and pass them to the encoded backgroundContext.

Prepopulate values through a custom LWC form

If you are after a more custom experience and still want to use lwc recipes you should look into lightning-record-edit-form .

Official Documentation - LWC lightning record edit form .

The docs are long for this one, but provide an example for prepopulating just before the Submit() on the JS side:

If you want to prepopulate the values on the form BEFORE rendering the form, giving the user an opportunity to review and change the values if needed, another approach is needed.

On the html condition the presentation of the form, and use custom attributes to populate the value, <lightning-input-field field-name="AccountId" value={accIdVal}></lightning-input-field> .

The example below shows how to prepopulate the AccountId field it you want to create a new ‘sibling’ Contact from the Contact record page.

First the pictures, then the actual code:

Note: If you are not trying to prepopulate an Id field (lookup \ master-detail), you don’t necessarily need to use the getter approach.

Now the code blocks if you want to copy paste:

html <template><lightning-quick-action-panel header="New Contact"><lightning-record-edit-form if:true={contact.data} object-api-name="Contact" onsubmit={handleSubmit} onsuccess={handleSuccess}><lightning-messages></lightning-messages><div class="slds-m-around_medium"><lightning-input-field field-name="AccountId" value={accIdVal}></lightning-input-field><lightning-input-field field-name="FirstName"></lightning-input-field><lightning-input-field field-name="LastName"></lightning-input-field><div class="slds-m-top_medium"><lightning-button variant="brand" type="submit" name="save" label="Create"></lightning-button></div></div></lightning-record-edit-form></lightning-quick-action-panel></template>JSimport { LightningElement, api, wire } from "lwc";
import { getRecord, getFieldValue } from "lightning/uiRecordApi";
import ACCOUNT_ID_FIELD from "@salesforce/schema/Contact.AccountId";
export default class NewContactDemo extends LightningElement {
@api recordId;
@api objectApiName;
@wire(getRecord, { recordId: "$recordId", fields: ["Contact.AccountId"] }) contact;
get accIdVal() { return getFieldValue(this.contact.data, ACCOUNT_ID_FIELD);}
}
xml<?xml version="1.0" encoding="UTF-8" ?><LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata"><apiVersion>53.0</apiVersion><isExposed>true</isExposed><targets><target>lightning__RecordAction</target></targets><targetConfigs><targetConfig targets="lightning__RecordAction"><actionType>ScreenAction</actionType></targetConfig></targetConfigs></LightningComponentBundle>

Another note: yes I know you can nest a gitHub gist on medium… one day I’ll organize my gists and replace the above block and pics .

Other ways to Prepopulate

The above 3 are only a few examples for prepopulating values. Other ways include (but not limited to *caveat*) using Screen Flows, Trigger Flows, Apex Triggers, and other automations such as process builder and workflows (which will be retired at some future point).

Hope this will be helpful to someone, or at least to my future self.

Thanks for reading.

For SEO purposes, this article tried to answer the below questions:

How to prepopulate values in a quick action? How to auto populate values through URL params on Salesforce? Why the background is empty when auto or prepopulating URL params? How to prepopulate lightning-edit-form or lwc form on load? How to prepopulate a lookup field on lightning-edit-form? Why the LWC lookup field is not prepopulating with given value? Why the lookup field on LWC is not rendering with the prepopulated value?

--

--