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
Sushma  RamakrishnanSushma Ramakrishnan 

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 :
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 1
Error is in this part :
     update fvr;

Please Help....

 
Best Answer chosen by Sushma Ramakrishnan
Arunkumar RArunkumar R
Hi,
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,
public class UpdateRiskStatusUtils
{
    public static void UpdateVendorRiskStatus(List<Vendor_Risk__c> fvr)
    {
     List<Vendor_Risk__c> fvrList = new List<Vendor_Risk__c>();
      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';
       fvrList.add(objvr); 
     } 
     update fvrList;     
    }
    
}

Note: Maximum number of record per DML event is 10000. 

All Answers

Arunkumar RArunkumar R
Hi,
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,
public class UpdateRiskStatusUtils
{
    public static void UpdateVendorRiskStatus(List<Vendor_Risk__c> fvr)
    {
     List<Vendor_Risk__c> fvrList = new List<Vendor_Risk__c>();
      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';
       fvrList.add(objvr); 
     } 
     update fvrList;     
    }
    
}

Note: Maximum number of record per DML event is 10000. 
This was selected as the best answer
Sushma  RamakrishnanSushma Ramakrishnan
Hi Arunkumar R,

Thanks so much for such quick help and fixing my issue...!!!