Last Updated on December 3, 2020 by Rakesh Gupta
Big Idea or Enduring Question:
- How can you avoid hard coding of ID in Lightning Flow or Process Builder?
Objectives:
This blog post will help us to understand the following
- Understand how to use custom Label to avoid hard code of ID in the Flow or Process
- Understand how to use custom setting to avoid hard code of ID in the Flow or Process
- Understand how to use custom metadata type to avoid hard code of ID in the Flow or Process
Business Use Case
Donna Serdula is working as a System administrator at Universal Containers (UC). She has received a requirement from the management whenever an account is created for Industry Not For Profit, auto-create a case and assigns it to Nonprofit Experts queue to verify the organization’s nonprofit status.
Automation Champion Approach (I-do):
Yes, it is sometimes a good idea to hard-code values (for example, while learning Apex or Process Builder or hard code Pie value i.e. 3.14), but there’s no simple rule as to when; it depends completely on context. If you’re hard coding the value of the earth’s gravitational constant, no one’s going to care. If you hard code the record ID (Queue, Group or Salesforce record Id, etc.), you’re in for trouble.
Time and time again, I see Apex code, Flows or Processes that contain hard-coded Ids, whether it’s the Description field, a user ID, or even a group ID, etc. The problem with hard coding IDs is that any changes need to be made to the Flow or Process itself, test again, and then deploy to production org.
The record IDs can change, for example between a sandbox and a production environment. It might be possible that a queue (Dupe Management) has a different ID in a sandbox and a production org.
Creating a Queue Nonprofit Experts
- Click Setup.
- In the User Interface, type Queues.
- Click on the New button.
- Enter Label the Queue Name will auto-populate.
- Click Save.
Now you can refer the Non-Profit Experts queue id while creating a case through Process Builder.
The problem with this approach (Hard-coding queue ID) is, If you are developing in the Sandbox, the IDs of the newly created Queue will change when you get to Production, and you have to do the rework.
–> As per Salesforce best practice, everyone will suggest you don’t Hardcode IDs, Query for them.
It is always good to follow Salesforce best practices where possible. At the moment Process Builder doesn’t allow to query and save the outcome (means record ID, field value, etc.) in a variable, even you can’t save newly created case ID (case created through Create a Record – process action) in a variable.
So what next? How to avoid Hard coding of IDs in Processes?
Some of the ways to avoid hard-coding are as follows
- Custom Label: – Custom labels are custom text values that can be accessed from Apex classes, Visualforce pages, Flows, Process Builder, or Lightning components. While deploying a custom label, its data also gets deployed. To avoid unexpected behavior make sure to update the custom label value post-deployment.
- Custom Setting: – Custom Settings are variables that you use in your code but set and modify outside of your code. Custom settings are cached. It is especially useful to store information that will be often accessed from Apex code or Flow as it will perform better than a custom object. It does not count against SOQL limits when fetched. You can use hierarchy custom settings in Process Builder directly, not list custom settings. The custom setting doesn’t bring data over when you deploy them.
- Custom Metadata Type: – Custom Metadata Types are similar to Custom Settings. While deploying a custom metadata type, its data also gets deployed.
Before discussing the solution, let me show you a diagram of a Process Flow at a high level. Please spend a few minutes to go through the following Flow diagram and understand it.
Let’s begin building this automation process.
Guided Practice (We-do):
There are 5 steps to solve Donna’s business requirement using Process Builder. We must:
- Creating a custom label to store chatter group Id
- Define process properties
- Define evaluation criteria
- Define process criteria
- Add action – create a record
Step 1: Creating a Custom Label to Store Queue Id
Now create a new custom label to store the queue (Nonprofit experts) Id.
- Click Setup.
- In the User Interface, type Custom Labels.
- Click on the New Custom Label button.
- Enter Short Description the Name will auto-populate.
- Now enter the Universal Containers Customers chatter group id in the Value.
- Click Save.
Our next task is to create a Process on the Account object to create a case if the accounting industry equal to Not for profit. To create a Process on the Contact object, follow the instructions below:
Step 2: Define Process Properties
- Click Setup.
- In the Quick Find box, type Process Builder.
- Select Process Builder, then click New.
- Name the Process and click the Tab button. The API Name will populate.
- As a best practice, always input a description.
- The process starts when A record changes.
- Click Save.
–> You can create a process that another process can invoke. With invocable processes, you can reuse sections of your processes. Build one invocable process, call it from multiple processes or from multiple action groups in the same process. You can invoke processes with objects that share at least one unique ID. For example, in the Account and Contact objects, the AccountId field is unique to Account and also used by Contact. You can create an invocable process that updates a Contact record. Then you can invoke it from:
- A process that updates an Account record’s billing address
- A process that adds an Account status
To create an invocable process, select It’s invoked by another process for The process starts when. For the current business scenario, we have selected A record changes, as shown in the preceding screenshot.
Step 3: Define Evaluation Criteria
- Click on the Add Object node to begin selecting the evaluation criteria.
- Select the Account object from the dropdown list.
- Start the process only when a record is created.
- Click Save.
Step 4: Define Process Criteria
- Click the Add Criteria node to begin defining the process criteria.
- Name the criteria.
- The criteria should execute actions when the conditions are met.
- Set Conditions
- Row 1
- Field: Account | Industry
- Operator: Equals
- Type: Picklist
- Value: Not For Picklist
- Row 1
- Select All of the conditions are met (AND).
- Click Save.
Step 5: Add Action – Create a Record
- Below Immediate Actions, click Add Action.
- For Action Type, select Create a Record.
- Name the action.
- Select the Case record type.
- Set Field Values:
- Row 1:
- Field: Case Currency
- Type: Picklist
- Value: U.S. Dollar
- Click Add Row
- Row 2:
- Field: Status
- Type: Picklist
- Value: New
- Click Add Row
- Row 3:
- Field: Description
- Type: String
- Value: Verify the organization’s Non-profit status
- Click Add Row
- Row 4:
- Field: Owner ID
- Type: Formula
- Value: $Label.Nonprofit_Experts_QueueID
- Click Add Row
- Row 5:
- Field: Account ID
- Type: Reference
- Value: Account | ID
- Row 1:
- Click Save.
The next step is to map the Owner ID field with a custom label. To do so select Formula for Type, as shown in the preceding screenshot. Then click on Build a Formula text and select System Variable dialog a shown in the following screenshot
It will open a popup where you have to select $Label and then select label Nonprofit Experts QueueID, as shown in the following screenshot
Once you’re done click on the Choose button. In the end, the formula for the Owner ID field should look like the following screenshot
Once you’re done click on the Use this Formula button. Add one more row to associate the new case with the account record, At the end Create a Record action should look like the following screenshot
In the end, Donna’s Process will look like the following screenshot:
Almost there! Once everything looks good, click the Activate button.
Proof of Concept
Now onwards if a business user creates an account with Industry Not For Profit, Process Builder will automatically create a new case. It’s time to test out the process.
- Navigate to the Account tab, and create a new account for Industry Not For Profit, as shown in the following screenshot:
- Once done, click on the Save button. It will open the account detail page and check case related list as shown in the following screenshot:
Formative Assessment:
I want to hear from you!
What is one thing you learned from this post? How do you envision applying this new knowledge in the real world?
Let me know by Tweeting me at @automationchamp, or find me on LinkedIn.
Hi Rakesh,
Small update you can add that custom metadata can now be used in Process Builder
Rakesh Hi,
As always, thank you very much for this useful information!
As I can understand it, since IDs might change in different environments, it’s a best practice to use Custom Labels, Custom Settings or Custom Metadata Types.
But, when using a Custom Label, for example, we DO add the ID (of a Queue, Record Type, etc.). So when I’ll create/deploy it in the Production Environment I might need to update it.
So what is the advantage of using the Custom Label?
Thank you in advance,
Gidi
Thank you so much for your feedback Gidi.
If you don’t do this, then you have to deactivate your process, clone it, then update it with correct id.
Thank you Rakesh for your response.
Gidi
Really awesome stuff. Yes, I too hate it when hard coded ids appear in apex or workflows.
Easy to understand. Thanks Rakesh 🙂
Clean one.
Thank you Mr. Carlos.