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
K S SyamkumarK S Syamkumar 

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

Hello All,
First of all this my first question posting here in this forum. If missed to follow any process please applogize and guide me the correct way.

I am verymuch new to sales force and I was trying out with trailhead module Apex & .Net Basics > Understand Execution Context.

while writing the code challege I faced an error like
---------------------------------------------------------------------------------
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: line 13, column 1
Trigger.AccountTrigger: line 3, column 1: []
----------------------------------------------------------------
WIth my limitted knowledge I tried all the possibility but could not succed. Please suggest what is wrong with my code. Thank you in advance.

AccountTriggerHandler class
public class AccountTriggerHandler {
    public static void CreateAccounts(List<Account> accts) {
        List<Account> newActs = new List<Account>();
        for (Account a : accts) {
            Account ac = new Account();
            ac = a;
            if(a.BillingState!=a.BillingState){
               a.ShippingStreet = a.BillingState ;
            }
            newActs.add(a);
        }
        if (newActs.size() > 0) {
            insert newActs;
        }
    }
}

Trigger
trigger AccountTrigger on Account (before insert) {
        System.debug('called trigger');
    if (Trigger.isBefore  && Trigger.isInsert) {
        System.debug('AccountTriggerHandler.CreateAccounts(Trigger.New)');
        AccountTriggerHandler.CreateAccounts(Trigger.New);
    }
}



AccountTriggerTest class

@isTest
public class AccountTriggerTest {
    @isTest static void TestCreateNewAccountInBulk() {
        List<Account> accts = new List<Account>();
        for(Integer i=0; i < 200; i++) {
            Account acct = new Account(Name='Test Account ' + i, BillingState = 'CA');
            accts.add(acct);
        }              
        // Perform Test
        Test.startTest();
        if(accts.size() > 0){
            insert accts;    
        }
        Test.stopTest();
        
       
        Boolean x = False;
        System.debug('data '+verifyAccts[0].BillingState);
        if (verifyAccts[0].BillingState == verifyAccts[0].BillingState ){
            x= True;
        }
           
        System.assertEquals(True, x);  
        
    }
}

 
Best Answer chosen by K S Syamkumar
CharuDuttCharuDutt
Hii K S Syamkumar
Try Below Code
Made Some Changes In Apex Class Now Working Fine
public class AccountTriggerHandler {
    public static void CreateAccounts(List<Account> accts) {
       
        for (Account a : accts) {
           
            if(a.BillingState!=null){
               a.ShippingStreet = a.BillingState ;
            }
            
        }
       
    }
}
Please Mark It As Best Answer If it Helps
Thank You!

 

All Answers

Suraj Tripathi 47Suraj Tripathi 47
Hi K S SyamKumar,

You can take refernce from this below code:-

If you write the trigger for before insert then you cannot perform DML operation i.e insert/update inside apex code. Check this code
Trigger:-
trigger AccountTrigger on Account (before insert) {
        System.debug('called trigger');
    if (Trigger.isBefore  && Trigger.isInsert) {
        System.debug('AccountTriggerHandler.CreateAccounts(Trigger.New)');
        AccountTriggerHandler.CreateAccounts(Trigger.New);
    }
}

Apex class:-
public class AccountTriggerHandler {
    public static void CreateAccounts(List<Account> accts) {
        List<Account> newActs = new List<Account>();
        for (Account a : accts) {
            if(a.BillingState!=null){
               a.ShippingStreet = a.BillingState ;
            }
        }
    }
}

Test class:-
@isTest
public class AccountTriggerTest {
    @isTest static void TestCreateNewAccountInBulk() {
        List<Account> accts = new List<Account>();
        for(Integer i=0; i < 200; i++) {
            Account acct = new Account(Name='Test Account ' + i, BillingState = 'CA');
            accts.add(acct);
        }              
        Test.startTest();
        if(accts.size() > 0){
            insert accts;    
        }
        Test.stopTest();
      
        Boolean x = False;
        System.debug('data '+Accts[0].BillingState);
        if (Accts[0].BillingState == Accts[0].BillingState ){
            x= True;
        }
           
        System.assertEquals(True, x);  
        
    }
}

In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 

Thanks and Regards
Suraj Tripathi
CharuDuttCharuDutt
Hii K S Syamkumar
Try Below Code
Made Some Changes In Apex Class Now Working Fine
public class AccountTriggerHandler {
    public static void CreateAccounts(List<Account> accts) {
       
        for (Account a : accts) {
           
            if(a.BillingState!=null){
               a.ShippingStreet = a.BillingState ;
            }
            
        }
       
    }
}
Please Mark It As Best Answer If it Helps
Thank You!

 
This was selected as the best answer
Rajinder Singh 34Rajinder Singh 34
Thanks for sharing this interesting information. I like this article (https://www.hindisight.com/). 
K S SyamkumarK S Syamkumar
@Suraj Tripathi 47, I have tried out your suggestion but unfortunately it did not work.
As I understand from your explanation we should not do any manual DML if a before insert is there in the execution context. So I removed the manual insert and tried. But it was same. Then I tried the suggestion from @CharuDutt surprisingly it workd. But here I did not understand what was the issue with the if condition that checks where billing state and shipping state are equal or not.

Requirement was to set shipping state to billing state. in my if condition i checked whether they are same or not and if not a manual assignment is done. But as @CharuDutt mentioned it is only checking whether Billing state is null or not.
Only thing I suspect here is that Shipping state is null in the if condition I wrote.

Can anyone pls help to understand me how does it help?


 
CharuDuttCharuDutt
Hii K S Syamkumar
Try  Below Code 
public class AccountTriggerHandler {
    public static void CreateAccounts(List<Account> accts) {
       
        for (Account a : accts) {
           
            if(a.BillingState != a.ShippingStreet && a.BillingState != null && a.ShippingStreet != null){
               a.ShippingStreet = a.BillingState ;
            }
            
        }
       
    }
}



trigger AccountTrigger2 on Account (before insert) {
        System.debug('called trigger');
    if (Trigger.isBefore  && Trigger.isInsert) {
        System.debug('AccountTriggerHandler.CreateAccounts(Trigger.New)');
        AccountTriggerHandler.CreateAccounts(Trigger.New);
    }
}



@isTest
public class AccountTriggerTest {
    @isTest static void TestCreateNewAccountInBulk() {
        List<Account> accts = new List<Account>();
        for(Integer i=0; i < 200; i++) {
            Account acct = new Account(Name='Test Account ' + i, BillingState = 'CA',ShippingStreet = 'lA');
            accts.add(acct);
        }              
        // Perform Test
        Test.startTest();
        if(accts.size() > 0){
            insert accts;    
        }
        Test.stopTest();
        
       
        Boolean x = False;
        System.debug('data '+accts[0].BillingState);
        if (accts[0].BillingState == accts[0].BillingState ){
            x= True;
        }
           
        System.assertEquals(True, x);  
        
    }
}
Please Mark It As Best Answer If it Helps
Thank You!