You need to sign in to do that
Don't have an account?
Olavo 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.
Thanks
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
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: Thanks guys,
All Answers
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: Sorry if it is a stupid question but I've started read about apex now.
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.
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: Thanks guys,