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.

  1. Create a text field on the Object.
    1. Go to object manager
    2. Click on subscriber object.
    3. Click on fields and relationship
    4. Click on New data type will be text
    5. Name the field “Subscriber unique ID”
    6. Select the Unique checkbox
  2. Create a Record trigger flow to populate this field
    1. Go to setup and search for Flows
    2. Click on flow and then create new.
    3. Select Record triggered flow
    4. Object = Subscriber
    5. Trigger the flow when a record is created
    6. Entry Condition = Null
    7. Optimize flow for = Fast field update
    8. Add decision Element after Start element
    9. Name the element “Check record Type”
    10. Name the first Outcome Partner
    11. Conditions should be {!$Record.RecordType.DeveloperName} = Partner
    12. We will create a new outcome for Customer
    13. For outcome= Partner we will create a get record element we will name it get partner
    14. Object will be a subscriber. Condition will be record type ID “partner record type ID”.How many records to store all records
    15. Add assignment element name the element = “Assign Partner count”
    16. Create a number variable and name the variable “Partner Count”
    17. Set the variable
      Partner Count > Equal Count > {!Get_partners}
    18. Select an update record element
    19. How to Find Records to Update and Set Their Values = Use the subscriber record that triggered the flow
    20. Condition None
    21. 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}),

      “”)))))

    22. We need to perform the same steps for customer outcome (Step M – Step U).
    23. Once completed, the flow will look like this.

Y. Activate the flow and create test records to test the flow.

— By Rishabh Dubey