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
anvesh@force.comanvesh@force.com 

trigger failed on insertion of records

 

Here  my scinario is  if  my Account name is not empty then it should be insert 2 records and current record  but  insertion is failed.

 

 

trigger mergeAccs on Account (after insert) {
list<Account> acs=new list<Account>{new Account(name='mergeAN1'),new Account(name='AnveMerge2')};
  for(Account AC:trigger.new){
   if(ac.name!=' '){
   insert acs;
}
}
}

Best Answer chosen by Admin (Salesforce Developers) 
Avidev9Avidev9

As I said in my last answer "

  1. Make sure that the code doesn go recurssive 

"

To do this create a static variable and use it as blocker

 

public class RecursiveBlocker{
 public static Boolean isBlocked = false;
}

 

trigger mergeAccs on Account(after insert) {
    if(RecursiveBlocker.isBlocked == false){
list < Account > acs = new list < Account > { new Account(name = 'mergeAN1'), new Account(name = 'AnveMerge2') }; list < Account > acsInsert = new list < Account > (); for (Account AC: trigger.new) { if (ac.name != '') { acsInsert.addAll(acs.deepClone(false, false, false)); } } RecursiveBlocker.isBlocked = true; insert acsInsert;
} }

All Answers

Avidev9Avidev9

Try this!

 

trigger mergeAccs on Account(after insert) {
    list < Account > acs = new list < Account > {
        new Account(name = 'mergeAN1'), new Account(name = 'AnveMerge2')
    };
    list < Account > acsInsert = new list < Account > ();


    for (Account AC: trigger.new) {
        if (ac.name != '') {
            acsInsert.addAll(acs.deepClone(false, false, false));
        }
    }

    insert acsInsert;
}

 You are forgetting bascis

  1. Always do a bulk handling of records
  2. After insertion ids are popuplated and hence your trigger was falling on the second iteration
  3. Make sure that the code doesn go recurssive 
anvesh@force.comanvesh@force.com

as  per  your  code  now  i  am  getiing  a  big big big  almost  300+  lines of  error  code  i  am getting

Imran MohammedImran Mohammed

I am afraid with the scenario you have in which you want to insert 2 more accounts with the current one will lead to recursive "Maximum Trigger depth reached" error.

I would rather suggest you to have a temporary field flag on Account that will be hidden on page layout that will determine whether the 2 new records created are through trigger.

You can create a boolean field that will be set to true for newly created records in trigger as below

 

trigger mergeAccs on Account(after insert) {
list < Account > acList = new list < Account > ();


for (Account ac: trigger.new) {
if (ac.name != null && ac.insertedThroughTrigger__c ==false) {
//add your new Accounts here
Account a1 = new Account(name='mergeAN1', insertedThroughTrigger__c = true);
Account a2 = new Account(name='AnveMerge2', insertedThroughTrigger__c = true);

acList.add(a1);
acList.add(a2);
}
}

insert acList;
}

Avidev9Avidev9

As I said in my last answer "

  1. Make sure that the code doesn go recurssive 

"

To do this create a static variable and use it as blocker

 

public class RecursiveBlocker{
 public static Boolean isBlocked = false;
}

 

trigger mergeAccs on Account(after insert) {
    if(RecursiveBlocker.isBlocked == false){
list < Account > acs = new list < Account > { new Account(name = 'mergeAN1'), new Account(name = 'AnveMerge2') }; list < Account > acsInsert = new list < Account > (); for (Account AC: trigger.new) { if (ac.name != '') { acsInsert.addAll(acs.deepClone(false, false, false)); } } RecursiveBlocker.isBlocked = true; insert acsInsert;
} }
This was selected as the best answer
sandeep@Salesforcesandeep@Salesforce

Whenever we write trigger on an object with a particular event and apply same DML in that trigger then it launch cyclic trigger thread so in order to avoid it static variables is best way to use as expalined above. much appriciate this way.

anvesh@force.comanvesh@force.com

My  Dear  Thank  you  for  your  valueble information  i  am  new for  salesforce.......

 

 

any  way   how we can access the class in  the trigger....because  i  got  error  like this

 

"Variable does not exist: RecusrsiveBlocker.isBlocked at line 2 column 8"

anvesh@force.comanvesh@force.com
hiii Imran what is insertedThroughTrigger__c here...cant we get any error like if we have trigger__c(from insertedThroughTrigger__c)
Avidev9Avidev9
Have a look I made a typo !
RecusrsiveBlocker is not same as the class name RecursiveBlocker