• Harsh singh 44
  • NEWBIE
  • 0 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 1
    Likes Given
  • 0
    Questions
  • 2
    Replies
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.
How can I delete all the records of a custom object in Developer Edition?