Welcome to another “Automate This!” In this live-streamed video series, we cover all things automation, from use cases and best practices to showcasing solutions built by Awesome Admin Trailblazers like you. With automation, you can remove manual tasks, drive efficiency, and eliminate friction and redundancy. In this episode, see how Warren Walters creates custom Auto Number logic using custom settings and flows. By combining these declarative tools, you can create automation like dynamic round robins and custom record numbering based on record types.
Use case: Conditional auto number and dynamic round robin
We want to create a round robin based on specific field values on the lead record. Instead of doing an assignment for every lead, we only want to do an assignment when a specific lead source is set. For example, when the lead source is set to the web, then a web Auto Number field will be incremented. We can run our round robin lead assignments based on this new field. Implementing round robin this way will allow for more flexibility in your assignment process based on record conditions.
Scenario: Lead round robin
Let’s say your company uses round robin lead assignment but wants specific leads to be sent to specific sales reps who are more familiar with the lead sales process. To do this, you can update the round robin lead assignment to be based on criteria in the lead source and assign the leads to the predefined groups.
Example:
Lead Source = Web → Round robin reps (Rep 2, Rep 4, Rep 5)
All other lead sources with round robin all 5 Reps
Solution: Update custom settings in a flow
What are custom settings?
Custom settings are a setup object that’s used to store custom data. They work similarly to custom objects but are primarily used by admins and developers to hold organization-wide data. You can add fields and data sets (similar to records) that are accessible in automation. Custom settings can be manipulated by a wide range of Salesforce tools and automation, such as formula fields, validation rules, flows, Apex, and SOAP API. Custom setting records are only accessible within Setup, unlike standard and custom objects.
How can custom settings help with conditional Auto Numbers?
We will use a custom setting to hold an org-wide variable of the Auto Number count. Since it’s a custom setting, not a custom Auto Number field, we can control when the counter is incremented. We will then use Flow to increment the counter when specific conditions are met.
Custom settings setup
We need a way to hold the custom Auto Number value controlled by the custom setting for every lead created. Similar to the standard way of creating round robin in Salesforce, we will use custom fields.
Create a custom number field with a descriptive name and description so we know what this field was used for if we review it in the future.
You can make this value unique so that they cannot have duplicates. Note: By making this a unique value, this may cause automation errors, but it will keep your data clean.
Round Robin formula field
Create a Round Robin field that tells the system to assign each new lead to the next user in order. To do this, we need a formula field that rotates the values based on the number of users in our round robin.
Create a formula field number with a descriptive name and description.
Example: If you have three users in your round robin, your formula would look like:
MOD(Lead_Source_Web_Number__c,3)+1
When the Lead_Source_Web_Number__c = 6 then round robin field = 1
When the Lead_Source_Web_Number__c = 7 then round robin field = 2
When the Lead_Source_Web_Number__c = 8 then round robin field = 3
When the Lead_Source_Web_Number__c = 9 then round robin field = 1 - back to the start
Set the Blank Field Handling property to ‘Treat blank fields as blank’ so that leads that don’t meet the round robin criteria don’t trigger the assignment rules.
Once our automation is set up, this field will be used in the lead assignment rules.
Custom setting
Create the custom setting that will hold the Auto Number count. This value will be incremented by the round robin flow.
Set up the Custom Setting definition in Setup > Custom Settings.
Create a number field on the custom setting that will hold the Auto Number count. The default value should be 1 so that the incrementation has a starting point. The starting value can be changed later if needed.
To initialize the custom setting data, click Manage, then New. Enter the starting number for the Auto Number.
You can always manually update the Auto Number value here if needed from data loads or custom incrementation.
Use lead assignment rules to assign leads to reps to leads
Create lead assignment rules based on the Auto Number incrementation and the round robin field. In this example, we have the custom and standard round robin in the same lead assignment rules.
Use Flow to increment the custom setting and lead value
To achieve this, we will need two record-triggered flows. A before-save flow is used to update the Lead Source Web Number, and an after-save flow is used to update the custom setting. Both flows are needed because of Salesforce’s order of operations. In order for the lead assignment rules to pick up the Lead Source Web Number, it needs to be updated before the data is committed to the database. The after-save flow is used to update the custom setting once the lead update is complete.
Before-save flow
Create a before-save record-triggered flow that (1) queries the custom setting, (2) checks if the Lead Source is Web, (3) increments the custom setting Auto Number web field by one, and (4) sets the values of the Lead Source Web Number field.
This flow is only triggered when leads are created, and conditions are added, so it only fires when there is a value in the lead source.
Element 1: Before we start to increment or make decisions, let’s grab the custom setting that holds the web Auto Number value with a Get Records element.
Element 2: We’re going to use a Decision element to check if the lead source is Web. Additional lead sources or decision nodes could be added here if we were incrementing based on additional criteria.
Element 3: In the Web decision path, we use an Assignment element to increment the custom setting Auto Number web field by one using the add operator.
Element 4: Then, we use another Assignment element to set the lead's Web Number field to the custom setting Auto Number field, which was previously incremented in the step above. The lead record that triggered the flow is indicated by $Record global variable.
Since this is a before-save flow, we do not need to do an update operation on the lead record. Setting the value during the assignment effectively does the same thing.
After-save flow
Create an after-save record-triggered flow. Our automation will update records in addition to the one that triggered the process. Now, we can create a record-triggered flow that will increment the custom setting and update the lead value.
This flow is only triggered when leads are created, and conditions are added, so it only fires when there is a value in the lead source.
Repeat elements 1, 2, and 3 from the before-save flow since they are exactly the same. We need this logic to be the same so that the Auto Number updates on the custom setting will be in sync. There is potential for optimization here, where we can use a sub-flow to use the same logic.
Next, we use an Update Records element to save the custom setting record with the changed Auto Number value.
Of course, since we’ve made changes, we need to save and activate the flow.
Let’s test our flow
To test this flow, we can either use flow debug or create leads directly in the sandbox. Using the flow debug feature to test will only show the incrementation and updating of the custom setting, but not the actual lead assignment.
To perform a complete end-to-end test, we will need to test by creating a new lead in the sandbox. When testing in the org, check the ‘Assign using active assignment rule’ box to ensure your assignment rules have run.
Upon save, the Lead Source Web Number field should only be updated when the value in the Lead Source is equal to Web. Then, the Lead Source Web Round Robin field will display the appropriate number for the lead assignment rules.
Let’s recap
We were able to create a custom Auto Number field based on criteria the business decided on, and that allowed for a dynamic round robin lead assignment. This technique of custom Auto Numbers can be applied in many different ways. For example, you can create custom invoice numbers depending on record types.
One question that may arise is, “Why aren’t we using custom metadata types?” As of right now, custom metadata types cannot be updated by a flow. If updating via Flow gets added to custom metadata types in the future, then it would be the preferred setup tool to use in this situation. There’s an idea on the IdeaExchange to update custom metadata types in Flow— please vote on it. You can find all the metadata from this demo in this GitHub repository.
Custom settings considerations
Custom settings are a type of custom object. Each custom setting counts against the total number of custom objects available for your organization.
Only custom settings definitions are included in packages, not data. This means the Auto Number value does not carry over during deployments.
Welcome to another “Automate This!” In this live-streamed video series, we cover all things automation, from use cases and best practices to showcasing solutions built by Awesome Admin Trailblazers like you. With automation, you can remove manual tasks, drive efficiency, and eliminate friction and redundancy. In this episode, let’s see how Bradley Condon built the […]
Welcome to another “Automate This!” In this live-streamed video series, we cover all things automation, from use cases and best practices to showcasing solutions built by Awesome Admin Trailblazers like you. With automation, you can remove manual tasks, drive efficiency, and eliminate friction and redundancy. In this episode, let’s see how Ohad Idan vanquishes shared […]
Welcome to another “Automate This!” In this live-streamed video series, we cover all things automation, from use cases and best practices to showcasing solutions built by Awesome Admin Trailblazers like you. With automation, you can remove manual tasks, drive efficiency, and eliminate friction and redundancy. In this episode, learn how Becka Dente leverages a simple […]