Workaround for conditional auto number field in Salesforce
- March 07, 2022
On a custom object “Subscriber” we have two record types — customer and partner. We want an auto number field that will assign a unique ID to the subscriber record. For the customer, the ID will be C-0000 and for the partner the ID will be P-0000.
Salesforce provides a standard data type for the auto number field, but the auto number data type doesn’t allow us to add conditions.
There’s more information on the issue on the Salesforce Trailblazer Community :
https://trailhead.salesforce.com/trailblazer-community/feed/0D54S00000A936tSAB
This can be done using record Triggered Flow.
- Create a text field on the Object.
- Go to object manager
- Click on subscriber object.
- Click on fields and relationship
- Click on New data type will be text
- Name the field “Subscriber unique ID”
- Select the Unique checkbox
- Create a Record trigger flow to populate this field
- Go to setup and search for Flows
- Click on flow and then create new.
- Select Record triggered flow
- Object = Subscriber
- Trigger the flow when a record is created
- Entry Condition = Null
- Optimize flow for = Fast field update
- Add decision Element after Start element
- Name the element “Check record Type”
- Name the first Outcome Partner
- Conditions should be {!$Record.RecordType.DeveloperName} = Partner
- We will create a new outcome for Customer
- For outcome= Partner we will create a get record element we will name it get partner
- Object will be a subscriber. Condition will be record type ID “partner record type ID”.How many records to store all records
- Add assignment element name the element = “Assign Partner count”
- Create a number variable and name the variable “Partner Count”
- Set the variable
Partner Count > Equal Count > {!Get_partners} - Select an update record element
- How to Find Records to Update and Set Their Values = Use the subscriber record that triggered the flow
- Condition None
- Subscriber_unique_ID__c = Create a new resource to update the field
Resource Type = Formula
Name = Partner ID
Datatype = text
Syntax=
IF({!PartnerCount}=0,”P-00001″,
IF(LEN(TEXT({!PartnerCount}))=1,”P-000″+ TEXT({!PartnerCount}),
IF(LEN(TEXT({!PartnerCount}))=2,”P-00″+ TEXT({!PartnerCount}),
IF(LEN(TEXT({!PartnerCount}))=3,”P-0″+ TEXT({!PartnerCount}),
IF(LEN(TEXT({!PartnerCount}))=4,”P-“+ TEXT({!PartnerCount}),
“”)))))
- We need to perform the same steps for customer outcome (Step M – Step U).
- Once completed, the flow will look like this.
Y. Activate the flow and create test records to test the flow.
— By Rishabh Dubey