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
NKrishnaNKrishna 

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);
       } 
    }
}

 

kiranmutturukiranmutturu

what is the batch size of the records that u set in the datalaoder?

NKrishnaNKrishna

Its default 200 only.

kiranmutturukiranmutturu

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....

NKrishnaNKrishna

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.

NKrishnaNKrishna

The sample debug log

 

09:59:13.139 (139264000)|USER_DEBUG|[36]|DEBUG|SSS AccountList Size() BEFORE INSERT 200
....................
....................
....................
09:59:14.833 (1833285000)|USER_DEBUG|[41]|DEBUG|SSS AccountList Size() AFTER INSERT 200
....................
....................
....................
09:59:15.231 (2231067000)|SYSTEM_METHOD_ENTRY|[36]|System.debug(ANY)
09:59:15.231 (2231075000)|USER_DEBUG|[36]|DEBUG|SSS AccountList Size() BEFORE INSERT 200
....................
....................
....................
09:59:15.527 (2527872000)|SYSTEM_METHOD_ENTRY|[41]|System.debug(ANY)
09:59:15.527 (2527881000)|USER_DEBUG|[41]|DEBUG|SSS AccountList Size() AFTER INSERT 200

 

Kevin SwiggumKevin Swiggum

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.