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
Amlan MaitiAmlan Maiti 

How to Bulkify trigger handler Code

Hi All,
I want to update some task ownerid in my task trigger handler code. I have tried a piece of code which is not bulkified. Can you please help me with the bulkification of the same.
Thanks in Advance.
 
List<task> taskWithoutClosedCases = new List<Task>();
//Some logic to add tasks in the above List
if(mapId_Case!= null){
	for(task objTask : taskWithoutClosedCases){
		if(isInsert){
			if(objTask.Override_Default_Assigned_To__c == false && objTask.IsClosed == false){
				List<Mapping__c> mappingRecord = [SELECT ID, Assigned_To__c FROM Mapping__c where Business_Unit__c=:objTask.Contact_Center_BU__c 
												  AND Task_Type__c=:objTask.Task_Type__c AND Type__c = 'Task Type - Assigned To'];
				if(mappingRecord.size() > 0){
					if(mappingRecord.get(0).Assigned_To__c != null){
						objTask.OwnerId = mappingRecord.get(0).Assigned_To__c;
					}else if(mappingRecord.get(0).Assigned_To__c == null && mappingRecord.get(0).Id != null){
						objTask.OwnerId = UserInfo.getUserId();
					}
				}
			}
		}
	}
}

 
Best Answer chosen by Amlan Maiti
Bryan Leaman 6Bryan Leaman 6
I think something like the following, where you loop through tasks twice, once building a list of the criteria used in your SOQL query, build a map based on the key values that associate mapping__c with a task, then loop through the tasks again and lookup into the map:
 
List<task> taskWithoutClosedCases = new List<Task>();
//...Some logic to add tasks in the above List
if(mapId_Case!= null && isInsert) {

    // Get lists (sets) of business units and task types for soql query
    Set<String> BusinessUnits = new Set<String>();	// or is this an Id ??
    Set<String> TaskTypes = new Set<String>(); 
    for(task objTask : taskWithoutClosedCases){
        if(objTask.Override_Default_Assigned_To__c == false && objTask.IsClosed == false){
	    if (!String.isBlank(objTask.Contact_Center_BU__c)) BusinessUnits.add(objTask.Contact_Center_BU__c);
	    if (!String.isBlank(objTask.Task_Type__c) TaskTypes.add(objTask.Task_Type__c);
	}
    }

    // Query Mapping__c once and build map to records
    Map<String,Mapping__c> mappingMap = new Map<String,Mapping__c>();
    for(Mapping__c m : [
	SELECT ID, Assigned_To__c 
	FROM Mapping__c 
	WHERE Business_Unit__c in :BusinessUnits
	  AND Task_Type__c in :TaskTypes 
	  AND Type__c = 'Task Type - Assigned To']
    ) {
	mappingMap.put(m.Business_Unit__c + ':' + m.TaskType__c, m);
    }

    // process tasks
    for(task objTask : taskWithoutClosedCases){
        if(objTask.Override_Default_Assigned_To__c == false && objTask.IsClosed == false){
	    Mapping__c mappingRecord = mappingMap.get(objTask.Contact_Center_BU__c + ':' + objTask.Task_Type__c);
            if(mappingRecord!=null){
                    if(mappingRecord.Assigned_To__c != null){
                        objTask.OwnerId = mappingRecord.Assigned_To__c;
                    }else if(mappingRecord.Assigned_To__c == null && mappingRecord.Id != null){
                        objTask.OwnerId = UserInfo.getUserId();
                    }
            }
        }
    }
}

Sorry, looks like it didn't keep all my indents the way I intended them.