• K S Syamkumar
  • NEWBIE
  • 10 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
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);  
        
    }
}

 
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);  
        
    }
}