+ Start a Discussion
Thomas D.Thomas D. 

Bulkified trigger still giving to many soql queries error

Hey All,

I have a trigger which i thought was bulkified, however when mass updating records through gridbuddy i still get the to many soql queries error.

Any help is appreciated.

below is the code of the trigger.

the map query is where it errors: 
trigger Fill_OneOff_Lookups on VG_One_Off_Planning__c (before update, before insert) {
            system.debug('test1 in trigger');
            list<id> UserIDs = new list<id>();
    		list<User> CurrentUsers = new list<User>();
            for (VG_One_Off_Planning__c OneoffLine : trigger.new)
                system.debug('test2 in for ' + OneoffLine);
                if(OneoffLine.ContactID__c != null)
                	OneoffLine.Contact__c = OneoffLine.ContactID__c;
                if(OneoffLine.Al_ID__C != null)
                	OneoffLine.Account_Location__c = OneoffLine.AL_ID__c;
                OneoffLine.Account__c = OneoffLine.Location_Account_ID__c;                 
                system.debug('test3 in trigger' + OneoffLine.User_Id__c);
                if(OneoffLine.User_ID__c != null)  
                	boolean ExecuteCode = true;
                   	/* if(trigger.IsUpdate){
                        VG_One_Off_Planning__c OldPlanningRecord = System.Trigger.oldMap.get(OneOffLine.Id);
                        system.debug('test3 in trigger update' + OldPlanningRecord);
                        if(OneoffLine.User_ID__c == OldPlanningRecord.User_ID__c){
                            ExecuteCode = false;
                	system.debug('test4 ' + ExecuteCode);                
                	//if(ExecuteCode == true){               
    		Map<Id,User> GetUsers = new map<Id,User>([SELECT Id, IsActive FROM User WHERE Id in :UserIDs]);
    		currentusers = GetUsers.values();
    		for (VG_One_off_Planning__c oneoff : trigger.new)
               	for(integer index =0; index <  CurrentUsers.size(); index++)
                	if(CurrentUsers[index].IsActive == true && oneoff.User_ID__c == CurrentUsers[index].Id )
                		Oneoff.OwnerId = Oneoff.User_ID__c;

With kind regards,
Anurag JainAnurag Jain
May be your trigger is calling again and again or any other trigger is excuting check debug logs
Hi thomas ,

could you do the operation after setting a debug log and share the debug log 
I would check your workflow rules and field updates - to see if something is firing the trigger over and over.  You code looks okay.