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
TuckoTucko 

Trigger firing more than once, how to stop it?

Hello guys,
I have these trigger and a helper clas to help stop firing more than once, but seems something doesn't work like it should, can someone point me where I am wrong. Thank you!

Trigger:
trigger AccountTrigger1 on Account (after update,after insert) {
       
    
     if(!HelperClass.hasAlreadyfired()){
    List<Account>listAccounts = new List<Account>();
        
    //Во for-циклусот листаме низ сите профили
        //за кои се проверува условот од if
    for(Account a : Trigger.new){
        
        //Се креираат 5 нови профили, доколку условот:
        // a.CheckPayment__c == True е исполнет
        if(a.CheckPayment__c == True){
            
            //Со секоја итерација на for - циклусот се креира по еден профил
            //for - циклусот завршува со креирање на нови профили кога 
            //итераторот i ќе ја достигне својата максимална вредност
            //во овој случај 5
            for (Integer i=0; i<5; i++){
               
                //Креирање на профил
                Account account = new Account();
                account.Name = 'Akaunt' + i;
                account.First_Name__c = 'Acount1';
                account.Second_Name__c = 'Surname1';            
                account.Status__c = 'Idle';
                listAccounts.add(account);
            }
         }
        //Додавање на профилите
        insert listAccounts;
     }
    }
    HelperClass.setAlreadyfired();
}
Ignore the comments :)

HelperClass:
 
public class HelperClass{
    public static boolean flagvalue = false;


    public static boolean hasAlreadyfired() {
        return flagvalue;
    }
    
    public static void setAlreadyfired() {
        flagvalue = true;
    }



}


 
Best Answer chosen by Tucko
Amit Chaudhary 8Amit Chaudhary 8
Hi Tucko,

Please follow below step :- 
Step 1 :- create one check box field on account object "AccountCreated__c" and set the default values as false. 
Step 2 :- And update that field as true once created 5 account by below trigger

Please try below code
trigger AccountTrigger1 on Account (before update,before insert) 
{
    if( AccountTriggerHandler.isFirstTime == true )
	{
		AccountTriggerHandler.isFirstTime = false;
		List<Account> listAccounts = new List<Account>();
			
		for(Account a : Trigger.new)
		{
			if(a.CheckPayment__c == True && a.AccountCreated__c == false )
			{
				for (Integer i=0; i<5; i++)
				{
					Account account = new Account();
					account.Name = 'Akaunt' + i;
					account.First_Name__c = 'Acount1';
					account.Second_Name__c = 'Surname1';            
					account.Status__c = 'Idle';
					listAccounts.add(account);
				}
				a.AccountCreated__c = true;
			}
		 }
           insert listAccounts;
     }
}

Please mark this as solution if this will help you.

Thanks
Amit Chaudhary

All Answers

Mrunali GaonkarMrunali Gaonkar
Hi Bozidar,
    This is due to Recursive Trigger.Following link will help you out.
https://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US

Thanks and Regards,
Mrunali Gaonkar.
TuckoTucko
Hi Mrunali, 

Thank you for your reply, I open the link and try the given code, but still my trigger is firing everytime I update something on the account, unless I uncheck checkPayment.
Mrunali GaonkarMrunali Gaonkar

Hi,

Your DML statement should be out of for loop.

 
TuckoTucko
I put out of both For loops, but still no changes.
Amit Chaudhary 8Amit Chaudhary 8
Hi Tucko,

Please try below code . Please create one handler class
public class AccountTriggerHandler
{
     public static Boolean isFirstTime = true;
}
Please update your trigger line below :-
trigger AccountTrigger1 on Account (after update,after insert) 
{
    if( AccountTriggerHandler.isFirstTime == true )
	{
		AccountTriggerHandler.isFirstTime = false;
		List<Account> listAccounts = new List<Account>();
			
		for(Account a : Trigger.new)
		{
			if(a.CheckPayment__c == True)
			{
				for (Integer i=0; i<5; i++)
				{
					Account account = new Account();
					account.Name = 'Akaunt' + i;
					account.First_Name__c = 'Acount1';
					account.Second_Name__c = 'Surname1';            
					account.Status__c = 'Idle';
					listAccounts.add(account);
				}
			 }
		 }
           insert listAccounts;
     }
}
Please mark this as solution if this will help you



 
TuckoTucko
Hi Amit, thank you for your effort, but it doesn't work, still creating new accounts after every update once checkPayment is true.
Amit Chaudhary 8Amit Chaudhary 8
Hi Tucho,

According to your existing logic every time you will update any account where checkPayment is true then will create 5 new account for same.
If you want to perform the same activity only once then you need to mark that flag as false.
 
TuckoTucko
Yes, I want to use the trigger only once, when CheckPayment become true and after that I want trigger to stop from creating new accounts, can you explain me what you think under: "you need to mark that flag as false." , as I am still new in salesforce and learning. 

Thank you.
TuckoTucko
I am sorry, but can you explain me little more about the workflow, which field need to be true in order to update CheckPayment as false ?
TuckoTucko
I made the workflow to work, but this is not the solution I need, I neet that checkbox to stay checked, Isn't there any way of doing this with helper class like above ?
Amit Chaudhary 8Amit Chaudhary 8
Hi Tucko,

Please follow below step :- 
Step 1 :- create one check box field on account object "AccountCreated__c" and set the default values as false. 
Step 2 :- And update that field as true once created 5 account by below trigger

Please try below code
trigger AccountTrigger1 on Account (before update,before insert) 
{
    if( AccountTriggerHandler.isFirstTime == true )
	{
		AccountTriggerHandler.isFirstTime = false;
		List<Account> listAccounts = new List<Account>();
			
		for(Account a : Trigger.new)
		{
			if(a.CheckPayment__c == True && a.AccountCreated__c == false )
			{
				for (Integer i=0; i<5; i++)
				{
					Account account = new Account();
					account.Name = 'Akaunt' + i;
					account.First_Name__c = 'Acount1';
					account.Second_Name__c = 'Surname1';            
					account.Status__c = 'Idle';
					listAccounts.add(account);
				}
				a.AccountCreated__c = true;
			}
		 }
           insert listAccounts;
     }
}

Please mark this as solution if this will help you.

Thanks
Amit Chaudhary
This was selected as the best answer
TuckoTucko
Thank you very much, works perfectly.