• Huma Perween
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
Hi
I'm trying to do the Understanding Execution Context module challenge and I really can't see why this won't work. The following code says
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AccountTrigger: execution of BeforeInsert caused by: System.SObjectException: DML statement cannot operate on trigger.new or trigger.old Class.AccountTriggerHandler.CreateAccounts:

trigger AccountTrigger on Account (before insert) { 
     if ( Trigger.isBefore && Trigger.isInsert)
     {
              AccountTriggerHandler.CreateAccounts(Trigger.new);
     }
    }

public class AccountTriggerHandler {
    public static void CreateAccounts(List<Account> Accnts)
    {
         List<Account> ValidAccounts = new List<Account>();
    
            for (Account a : Accnts) {
               a.ShippingState = a.billingState;
               
                ValidAccounts.add(a);
               
            }
    
            if (ValidAccounts.size() > 0) {
                insert ValidAccounts;
            }
    }
}