Last Updated on April 25, 2023 by Rakesh Gupta
Big Idea or Enduring Question:
- How can you avoid hard coding of ID in Salesforce Flow?
Objectives:
This blog post will help us to understand the following
- Understand how to use custom Labels to avoid hard code of ID in the Flow
- Understand how to use Custom Settings to avoid hard code of ID in the Flow
- Understand how to use Custom Metadata Types to avoid hard code of ID in the Flow
Business Use Case
Donna Serdula is working as a System administrator at Gurukul On Cloud (GoC). 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 Salesforce Flow 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 or Flows 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 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 (Nonprofit Experts) has a different ID in a sandbox and a production org.
Now you can refer the Nonprofit Experts queue id while creating a case through Salesforce Flow.
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.
So What Next? How to Avoid Hard coding of IDs in Flows?
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, 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. 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 going through the following Flow diagram and understanding it. Let’s begin building this automation process.
Guided Practice (We-do):
There are 2 steps to solve Donna’s business requirement using After-Save Record-Triggered Flow. We must:
- Create a Nonprofit Experts Queue
- Salesforce Flow Steps:
- Define Flow properties
- Formula to determine whether the record is created or updated
- Add a decision element to check account industry
- Add a get record element to find queue Id
- Add a decision element to check the queue found or not
- Add a create records element to create a new case
Step 1: 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.
Step 2.1: Define Flow Properties
- Click Setup.
- In the Quick Find box, type Flows.
- Select Flows, then click on the New Flow.
- Select the Record-Triggered Flow option, click on Create and configure the flow as follows:
- Object: Account
- Trigger the Flow When: A record is created or updated
- Set Entry Criteria
- Condition Requirements: None
- Optimize the Flow For Action and Related Records
- Click Done.
Step 2.2: Formula to Determine Whether the User is Created or Updated
- Under Toolbox, select Manager, then click New Resource to determine whether the record is new or old.
- Input the following information:
- Resource Type: Formula
- API Name: forB_IsNew
- Data Type: Boolean
- Formula: IsNew()
- Click Done.
Step 2.3: Add a Decision Element to Check the Account Industry
We will use the Decision element to check whether the account industry is Not For Profit.
- On Flow Designer, click on the +icon and select the Decision element.
- Enter a name in the Label field; the API Name will auto-populate.
- Under Outcome Details, enter the Label the API Name will auto-populate.
- Condition Requirements to Execute Outcome: All Conditions Are Met (AND)
- Row 1:
- Resource: {!$Record.Industry}
- Operator:Equals
- Value:Not For Profit
- Click Add Assignment
- Row 2:
- Resource: {!forB_IsNew}
- Operator: Equals
- Value: {!$GlobalConstant.True}
- Row 1:
- When to Execute Outcome: If the condition requirements are met.
- Click Done.
Step 2.4: Add a Get Record Element to Find Queue Id For Nonprofit Experts
The next step is to find the Nonprofit Experts Queue.
- On Flow Designer, below the Not For profit node, click on the +icon and select the Get Records element.
- Enter a name in the Label field; the API Name will auto-populate.
- Select the Record Group object from the dropdown list.
- Select All Conditions Are Met (AND).
- Set Filter Conditions
- Row 1:
- Field: DeveloperName
- Operator: Equals
- Value: Nonprofit_Experts
- Field: DeveloperName
- Click Add Condition
- Row 2:
- Field: Type
- Operator: Equals
- Value: Queue
- Row 1:
- How Many Records to Store:
- Select Only the first record
- How to Store Record Data:
- Choose the option to Automatically store all fields.
- Click Done.
Step 2.5: Add a Decision Element to Check the Queue from the Record Variable
Now we will use the Decision element to check the Record Variable from step 2.4 to find if it returns the queue or not.
- On Flow Designer, click on the +icon and select the Decision element.
- Enter a name in the Label field; the API Name will auto-populate.
- Under Outcome Details, enter the Label the API Name will auto-populate.
- Condition Requirements to Execute Outcome: All Conditions Are Met (AND)
- Row 1:
- Resource: {!Get_Queue}
- Operator: Is Null
- Value: {!$GlobalConstant.False}
- Click Done.
- Row 1:
Step 2.6: Create Records – Create a Case and Assign it to Nonprofit Queue
To create a case and assign it to Nonprofit Experts queue, we will use Create Records element.
- On Flow Designer, click on the +icon and select the Create Records element.
- Enter a name in the Label field; the API Name will auto-populate.
- Input the following information:
- How Many Records to Create: One
- How to Set the Record Fields: Use separate resources, and literal values
- Object: Case
- Set Field Values for the Case
- Row 1:
- Field: Status
- Value: New
- Click Add Field
- Row 2:
- Field: OwnerId
- Value: {!Get_Queue.Id}
- Click Add Field
- Row 3:
- Field: CurrencyIsoCode
- Value: USD
- Click Add Field
- Row 4:
- Field: Description
- Value: Verify Organization’s Nonprofit Status
- Click Add Field
- Row 5:
- Field: AccountId
- Value: {!$Record.Id}
- Row 1:
- Click Save.
In the end, Donna’s Flow will look like the following screenshot:
Once everything looks good, perform the steps below:
- Click Save.
- Enter the Flow Label the API Name will auto-populate.
- Click Show Advanced.
- API Version for Running the Flow: 56
- Interview Label: Don’t Hard Code Id {!$Flow.CurrentDateTime}
- Click Save.
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, the record-triggered flow 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? Feel free to share in the comments below.
Thank you for the article. I see instances of hardcoding as well and it drives me nuts. One suggestion is that we always use DeveloperName to query for the queue or group as these are always unique (Nonprofit_Experts) while Name is not required to be unique.
We don’t add a test for the existence of the queue as it must exist for our business processes. UAT will tell you if you forgot to deploy/create it or not. In your flow, you test for it and if it does not exist, do nothing. I would not recommend this. How do you know if something is wrong if your processes detect something missing but tells no one?
What is valuable for the business I think is to test for membership to the queue. Is there at least one user where IsActive = TRUE? If not, instead of doing nothing, the negative branch would email the admin stating there is no one in this Queue.
Thank you again for all you do. Automation Champion is a world class SF resource! Gerald Demers
That’s right, friend 🙂. I’ve updated the blog post to use Developer Name. Thank you 🙏!!! Happy Thanksgiving!!
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.