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
Keith Stephens 18Keith Stephens 18 

Need help to bulkify trigger

Hello All,
I am new to Salesforce development, so I hope someone can help me.
I need to get this trigger bulkified, but I do not know how, this trigger was written by someone else who no longer works for the company.
trigger ProcedureRateUpdates on Procedure__c (before update, before insert) {
	// if triggers have been globally disabled via TriggerCustomSettings setting, then exit
	if (TriggerSettings.areTriggersDisabled()) return;
	
	List<AggregateResult> centerRateCount = null;
	List<AggregateResult> acctRateCount = null;
	Integer maxCtrRates = 0;
	Integer maxAcctRates = 0;
	Id centerId = null;
	Boolean isManualProcPricing = false;
	
	
	// Set<Id> procIds = trigger.newMap.keySet();
	// List<Procedure_c> procs = [SELECT Id,  FROM Procedure__c 
	//						   WHERE Id IN :procIds];
					   
	for (Procedure__c proc: trigger.new) {
		if (proc.Override_Procedure_Rate__c == false) {
			// get the Main Center ID for the current procedure
			try {
				centerId = [SELECT Main_Center__c FROM Center__c WHERE Id = :proc.Center__c][0].Main_Center__c;
			} catch (Exception e) {
				proc.addError('Unable to determine Main Center. Ensure that this procedure has a Center selected.');
				continue;
			}
			// determine if Manual Procedure Pricing checkbox is checked
			isManualProcPricing = [SELECT Manual_Procedure_Pricing__c FROM Main_Center__c WHERE Id = :centerId][0].Manual_Procedure_Pricing__c;
			if (isManualProcPricing == false) {
				// only process the costs if entering a new procedure or updating an existing one that has a blank amount and cost
				// or updating an existing one only if the Procedure Status is not 'OK to Pay' or 'Paid')
				if (Trigger.isInsert || (Trigger.isUpdate && proc.Amount_To_Pay__c == NULL && proc.Procedure_Cost__c == NULL) || 
					(Trigger.isUpdate && (proc.Procedure_Status__c != 'OK to Pay' && proc.Procedure_Status__c != 'Paid' &&
					   (proc.Amount_To_Pay__c != NULL && proc.Procedure_Cost__c != NULL)))) {
					// get count of Center Procedure Rates that match current procedure
					centerRateCount = 
						[SELECT COUNT(Id) maxRates FROM Center_Procedure_Rate__c 
						 WHERE CPT_Code__c = :proc.CPT_Code__c AND Center__c = :proc.Center__c];
						 
					maxCtrRates = centerRateCount[0].get('maxRates') == null ? 0 : (Integer)centerRateCount[0].get('maxRates');
						
					if (maxCtrRates == 0) {
						System.debug('proc.Center__c = ' + proc.Center__c);
						// no rates found in Center Procedure Rate object, so look at Account Procedure Rate object for rate
						acctRateCount = [SELECT COUNT(Id) maxRates FROM Account_Procedure_Rate__c 
							 			 WHERE CPT_Code__c = :proc.CPT_Code__c AND Main_Center__c = :centerId];
						maxAcctRates = acctRateCount[0].get('maxRates') == null ? 0 : (Integer)acctRateCount[0].get('maxRates');
						
						if (maxAcctRates == 0) { 
							proc.addError('No rates found for this procedure.');
						} else if (maxAcctRates == 1) {
							Account_Procedure_Rate__c rate = [SELECT Amount_To_Pay__c, Procedure_Cost__c FROM Account_Procedure_Rate__c 
														 	 WHERE  CPT_Code__c = :proc.CPT_Code__c AND Main_Center__c = :centerId];
							proc.Amount_To_Pay__c = rate.Amount_To_Pay__c;
							proc.Procedure_Cost__c = rate.Procedure_Cost__c;
						} else {
							proc.addError('More than one procedure rate for the main center was found.');
						}
					} else if (maxCtrRates == 1) {
						Center_Procedure_Rate__c rate = [SELECT Amount_To_Pay__c, Procedure_Cost__c FROM Center_Procedure_Rate__c 
														 WHERE  CPT_Code__c = :proc.CPT_Code__c AND Center__c = :proc.Center__c];
						proc.Amount_To_Pay__c = rate.Amount_To_Pay__c;
						proc.Procedure_Cost__c = rate.Procedure_Cost__c;
					} else {
						proc.addError('More than one procedure rate for the center was found.');
					}
				}
			}
		}
	} 
}



Thanks,
Keith.
Harsh singh 44Harsh singh 44
To make your trigger bulkified, follow below points.
1. Use the helper class 
2. Avoid writing the SOQL query inside the for loop.
3. Use custom label so as to show the error.
Harsh singh 44Harsh singh 44
To make your trigger bulkified, follow below points.
1. Use the helper class 
2. Avoid writing the SOQL query inside the for loop use the list/set/map to avoid the SOQL.
3. Use custom label so as to show the error.
Keith Stephens 18Keith Stephens 18
Thanks, but as stated I am new to salesforce I have not touched or seen it until now,
What is list/set/map?  Do you mean SQL when you say SOQL?
Keith.