You need to sign in to do that
Don't have an account?
Need help to call apex class from trigger
Hi All,
I have read about one of the Best Practices of Triggers is that : Writting the logic in Apex Class and Calling the class from trigger.
My Trigger :
My Apex Class :
But I am getting the below Error :
update fvr;
Please Help....
I have read about one of the Best Practices of Triggers is that : Writting the logic in Apex Class and Calling the class from trigger.
My Trigger :
trigger UpdateRiskStatus1 on Vendor_Risk__c (after insert) { UpdateRiskStatusUtils.UpdateVendorRiskStatus(Trigger.New); }
My Apex Class :
public class UpdateRiskStatusUtils { public static void UpdateVendorRiskStatus(List<Vendor_Risk__c> fvr) { for (Vendor_Risk__c objvr :[select Id , Risk_Status__c from Vendor_Risk__c where id NOT IN : Trigger.new and Risk_Status__c = 'Current']) { objvr.Risk_Status__c = 'Previous'; fvr.add(objvr); } update fvr; } }
But I am getting the below Error :
Error: Invalid Data. Review all error messages below to correct your data. Apex trigger UpdateRiskStatus1 caused an unexpected exception, contact your administrator: UpdateRiskStatus1: execution of AfterInsert caused by: System.SObjectException: DML statement cannot operate on trigger.new or trigger.old: Class.UpdateRiskStatusUtils.UpdateVendorRiskStatus: line 10, column 1Error is in this part :
update fvr;
Please Help....
You cannot do DML statement on trigger.new or trigger.old. The error you have receieved because you are adding values in the trigger.new list i.e: fvr.
So create a new list and update the values. Here is the updated code,
Note: Maximum number of record per DML event is 10000.
All Answers
You cannot do DML statement on trigger.new or trigger.old. The error you have receieved because you are adding values in the trigger.new list i.e: fvr.
So create a new list and update the values. Here is the updated code,
Note: Maximum number of record per DML event is 10000.
Thanks so much for such quick help and fixing my issue...!!!