How to Create a Conditional Auto Number

Every once and a while there is a situation where we need to have an Auto Number generated, but only for records that meet a certain criteria.  Lets say that we use the Account Number field as our client’s identifier, and we want to automate this process of incrementing it one every time we have a new client.  There are a few different ways that we can solve this, but we’re going to use Visual Flow.

Automation Overview:

  1. Account Meets Criteria – Process Builder triggered (Pass inputs into Visual Flow)
  2. Custom Object Record with Auto Number created in Flow
  3. Lookup value of Auto Number
  4. Update the Account

For this solution we have three different options in how we can do it architecturally (with our Auto Number):

  1. Custom Object
  2. Custom Metadata
  3. Custom Settings

For me, I am going to go with using a Custom Object.  I think that it is the easiest option of the three to implement, and most Salesforce Admins can relate to Custom Objects more than the other two options.  So, what fields do we need create in this Custom Object?  None!  We just need to make sure we set the Name to be an Auto Number.

AutoNumberObject.jpg

HINT: If you ever need to reset the number, you can switch the Name field to Text and then back to an Auto Number and it will let you pick the Starting Number again.

Here is what our finalized Object looks like:

AutoNumberCustomObject.jpg

Now that we have our Object ready, we can go create our Flow (Setup | Create | Workflows & Approvals | Flows).

NewFlow.jpg

The first step is going to be grabbing our Record Create.

RecordCreate1.jpg

We want to select the Client Number Object that we just created, and then remove the row asking us to set a field value.

RemoveField.jpg

Now, we want to create a variable to house the Record Id.

var_ClientNumberId.jpg

CreateClientNumber

Hit OK, and then we want to set this as our Starting Element.

StartingElementCreateClientNumber.jpg

Now, we want to grab a Record Lookup and find the Name (Auto Number) of the record we just created.

AddRecordLookup.jpg

We are going to need to create a variable to store the Auto Number value that we will then Update our Account with.

var_ClientNumber.jpg

FindAutoNumber

Hit OK.  And now drag out the Record Update element.

RecordUpdateClientNumber.jpg

We will be passing into our Flow the Account Id value, and we will want to use that here to filter on our Record Update.  So we need to create the variable to store that.

var_AccountId.jpg

Map the Client Number to the Account Number field, and hit OK.

UpdateAccountAccountNumber.jpg

Connect the elements together to finish our Flow.

CompletedClientNumberFlow.jpg

And lastly we want to Save and then Activate our Flow!

CreateClientNumberFlowSave

ActivateClientNumberFLow.jpg

Fantastic!  Now we need to setup our Process Builder that fires this Flow.  So lets go create a new Process Builder for this.  (Setup | Create | Workflows & Approvals | Process Builder)

CreateClientNumberPB.jpg

We want the Process Builder to fire on our Account, so for our object select Account, and for starting the process select when a record is created or edited.

PB1.jpg

This next step is completely on you for when the record meets the criteria.  For my example we will just use when the Type is now a Customer.  Careful with the Advanced section at the bottom, you aren’t going to want your Process Builder to fire more than once per Account!

NewCustomerCriteria.jpg

Now we get to setup our Immediate Action of launching a Flow.  All we need to do is pass in our Account Id.

PBLaunchFlow.jpg

Hit Save, and then hit Activate up in the top right corner… and you’re done!

 

 

 

