+ Start a Discussion
Olavo ZapataOlavo Zapata 

Trigger for autonumber just in specific cases

Hi all,

I'm trying to create a trigger to generate an autonumber that will only count in specific use cases on Customer Object.
For example, when the client has Active Status and has a project end date.

However, with the code I wrote this giving me an error when I'm trying to update or insert any information to the Customer.
Trigger.
trigger AutonumberOnlyISOnb on JBCXM__CustomerInfo__c (after insert, after update) {
    list<JBCXM__CustomerInfo__c> l= [SELECT Id,Name,IS_Autonumber__c FROM JBCXM__CustomerInfo__c where IS_Autonumber__c !=:null order by IS_Autonumber__c desc limit 1];
    decimal maxacc = l[0].IS_Autonumber__c;
    
   for(JBCXM__CustomerInfo__c li:trigger.new) {
       if(li.JBCXM__Status__c == 'a0U1a000000H1GiEAK' &&
           li.Onboarding_End_Date__c != null
       )
    {
     li.IS_Autonumber__c = Integer.valueOf(maxacc)+1;
    }       
    }       
}
Error
Error:Apex trigger AutonumberOnlyISOnb caused an unexpected exception, contact your administrator: AutonumberOnlyISOnb: execution of AfterUpdate caused by: System.FinalException: Record is read-only: ()
Do you know what could be?

Thanks
 
Best Answer chosen by Olavo Zapata
Olavo ZapataOlavo Zapata
Hi Glyn and Craig,

With both your help now it is working. Thanks a lot for that.

In the first time, I was trying to do the action with a before instead an after but was showing me an error of no record to update.
With Craig's help, I create the list to save the information from the loop as his example. But that showed some errors as well.
Now backing to use an before its working.

The final code if someone would like to try:
trigger AutonumberOnlyISOnb on JBCXM__CustomerInfo__c (before insert, before update){
    list<JBCXM__CustomerInfo__c> l= [SELECT Id,Name,IS_Autonumber__c FROM JBCXM__CustomerInfo__c WHERE IS_Autonumber__c !=:null ORDER BY IS_Autonumber__c DESC LIMIT 1];
    decimal maxacc = l[0].IS_Autonumber__c;
    
    List<JBCXM__CustomerInfo__c> LiList = new List<JBCXM__CustomerInfo__c>();
    for(JBCXM__CustomerInfo__c li:trigger.new) {
        if(li.JBCXM__Status__c == 'a0U1a000000H1GiEAK' && li.Onboarding_End_Date__c != null) {
            li.IS_Autonumber__c = Integer.valueOf(maxacc)+1;
        }
        LiList.add(li);
    }
}
Thanks guys,

All Answers

Craig Coates JWCraig Coates JW
This is an AFTER trigger but you aren't committing any changes the the database. Try this.
trigger AutonumberOnlyISOnb on JBCXM__CustomerInfo__c (after insert, after update) {
    list<JBCXM__CustomerInfo__c> l= [SELECT Id,Name,IS_Autonumber__c FROM JBCXM__CustomerInfo__c where IS_Autonumber__c !=:null order by IS_Autonumber__c desc limit 1];
    decimal maxacc = l[0].IS_Autonumber__c;
    
List <JBCXM__CustomerInfo__c> LiLst = new List<JBCXM__CustomerInfo__c>();   
for(JBCXM__CustomerInfo__c li:trigger.new) {
       if(li.JBCXM__Status__c == 'a0U1a000000H1GiEAK' &&
           li.Onboarding_End_Date__c != null
       )
    {
     li.IS_Autonumber__c = Integer.valueOf(maxacc)+1;
    }
    LiList.add(li)
​    }
    update LiList;       
}

 
Craig Coates JWCraig Coates JW
Whoops, forgot a semicolon
trigger AutonumberOnlyISOnb on JBCXM__CustomerInfo__c (after insert, after update) {
    list<JBCXM__CustomerInfo__c> l= [SELECT Id,Name,IS_Autonumber__c FROM JBCXM__CustomerInfo__c where IS_Autonumber__c !=:null order by IS_Autonumber__c desc limit 1];
    decimal maxacc = l[0].IS_Autonumber__c;
    
List <JBCXM__CustomerInfo__c> LiLst = new List<JBCXM__CustomerInfo__c>();   
for(JBCXM__CustomerInfo__c li:trigger.new) {
       if(li.JBCXM__Status__c == 'a0U1a000000H1GiEAK' &&
           li.Onboarding_End_Date__c != null
       )
    {
     li.IS_Autonumber__c = Integer.valueOf(maxacc)+1;
    }
	LiList.add(li);
​    }
	update LiList;       
}
Olavo ZapataOlavo Zapata
Hi @Craig,

Thanks for your help, but doesn't work I made the change that u suggested and continue showing me an error when I try to save anything to the Customer.

Strange that now looks a different error:
Apex trigger AutonumberOnlyISOnb caused an unexpected exception, contact your administrator: AutonumberOnlyISOnb: execution of AfterUpdate caused by: System.SObjectException: DML statement cannot operate on trigger.new or trigger.old: ()
Sorry if it is a stupid question but I've started read about apex now.

 
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
Olavo,

The problem is that you can't modify the fields of a record in its after trigger - it has already been written to the database and is now read-only.  You must do this in a before trigger - before the record is written to the database.  Change your afters to befores, and you should be OK.
Olavo ZapataOlavo Zapata
Hi Glyn and Craig,

With both your help now it is working. Thanks a lot for that.

In the first time, I was trying to do the action with a before instead an after but was showing me an error of no record to update.
With Craig's help, I create the list to save the information from the loop as his example. But that showed some errors as well.
Now backing to use an before its working.

The final code if someone would like to try:
trigger AutonumberOnlyISOnb on JBCXM__CustomerInfo__c (before insert, before update){
    list<JBCXM__CustomerInfo__c> l= [SELECT Id,Name,IS_Autonumber__c FROM JBCXM__CustomerInfo__c WHERE IS_Autonumber__c !=:null ORDER BY IS_Autonumber__c DESC LIMIT 1];
    decimal maxacc = l[0].IS_Autonumber__c;
    
    List<JBCXM__CustomerInfo__c> LiList = new List<JBCXM__CustomerInfo__c>();
    for(JBCXM__CustomerInfo__c li:trigger.new) {
        if(li.JBCXM__Status__c == 'a0U1a000000H1GiEAK' && li.Onboarding_End_Date__c != null) {
            li.IS_Autonumber__c = Integer.valueOf(maxacc)+1;
        }
        LiList.add(li);
    }
}
Thanks guys,
This was selected as the best answer
Yasser NabanguiYasser Nabangui
Thanks for this Trigger code, It helped me a lot