If we have a custom object with Name as AutoNumber and 3 record types, say 'A', 'B' and 'C', is it possible to set Name field's value based on its record type? For example, if I try to create a record with 'A' record type then is it possible to set its Name field's value to something like A-001 and if a record is created with 'B' record type set the Name field's value to B-001 etc.? I understand that Name autonumber field is not writeable but trying to see if there is any way to achieve this.


2 Answers 2


I don't think its directly possible. But one ope approach can be: Creating a new custom setting for that object and create fields(one for each record type of that object) Where we will store the value of the Last number lets say(0001). Same for other record types.

Create a new Text field on the object(Make it read-only for all the users) and in before insert trigger depending on the record type of your object pull the value from the custom setting and add 1 do it.eg (A-{Fected no. from custom setting +1}). and store the new added value back to the custom setting

In case of Bulk insertion. modify the before trigger to add 1 for all the records in the new records for loop and at the end update the custom setting with the last number.


You need to create a little table that keeps the list of Autonumbers. Basically, any query-based approach will have issues when records are greater than 50000 or two people are creating records at the same time.

Here is the solution Create a table ('ShipmentDetailNumber__c') with an AutoNumber Field with the format that you would like, except the 'A-' part. I created a Lookup field on the Main SObject ('ShipmentDetail__c'), mainly for debugging and I like to be tidy.

Create a trigger on the ShipmentDetail__c that is like the following (Note, I use the 'SQ-' and 'SN-' that I needed).

What you are going to do is create a ShipmentDetailNumber__c for each ShipmentDetail__c. This will 'reserve' the AutoNumber, in case someone else is creating records at the same time. After querying the records in ShipmentDetailNumber__c to get the AutoNumber, you create your desired AutoNumber on ShipmentDetail__c!

Here is the Code:

trigger ShipmentDetail on ShipmentDetail__c(before insert) {
        //if there is nothing in Trigger New, there is nothing to do!
        if (Trigger.New.isEmpty()) {
        //reserve AutoNumbers for each new record
        List<ShipmentDetailNumber__c> autoNumbers = new List<ShipmentDetailNumber__c>();
        for (ShipmentDetail__c record : Trigger.New) {
            if (String.isNotBlank(record.Name)) {
            autoNumbers.add(new ShipmentDetailNumber__c());
        insert autoNumbers;
        //Get AutoNumbers
        Set<Id> autoNumberIds = new Set<Id>();
        for (SObject autoNumberRecord : autoNumbers) {
        autoNumbers = [
            SELECT Name
            FROM ShipmentDetailNumber__c
            WHERE Id IN :autoNumberIds
        //Create names and link to ShipmentDetailNumber__C
        Integer n = 0;
        for (ShipmentDetail__c record : Trigger.New) {
            if (String.isNotBlank(record.Name)) {

        Id bookingRecordType = Schema.SObjectType.ShipmentDetail__c.getRecordTypeInfosByName()

            //Your record types and prefixes goes here
            if (record.RecordTypeId == bookingRecordType) {
                record.Name = 'SB-' + autoNumbers[n].Name;
                record.Auto_Number_Record__c = autoNumbers[n].Id;
            } else {
                record.Name = 'SQ-' + autoNumbers[n].Name;
                record.Auto_Number_Record__c = autoNumbers[n].Id;

Note: some credit due to https://developer.salesforce.com/forums/?id=906F00000008utJIAQ, though I have refactored the code

  • 1
    This is an old question, but I haven't found any good answers to it and decided to document what I did here
    – dbwood3
    Dec 14, 2021 at 17:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.