You need to sign in to do that
Don't have an account?

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:
HelperClass:
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; } }
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
Please mark this as solution if this will help you.
Thanks
Amit Chaudhary
All Answers
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.
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.
Hi,
Your DML statement should be out of for loop.
Please try below code . Please create one handler class Please update your trigger line below :- Please mark this as solution if this will help you
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.
Thank you.
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
Please mark this as solution if this will help you.
Thanks
Amit Chaudhary