14 thoughts on “How to Create a Conditional Auto Number

  1. Doug Ayers March 28, 2016 / 11:33 am

    Hi David,

    Interesting use of custom object auto-number! Question, was the business requirement that the auto-numbers that are assigned are in as sequential order as possible or just that a unique value is auto assigned?

    If the former then your solution is necessary to control when a dummy record is inserted into the custom object to generate a new numbe.

    If the latter, this may be achieved by adding the auto number field to your main object then using process builder to copy it as needed into the account number field. Of course, as client records are created that already specify their own account number then a bunch of auto number values get generated but not used. But that may be ok depending on the requirement.

    Thanks,

    Doug

    Like

    • David Litton March 28, 2016 / 3:12 pm

      Thanks Doug! Yes, the requirement would be to have it conditionally assigned in a sequential order. As I was writing this I technically even found a way to solve this without using Flow and just using two Process Builders, but the purpose was to show how to build it with Flow. Like you mentioned, there are quite a few different ways to go around solving this depending on the requirements! And, if the latter is an option with the requirements, then it would be the best solution in terms of using the least amount of development.

      Liked by 1 person

      • Joey Len (@joeysayz) May 12, 2016 / 8:43 pm

        David,
        Love the architecture but you mentioned using 2 Process Builders to achieve this goal. Mind sharing? I am building a configurator/quoting tool for our sales team and need to have a unique identifier for each quote that is submitted.
        Thanks in advance,
        Joey

        Like

      • David Litton May 13, 2016 / 10:03 pm

        Joey,

        Of course, I might throw in a second post to go into that in more detail, but from a high overview:

        First thing is we need to add an Account Lookup to our Custom Object. Next, we have Process Builder triggering in the same criteria we have now, but we do a Record Create for the new record in that Process Builder. Make sure you map the Account Id (or record ID) to that Lookup we created.

        Now for your second Process Builder, simply create it to trigger on Created, and then have it update the Account with the Autonumber. You’ll be using the Update Record feature with the ‘related to this record’ instead of ‘this record’.

        Does that make sense?

        Thanks,
        David

        Like

  2. karen June 20, 2016 / 6:49 pm

    hi david,
    did you ever further document the process using 2 process builders? i need to create conditional sequential autonumbers, but haven’t used flows. i have used process builder, and think i understand your second process builder, but could use a bit more of an explanation about how the first one is configured.. for starters, i’m not clear if the first one is built on the custom object or the account.. sorry for being dense and thanks for taking the time to find solutions to hard problems.
    karen

    Like

      • Paul Fennell August 30, 2016 / 10:43 pm

        Hi David,

        In this next post it appears you left off the Lookup field on the custom object. Secondly, in the second process builder I think you inadvertently stated that this would be formed on the Account object, when it appears this should trigger on the creation of the custom object.

        Could you confirm? I’ve been driving myself nuts trying to get this done to no avail.

        Like

      • David Litton August 30, 2016 / 10:50 pm

        Hey Paul,

        Good catch! I missed that. The Custom Object is where the second process builder needs to be, and the screen shot shows that – but my wording was incorrect. I’ve updated the wording so it hopefully is more clear now. I believe this was meant for the other post for the Process Builder only approach though?

        Let me know if you’ve got any questions or want to jump on a quick screen share to solve the issue.

        Thanks!

        Like

  3. Tim Wiech January 6, 2017 / 12:29 am

    Hi David. This fit my use case perfectly as I need to assign a different autonumber based on account type, client type, and status… But I’m running into a flow error. I followed the instructions exactly, the only thing changed are the names, but when the Process triggers the flow i get this error:
    An error occurred at element Create_BR_Autonumber (FlowRecordCreate).
    INSERT — INSERT FAILED — ERRORS : (INVALID_ID_FIELD) Record ID: id value of incorrect type:
    ________________________________________
    This report lists the elements that the flow interview executed. The report is a beta feature.
    We welcome your feedback on IdeaExchange.
    Flow Details
    Flow Name: Account_BR_Autonumber_Assignment
    Type: Autolaunched Flow
    Version: 3
    Status: Active
    Flow Interview Details
    Interview Label: Account BR Autonumber Assignment 1/5/2017 6:13 PM
    Current User: Tim Wiech (00541000001jB0V)
    Start time: 1/5/2017 6:13 PM
    Duration: 0 seconds
    How the Interview Started
    Tim Wiech (00541000001jB0V) started the flow interview.
    Some of this flow’s variables were set when the interview started.
    var_AccountId = 0014100000E0GpyAAF
    RECORD CREATE: Create_BR_Autonumber
    Create one BR_AutoNumber__c record.
    Result
    Failed to create record.
    ________________________________________
    Error Occurred: INSERT — INSERT FAILED — ERRORS : (INVALID_ID_FIELD) Record ID: id value of incorrect type:
    ________________________________________

    Any idea why it would be failing to create the basic record? I did not add any fields to the autonumber object. Also, I am the system admin on a fresh SF org created after winter 17 release. I am also set as a flow user.

    Like

    • Tim Wiech January 6, 2017 / 1:01 am

      Nevermind, the 2 step Process Builder method delivered the desired results without an error

      Like

  4. Sourav April 5, 2017 / 8:56 am

    Hi David

    I have an exact need, and I have to update a field ” Tax Invoice S2″ in Quote Line Item object, if certain criteria are fulfilled. I did exactly same. I created a new custom object, then the autonumber field in that and map with the above field in QLI object. The PB I created on QLI, with the desired criterias. But while running its showing me below error. Can you plz suggest. Thanks
    I couldn’t got where I go wrong.

    Error element myRule_1_A1 (FlowActionCall).
    An error occurred when executing a flow interview.

    This report lists the elements that the flow interview executed. The report is a beta feature.
    We welcome your feedback on IdeaExchange.
    Flow Details
    Flow Name: Create_Tax_Invoice_QLI
    Type: Workflow
    Version: 1
    Status: Active
    Flow Interview Details
    Interview Label: Create_Tax_Invoice_QLI-1_InterviewLabel
    Current User: Sourav Patel (005N0000002fVbd)
    Start time: 4/5/2017 3:47 PM
    Duration: 0 seconds
    How the Interview Started
    Sourav Patel (005N0000002fVbd) started the flow interview.
    Some of this flow’s variables were set when the interview started.
    myVariable_old = 0QLN0000000IhwGOAS
    myVariable_current = 0QLN0000000IhwGOAS
    ASSIGNMENT: myVariable_waitStartTimeAssignment
    {!myVariable_waitStartTimeVariable} Equals {!Flow.CurrentDateTime}
    Result
    {!myVariable_waitStartTimeVariable} = “4/5/2017 3:47 PM”
    DECISION: myDecision
    Executed this outcome: myRule_1
    Outcome conditions: and
    1. {!myVariable_current.Quote.ContractId} (800N0000000p6ruIAA) Does not equal null
    2. {!myVariable_current.Quote.Contract.Status} (Activated) Equals Activated
    3. {!myVariable_current.Trans_Gross_premium__c} (3,195.549) Greater than 0
    4. {!myVariable_current.Product2.Name} (Voluntary) Equals Voluntary
    Logic: All conditions must be true (AND)
    DECISION: myRule_1_pmetdec
    CREATE_TAX_INVOICE_NUMBER_FLOW (FLOWS): myRule_1_A1
    Inputs:
    var_QLIid = {!myVariable_current.Id} (0QLN0000000IhwGOAS)

    Error Occurred: An error occurred when executing a flow interview.

    Like

  5. Sourav April 5, 2017 / 9:08 am

    Just try to run again, and got this error, similar to above posted by Tim, Can you suggest plz. Thnx

    Error element Create_Invoice_Number (FlowRecordCreate).
    This error occurred when the flow tried to create records: INVALID_ID_FIELD: Record ID: id value of incorrect type: . For details, see API Exceptions.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s