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

before insert, After insert triggers called twice
I created a new apex trigger in Account object.
The trigger checks the Account's OwnerId is Active or not.
If active then It will go to before insert & after insert events as per the below trigger code.
Once I check with active users ITs worked fine . but,
I created a new csv file called Account.csv consists 250 Active User's records and 250 Inactive user's records.
So Once I ran via data loader, I am getting 3 debug log files.
The every debug logs shows the 2 time of before insert operation and 2 times of after insert operation.
Why It happened?
Anything I missed in the code?
trigger AccountTrigger on Account (before insert, before update, after insert, after update) { if(Trigger.IsInsert) { List<Account> AccountList = new List<Account>(); Map<Id,List<Account>> AccountListMap = new Map<Id,List<Account>>(); Map<Id, Boolean> UsersActiveMap = new Map<Id, Boolean>(); for(Account AccountNew : Trigger.New) { List<Account> ListAccount = AccountListMap.get(AccountNew.OwnerId); if(ListAccount == null) { ListAccount = new List<Account>(); AccountListMap.put(AccountNew.OwnerId, ListAccount); } ListAccount.add(AccountNew); } system.debug('AccountListMap Values '+ AccountListMap); system.debug('AccountListMap SIZE Values '+ AccountListMap.size()); List<User> UserList = [SELECT Id, name, Username, IsActive FROM user WHERE Id IN : AccountListMap.Keyset()]; for(User Users : UserList) { UsersActiveMap.put(Users.Id, Users.IsActive); } system.debug('UsersActiveMap Values '+ UsersActiveMap); for(Id OwnerId : AccountListMap.Keyset()) { if(UsersActiveMap.get(OwnerId)) { AccountList.addall(AccountListMap.get(OwnerId)); system.debug('AccountList' + AccountList.size()); } } system.debug('AccountList Values '+ AccountList); if(Trigger.IsBefore) { system.debug('SSS AccountList Size() BEFORE INSERT ' + AccountList.size()); AccountTriggerClass.UpdateCallFrequency(AccountList); } if(Trigger.IsAfter) { system.debug('SSS AccountList Size() AFTER INSERT ' + AccountList.size()); AccountTriggerClass.CreateTaskOnAccount(AccountList, false); } } if(Trigger.IsUpdate) { if(Trigger.IsBefore) { Map<Id, Account> AccountMapNew = new Map<Id, Account>(); Map<Id, string> AccountMapOld = new Map<Id, string>(); //List<Account> AccountList = new List<Account>(); Map<Id,List<Account>> AccountListMap = new Map<Id,List<Account>>(); Map<Id, Boolean> UsersActiveMap = new Map<Id, Boolean>(); for(Account AccountNew : Trigger.New) { List<Account> ListAccount = AccountListMap.get(AccountNew.OwnerId); if(ListAccount == null) { ListAccount = new List<Account>(); AccountListMap.put(AccountNew.OwnerId, ListAccount); } ListAccount.add(AccountNew); } system.debug('AccountListMap Values '+ AccountListMap); system.debug('AccountListMap SIZE Values '+ AccountListMap.size()); List<User> UserList = [SELECT Id, name, Username, IsActive FROM user WHERE Id IN : AccountListMap.Keyset()]; for(User Users : UserList) { UsersActiveMap.put(Users.Id, Users.IsActive); } system.debug('UsersActiveMap Values '+ UsersActiveMap); for(Id OwnerId : AccountListMap.Keyset()) { if(UsersActiveMap.get(OwnerId)) { List<Account> AccList = new List<Account>(); AccList = AccountListMap.get(OwnerId); for(Account Acc : AccList) { AccountMapNew.put(Acc.Id, Acc); } } } system.debug('AccountMapNew Values '+ AccountMapNew); for(Account AccountOld : Trigger.Old) { AccountMapOld.put(AccountOld.id, AccountOld.Name); } system.debug('AccountMapOld Values '+ AccountMapOld); AccountTriggerClass.UpdateAccount(AccountMapNew, AccountMapOld); } if(Trigger.IsAfter) { List<Account> AccountsMarkedDeleted = new List<Account>(); Map<Id, Account> OldAccountMap = new Map<Id, Account>(); Map<Id, Account> NewAccountMap = new Map<Id, Account>(); //List<Account> AccountList = new List<Account>(); Map<Id,List<Account>> AccountListMap = new Map<Id,List<Account>>(); Map<Id, Boolean> UsersActiveMap = new Map<Id, Boolean>(); for(Account AccountNew : Trigger.New) { List<Account> ListAccount = AccountListMap.get(AccountNew.OwnerId); if(ListAccount == null) { ListAccount = new List<Account>(); AccountListMap.put(AccountNew.OwnerId, ListAccount); } ListAccount.add(AccountNew); } system.debug('AccountListMap Values '+ AccountListMap); system.debug('AccountListMap SIZE Values '+ AccountListMap.size()); List<User> UserList = [SELECT Id, name, Username, IsActive FROM user WHERE Id IN : AccountListMap.Keyset()]; for(User Users : UserList) { UsersActiveMap.put(Users.Id, Users.IsActive); } system.debug('UsersActiveMap Values '+ UsersActiveMap); for(Account AccountOld : Trigger.Old) { OldAccountMap.put(AccountOld.Id, AccountOld); } system.debug('OldAccountMap Values '+ OldAccountMap); for(Id OwnerId : AccountListMap.Keyset()) { if(UsersActiveMap.get(OwnerId)) { List<Account> AccList = new List<Account>(); AccList = AccountListMap.get(OwnerId); for(Account Acc : AccList) { //AccountMapNew.put(Acc.Id, Acc); if(!Acc.Active_Dealer__c) AccountsMarkedDeleted.add(Acc); else { //if(UsersActiveMap.get(AccountNew.OwnerId)) NewAccountMap.put(Acc.Id, Acc); } } } } system.debug('AccountsMarkedDeleted Values '+ AccountsMarkedDeleted); system.debug('NewAccountMap Values '+ NewAccountMap); AccountTriggerClass.UpdateContactForActiveDealers(AccountsMarkedDeleted); AccountTriggerClass.UpdateTaskOnAccount(NewAccountMap, OldAccountMap); } } }
what is the batch size of the records that u set in the datalaoder?
Its default 200 only.
then its ok to get 2 debug logs for insert and 2 debug logs for after insert. whats wrong in that?
first 200--> before insert then remaining 50 --> one more before insert like the same for after insert....
In the first debug log,
I am getting two times of 200 records in before & after insert.
the second debug log, I am, getting two times of 50 records in before & after insert.
Every record called two times on before insert and two times on after insert.
thats what trigger does...
Please help me.
The sample debug log
Do you have any workflow rules on Account that perform field updates? Workflow will fire a second update on the record...and in turn will fire the triggers again.