You need to sign in to do that
Don't have an account?
K 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);
}
}
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);
}
}
Try Below Code
Made Some Changes In Apex Class Now Working Fine Please Mark It As Best Answer If it Helps
Thank You!
All Answers
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:-
Apex class:-
Test class:-
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
Try Below Code
Made Some Changes In Apex Class Now Working Fine Please Mark It As Best Answer If it Helps
Thank You!
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?
Try Below Code Please Mark It As Best Answer If it Helps
Thank You!