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
jjagtjjagt 

APEX Trigger Simple Field Update (Using a formula field)

Hello all,

 

I am trying to create a trigger that sets a Currency Field in the Object, to the result of what a formula was.  The issue is that when i do this, it says 

 

Error:Apex trigger UpdatePriceValue caused an unexpected exception, contact your administrator: UpdatePriceValue: execution of AfterUpdate caused by: System.Exception: Record is read-only: Trigger.UpdatePriceValue: line 4, column 3

 

Any way around this?  I have the code below:

 

 

trigger UpdatePriceValue on Services_Selection__c (after insert, after update)

{

for (Services_Selection__c SS : Trigger.new)

{

SS.Price_Value__c = SS.Price_Formula__c;

}

}

 

Thanks in advance, to anyone who can help resolve this small issue.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
JimRaeJimRae

Change to a before trigger instead of after.  Based on what you have displayed, no reason why you couldn't.

 

 

trigger UpdatePriceValue on Services_Selection__c (before insert, before update)

{

for (Services_Selection__c SS : Trigger.new)

{

SS.Price_Value__c = SS.Price_Formula__c;

}

}