function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
bhaubhau 

Conditional Autonumber based on Record Type

Hello all,

 

I checked out the forums and I can't seem to find a solution to this. On person accounts (in Enterprise Edition), I have an auto-number field but I only want increment for my "customers" record type and not my "employee" record type. Right now it is auto-numbering for both even though I want it to increment when it's the customer record type.  Can someone help me with this/point me into the right direction or have some custom code that can do this?

 

Thanks much!

-Billy

chriseustacechriseustace

You could write a trigger that would update a field based upon the type of record it is.  You could also have 2 seperate fields then sum them together in order to get an overall count....

bhaubhau

On average, how long would it take to write this trigger? 

 

Thanks!

Jeremy.NottinghJeremy.Nottingh

Without involving Triggers, what do you think of this: have an autonumber field that will give everybody an unique number. Call it Autonumber__c.  Then have a second formula text field, called Customer_Number__c. Here's the formula for this field:

 

IF(

  $RecordType.Name = "Customer",

  Autonumber__c,

  null)

 

Then the field will be blank unless it's a customer, in which case you will see the autonumber. This is a lot easier than writing a Trigger. Let me know if you don't think that'll work.

 

Jeremy

Dan Sherrard-SmithDan Sherrard-Smith
Hi Jeremy,

Did this solution work for you? I'd be interested in using it if so.

Many thanks,

Dan
Tom FaxonTom Faxon
I'm struggling with a very similar dillema.

I see what Jeremy is getting at here; the customer number would only be populated on customers, but I expect it would not increment properly, as the first autonumber field would continue to increment on employees, as well?
Jennifer ClarkJennifer Clark
Was anyone able to figure out a solution to this? I've been looking for a solution for some time now, the auto-number field is for our Quote object but same idea as the question above. (Note: we have the Professional Edition) 
Josiah PloegerJosiah Ploeger
Showing up late to the party here but I was looking for a solution to this and found a great one written by David Litton here: 
https://salesforcesidekick.com/2016/03/28/how-to-create-a-conditional-auto-number/

Hope this helps those who come looking in the future.
Lisa HolmesLisa Holmes
Hello, I would be interested in using the trigger to autonumber rather than creating a custom object. Would someone be able to provide the code for this?
Josiah PloegerJosiah Ploeger
Hi Lisa, there really isn't a great way to do it without a custom object. Initially I had implemented a solution that queried the highest value that currently existed on the object in question, and then incremented that by one as the starting point. The problem you run into with that solution is if  2 people are creating accounts or whatever at the exact same time (which happens more than you think), each apex transaction is not aware of the other one and you end up with duplicate numbers. Having an auto number field solves that, but if you only want to have it auto number sometimes you need a separate object to track that incrementing. Here's an apex solution that still uses a custom object:
 
trigger Account_Triggers on Account (before insert) {
    if (Trigger.isBefore and Trigger.isInsert){
        List<Account> accountsToAddNumTo = new List<Account>();
        List<Customer_Account_Number__c> accountNums = new List<Customer_Account_Number__c>();

        String timestamp = date.today().format('**yyyy-MM-dd HH:mm:ss**');

        for (Account a : Trigger.new){
            if (a.RecordType.Name = 'Customer'){
                accountsToAddNumTo.add(a);
                Customer_Account_Number__c num = new Customer_Account_Number__c(Transaction_Timestamp__c = timestamp);
                accountNums.add(num);
            }
        }
        if (accountNums.size() > 0){
            insert accountNums;
            List<Id> idList = new List<Id>();
            for (Customer_Account_Number__c an : accountNums){
                idList.add(an.Id);
            } 
            accountNums = [SELECT Id, Name FROM Customer_Account_Number__c WHERE Id IN :idList];

            Integer counter = 0;
            for (Account a: accountsToAddNumTo){
                a.Customer_Account_Number__c = accountNums[counter].Name;
                counter += 1;
            }
        }
    }
}