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

How to use AutoNumber before Insert and Update

Hi, I need to know if is possible to use a AutoNumber field before an update/insert.

Let me explain:


Trigger -->  Class


Inside my class, I need to use the field MyNumber (AutoNumber ) for some calculus and write inside other field.


When I try to use it the result is always null :




Could someone help me?


Thank you in advance,


Jake GmerekJake Gmerek

My first thought is to check and possibly change your trigger to (after insert, after update).  I do not believe that the autonumbers are populated before the record is inserted/updated.



Thank you for your quick replay.


I tried to use after insert and update. The problem is that I use Trigger.New and I have an exception

System.FinalException: Record is read-only.


Have you got some idea?



Ankit AroraAnkit Arora

You can get the AutoNumber in After Insert and Update event. But I think your error "Record is read-only" is not related to this.


Are you updating the same record in trigger? Please provide code of your trigger so we can assist you more.




Ankit Arora

Blog | Facebook | Blog Page


trigger BarcodeFaturaTrigger on Fatura__c (after insert, after update) {
    Fatura__c[ ] fa;

    fa =;




public with sharing class BarcodeFatura {
public static void Calc_FaturaCod(Fatura__c[] fs){
    for (Fatura__c a:fs){       
           a.CalcNum =  a.AutoNum+1;





In this case I'll have Record is read-only because is readonly.

I would like to know how I manage to access  "a.AutoNum" .

I'm new to use apex. Maybe I need to use Trigger.newMap.

 I'm studying but it's hard to find documentation and examples.


Thank you.

Ankit AroraAnkit Arora

I got your problem.


You can not get autonumber in before insert and you can not put the value in any field on after insert event, it will give you the same error. But I hope you can do this in before update event.


Also in before insert event you can put AutoNumber + 1 in field but it will give you like this "null1".




Ankit Arora

Blog | Facebook | Blog Page


You managed to describe my problem. :)

Would you do to resolve it?


Think I kinda figured this out.  I have an Opportunity Number that is an AutoNumber field.  The users want that number in the Opportunity Name as well.  No problem.  BUT when the Opportunity goes to Stage 7: Booked, using a trigger I automatically clone the Opportunity (and its products). So for example, if the original OppName was MyOpp 12345, the clone should be MyOpp 12346 -- Renewal.  But by cloning, the new name was MyOpp 12345 -- Renewal.


I got around this in 2 steps:

in the trigger, after I clone the Opportunity, I put in these three statements:

            clOp.Type = 'Renewal';
            clOp.Name = clOp.Name + ' -- '+clOp.Type;
            clOp.Name = clOp.Name.replace(clOp.Opportunity_Number__c.trim(),'originalNum'); 


Then I built a new workflow rule: Update OppName for Renewal with the filter

CONTAINS(Name, ' -- Renewal')


this fires a field update with the formula

SUBSTITUTE(Name, 'originalNum', OpportunityNumberCalc__c )


where OpportunityNumberCalc__c is the autonum field.


Hope this helps,




Use a before insert, before update trigger, then change your class to check for null.


public with sharing class BarcodeFatura {
public static void Calc_FaturaCod(Fatura__c[] fs){
    for (Fatura__c a:fs){     
           a.CalcNum =  (a.AutoNum == null)? 1 : a.AutoNum+1;