You need to sign in to do that
Don't have an account?
Keith 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.
Thanks,
Keith.
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.
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.
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.
What is list/set/map? Do you mean SQL when you say SOQL?
Keith.