• Peter Minetree
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 1
    Replies
Future methods are typically used for:
Callouts to external Web services. If you are making callouts from a trigger or after performing a DML operation, you must use a future or queueable method. A callout in a trigger would hold the database connection open for the lifetime of the callout and that is a "no-no" in a multitenant environment.
 
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;
            }
    }
}