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
Abhijeet Purohit01Abhijeet Purohit01 

Error message

I am trying to update the value of a date / time field called as Last_Stage_Change__c by system.now(). When ever there is a change in value of a field called Sales_Order_Status__c then the field Last_Stage_Change__c should be updated. Both the fields belong to the the same object called Sales_Order__c. Here, Sales_Order_Status__c is a picklist field.

This is the code:

trigger UpdateField_LastStageChange on Sales_Order__c (after insert, after update) {
        List <Sales_Order__c> statusInsert = new List<Sales_Order__c>();
        List <Sales_Order__c> statusUpdate = new List<Sales_Order__c>();
        if(trigger.isInsert){
            for(Sales_Order__c so1:trigger.new){
                if( so1.Sales_Order_Status__c!=Null){
                    so1.Last_Stage_Change__c = system.now();
                    statusInsert.add(so1);
             } }
            update statusInsert;
            System.debug('last Stage Change Date/Time during Insert:'+statusInsert);
        } else if(trigger.isUpdate){
            for(Sales_Order__c so2:trigger.new){
                if(so2.Sales_Order_Status__c!=Null && so2.Sales_Order_Status__c!=trigger.oldMap.get(so2.id).Sales_order_Status__c){
                    so2.Last_Stage_Change__c = system.now();
                    statusUpdate.add(so2);
          } } update statusUpdate;
            System.debug('last Stage Change Date/Time during update:'+statusUpdate);
        } }

  I got an error message:

This is the error message: Error:Apex trigger UpdateField_LastStageChange caused an unexpected exception, contact your administrator: UpdateField_LastStageChange: execution of AfterUpdate caused by: System.FinalException: Record is read-only: Trigger.UpdateField_LastStageChange. How to solve this problem?

 Is it possible to replace the above trigger just by setting the default value of the field Last_Stage_Change__c to NOW()?

 Everytime the field is queried it will run the NOW() method and give the desired result.

 

Best Answer chosen by Admin (Salesforce Developers) 
jungleeejungleee

HI Abhijeet,

 

Just change the events to beforeupdate and before Insert in the trigger's first line. You cannot update the records of the objects on which you have written on trigger in the after events.

 

Hope this helps!

 

-Sam

All Answers

jungleeejungleee

HI Abhijeet,

 

Just change the events to beforeupdate and before Insert in the trigger's first line. You cannot update the records of the objects on which you have written on trigger in the after events.

 

Hope this helps!

 

-Sam

This was selected as the best answer
crop1645crop1645

and you don't need the update DML statements; before triggers will automatically update the records if the objects in Trigger.new are modified

Abhijeet Purohit01Abhijeet Purohit01

Thank you. Its working. I have changed from after to before and also I have commented the update operation.