// Populate the list of items based on trigger typeList
<AIT_Item__C> itemList = [SELECT j.Precio_Item__C, j.Orden_de_Servicio__C FROM AIT_Item__C j WHERE j.Orden_de_Servicio__r.id IN : Trigger.new FOR UPDATE];
The issue is, using a "before update trigger" I have to edit twice the Object in order to see the results. One to update the fields and another for the trigger to do the calculation.
Using an "after update trigger" (That would do both at one edit) I get the error: System.FinalException: Record is read-only: Trigger.ActualizaTotal_Orden: line 6, column 4
I reviewed the documentation and looks like you can't update a "new" field in a "Aftert Update" Trigger. So I just need a "After Update" Trigger to do what my current "Before Update" Trigger does.
I guess the issue would be due to use of Directly iterating trigger.new, once i also faced same error, so try this:
trigger WOROrder on Orden_de_Servicio__C (before update) {
List<Orden_de_Servicio__C> toUpdate=new List<Orden_de_Servicio__C>();
for(Orden_de_Servicio__C myOrder : [Select Id, Total_Orden__C from Orden_de_Servicio__C where id in: Trigger.new]){
myOrder.Total_Orden__C = 0;
// Populate the list of items based on trigger typeList
<AIT_Item__C> itemList = [SELECT j.Precio_Item__C, j.Orden_de_Servicio__C FROM AIT_Item__C j WHERE j.Orden_de_Servicio__r.id IN : Trigger.new FOR UPDATE];
// Process the list of items
for(AIT_Item__C itemToProcess : itemList){
myOrder.Total_Orden__C += itemToProcess.Precio_Item__C; }
toUpdate.add(myOrder);}
Th code can be optimized further, make sure you follow best practices.
Thanks Rahul, but it doen't do the trick (not even with the double edit)
Here it is the last version of my code (I improved it a little):
If the trigger is configured as "before update" (and deleting line 21) it does the addition with no errors but i need to edit the Order twice (The first update the values, and the second does the calculation.
If the trigger is configured as "after update" I get the error showed below. (but if it worked it would avoid the double editing)
Any suggesions?
Thanks in advance
trigger ActualizaTotal_Orden on Orden_de_Servicio__C (after update) {
Map<ID, Orden_de_Servicio__C> updateMap = new Map<ID, Orden_de_Servicio__C>();
// Populate the list of items based on trigger type List<AIT_Item__C> itemList = [SELECT j.Precio_Item__C, j.Orden_de_Servicio__C FROM AIT_Item__C j WHERE j.Orden_de_Servicio__r.id IN : Trigger.new FOR UPDATE];
for(Orden_de_Servicio__C myOrder : trigger.new){ myOrder.Total_Orden__C = 0; //JUST IN CASE THIS IS LINE 10
// Process the list of items for(AIT_Item__C itemToProcess : itemList) { myOrder.Total_Orden__C += itemToProcess.Precio_Item__C; //myOrder.Total_Orden__C += (trigger.new[0].Precio_Item__C); }
updateMap.put(myOrder.id, myOrder);}
update updateMap.values(); //JUST IN CASE THIS IS LINE 21 }
I get the following error:
Error: Invalid Data. Review all error messages below to correct your data.
Apex trigger ActualizaTotal_Orden caused an unexpected exception, contact your administrator: ActualizaTotal_Orden: execution of AfterUpdate caused by: System.FinalException: Record is read-only: Trigger.ActualizaTotal_Orden: line 10, column 4
What is the issue you are facing ?
You can use after update.....
The issue is, using a "before update trigger" I have to edit twice the Object in order to see the results. One to update the fields and another for the trigger to do the calculation.
Using an "after update trigger" (That would do both at one edit) I get the error: System.FinalException: Record is read-only: Trigger.ActualizaTotal_Orden: line 6, column 4
I reviewed the documentation and looks like you can't update a "new" field in a "Aftert Update" Trigger. So I just need a "After Update" Trigger to do what my current "Before Update" Trigger does.
Thanks in advance
Victor
I guess the issue would be due to use of Directly iterating trigger.new, once i also faced same error, so try this:
Th code can be optimized further, make sure you follow best practices.
Thanks Rahul, but it doen't do the trick (not even with the double edit)
Here it is the last version of my code (I improved it a little):
If the trigger is configured as "before update" (and deleting line 21) it does the addition with no errors but i need to edit the Order twice (The first update the values, and the second does the calculation.
If the trigger is configured as "after update" I get the error showed below. (but if it worked it would avoid the double editing)
Any suggesions?
Thanks in advance
trigger ActualizaTotal_Orden on Orden_de_Servicio__C (after update) {
Map<ID, Orden_de_Servicio__C> updateMap = new Map<ID, Orden_de_Servicio__C>();
// Populate the list of items based on trigger type
List<AIT_Item__C> itemList = [SELECT j.Precio_Item__C, j.Orden_de_Servicio__C
FROM AIT_Item__C j WHERE j.Orden_de_Servicio__r.id IN : Trigger.new FOR UPDATE];
for(Orden_de_Servicio__C myOrder : trigger.new){
myOrder.Total_Orden__C = 0; //JUST IN CASE THIS IS LINE 10
// Process the list of items
for(AIT_Item__C itemToProcess : itemList)
{
myOrder.Total_Orden__C += itemToProcess.Precio_Item__C;
//myOrder.Total_Orden__C += (trigger.new[0].Precio_Item__C);
}
updateMap.put(myOrder.id, myOrder);}
update updateMap.values(); //JUST IN CASE THIS IS LINE 21
}
I get the following error:
Error: Invalid Data. Review all error messages below to correct your data.
Apex trigger ActualizaTotal_Orden caused an unexpected exception, contact your administrator: ActualizaTotal_Orden: execution of AfterUpdate caused by: System.FinalException: Record is read-only: Trigger.ActualizaTotal_Orden: line 10, column 4