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
cl0s3rcl0s3r 

Bulkify Case Trigger

This is killing me, I am trying to bulkify this trigger so I dont get the dreaded SQL queries:101 msg.  I am not having any success thus far, any help would be greatly appreciated.

trigger AcscSecLvlPickup on Case (before update) {
	
	for(Case c: Trigger.new){
		System.debug('Alerting on --------> Starting the AcscSecLvlPickup Trigger');
		Task[] tsk = new Task[]{};
		
		for(Task t:[Select id from Task where whatid = :c.id and Status not in ('Canceled', 'Deferred', 'Completed')]){
			tsk.add(t);
		}
		System.debug('Alerting on --------> task '+tsk);
		//List<Task> tas = [Select id from Task where whatid = :c.id and Status not in ('Canceled', 'Deferred', 'Completed')];
		//List<Case> cas = new Case[]{};
		if(trigger.isUpdate){
			if(c.Create_Task__c == true && c.Status == 'Closed' && c.RecordTypeId == '012300000000PhJAAU'){
				if(tsk.size() > 0){
					for(integer i = 0; i<tsk.size();i++){
						tsk[i].Status = 'Completed';
						
					}
				}
				update tsk;
				c.Create_Task__c = false;
		 }
		}else{
		//System.debug('Alerting on --------> Starting the AcscSecLvlPickup Trigger');
		Case oldCase = Trigger.oldMap.get(c.ID);
		System.debug('Alerting on AcscSecLvlPickup Trigger --------------> '+'Stepping into the Entering If');
		if(c.SecLvlVal__c != true && c.OwnerId != oldCase.OwnerId && oldCase.ACSC_Second_Level_Picked_Up_Date__c == null && oldCase.Type == 'ACSC2ndLevel'){
		  c.ACSC_Second_Level_Picked_Up__c = true;
		  c.SecLvlVal__c = true;
			//c.SecLvlVal__c = true; 
			System.debug('Alerting on AcscSecLvlPickup Trigger  --------------> '+c.ACSC_Second_Level_Picked_Up__c);
		}
		}

	}
}

 

davidjgriffdavidjgriff

A few things are wrong here.

 

1. You have a SOQL query within a loop. That's your query limit problem right there.

2. You have a DML statement (update tsk;) also in that loop.

3. Your trigger only fires in "before update" situations, so your "if(trigger.isUpdate)" will always return true and your else block will never be executed.

 

What you probably want to do is go through the Cases in trigger.new to determine if they meet your criteria for task closing (line 14 currently), collect the Ids of the ones that do in a Set, then query for the tasks associated with those cases that meet your task criteria. Then you can just loop through those, close them all and update the whole list.

 

Make sense?

Vinit_KumarVinit_Kumar

To add to David's info,you are Hardcoding RecordTypeId which is not recommended at all.

 

Infact you should be querying on RecordType object to get the Id and then use it in your IF condition.