I have a trigger on the Contact object that populates lookup fields from a Custom object (Territory_Reference__c). I'm finding that since this trigger has been put into place, Apex CPU Timeout errors are more common across the system. Is the trigger below properly "bulkified"? Is there anything that can be done to clean it up a bit?

Thank you in advance!

trigger ContactGetTerritoryReference on Contact (before insert, before update) {
    Map<String,Territory_Reference__c> Terr2Terr_SA = new Map<String,Territory_Reference__c>();
    Map<String,Territory_Reference__c> Terr2Terr_DTT = new Map<String,Territory_Reference__c>();
    Map<String,Territory_Reference__c> Terr2Terr_OC = new Map<String,Territory_Reference__c>();
    //Map<String,Contact> Contact_RecordTypeID = new Map<String,Contact>(); 

    Set<String> SA = new Set<String>();   
    for(Contact a:Trigger.new){
        //filtering out dealer contact record type
        if (a.RecordTypeID !='012440000002T6J' && a.Account_Record_Type__c == 'Agency Establishment'){
            //something new
            //a.Agency_Name__c = a.Agency__c; // the default agency name will be the SA
        else if (a.Account_Record_Type__c != 'Agency Establishment' ){
            a.VOSS_Manager__c = null;
            a.NAIS_VSEN_Mgr__c = null;
            a.CBM__c = null;
            a.PSE__c = null; 
            a.RVP__c = null;
            a.MTSS__c = null; 
            a.Inside_Sales_Representative__c = null;
            a.VPSE__c = null;

    for (Territory_Reference__c t : [SELECT MTSS__c, AGENCY_DTT__c, NAIS_VOSS_Mgr__c, VSEN__c, NAIS_VSEN_Mgr__c, AGENT_DTT__c, AGENCY__c, AGENCY_ID__c, SA_DTT_KEY__c, VOSS__c, PSE__c, PSM__c, MSO__c, RVP__c, Area_Title__c, REGION__c, RMOS__c, ASD__c, CBM__c, VPSE__c, VSPM__c FROM Territory_Reference__c WHERE AGENCY_ID__c IN : SA]){

    if (!Terr2Terr_SA.isEmpty()){
        for(Contact a:Trigger.new){
            if (Terr2Terr_DTT.get(a.Dtt__c) <> null && a.RecordTypeID !='012440000002T6J' && a.Account_Record_Type__c == 'Agency Establishment' && a.Establishment_Type__c != 'Open Coverage'){
                Territory_Reference__c  t2t = Terr2Terr_DTT.get(a.Dtt__c);
                //a.Agency_Name__c = t2t.AGENCY__c;
                a.Inside_Sales_Representative__c = t2t.VSEN__c;
                a.VPSE__c = t2t.VPSE__c;

            if (Terr2Terr_OC.get(a.Dtt__c) <> null && a.RecordTypeID !='012440000002T6J' && a.Account_Record_Type__c == 'Agency Establishment' && a.Establishment_Type__c == 'Open Coverage'){
                Territory_Reference__c  t2t = Terr2Terr_OC.get(a.Dtt__c);
                //a.Agency_Name__c = t2t.AGENCY__c;
                a.Inside_Sales_Representative__c = t2t.VSEN__c;
                a.VPSE__c = t2t.VPSE__c;

            if (Terr2Terr_SA.get(a.Agency__c)<>null && a.RecordTypeID !='012440000002T6J' && a.Account_Record_Type__c == 'Agency Establishment'){
                Territory_Reference__c  t2t = Terr2Terr_SA.get(a.Agency__c);
                //a.Agency_Name__c = t2t.AGENCY__c;  

                a.VOSS_Manager__c = t2t.NAIS_VOSS_Mgr__c;
                a.NAIS_VSEN_Mgr__c = t2t.NAIS_VSEN_Mgr__c;
                a.CBM__c = t2t.MSO__c;
                a.PSE__c = t2t.PSE__c;
                //a.PSM__c = t2t.PSM__c;
                //a.RVP__c = t2t.RMOS__c;
                //a.VOSS__c = t2t.VOSS__c;
                a.RVP__c = t2t.RVP__c;
                a.MTSS__c = t2t.MTSS__c;
                a.Establishment_SA_Number__c = a.AGENCY__c;
                a.Establishment_Dtt__c = a.DTT__c;
                a.Establishment_Record_Type__c = a.Account_Record_Type__c;
                a.Establishment_Type2__c = a.Establishment_Type__c;
                //a.Xerox_Entity__c = t2t.REGION__c;
                //a.Area_Title_t2t__c = t2t.Area_Title__c;      
                //a.ASD__c = t2t.ASD__c;
                //a.CBM__c = t2t.CBM__c;
                //a.VSPM__c = t2t.VSPM__c;

Yes, you can consider your code bulkified.

  1. You have only 1 SOQL outside of loop.
  2. You don't have any DMLs
  3. I don't see any method invocations with potentials DML/SOQL.
  4. You iterate over trigger.new collection

But I would give a few pieces of advice on how you can improve your code. I'll mostly reference Salesforce docs, but still.

  1. Rename your Trigger to something general like 'ContactTrigger' or just 'Contact' since you want to have only 1 Trigger per SObject
  2. Create a Helper class and put there all your logic in a separate method
  3. Add switch statement on Trigger.operationType. Use can use something like this for your code design.

Good luck with your coding.


use Schema for getting RecordTypeId. Schema.SObjectType.Countact.getRecordTypeInfosByDeveloperName().get('Some_Name').getRecordTypeId()

  • I'd advise against the "ObjectTrigger" naming, because the code will be separated from conventional classes anyway, so one can name the trigger exactly the same as the object. I'd name it just "Contact". The trigger file would be "Contact.trigger" instead of "ContactTrigger.trigger". – Renato Oliveira Jun 18 '19 at 15:07
  • true, just my personal preference – ytiq Jun 18 '19 at 15:28
  • @RenatoOliveira, updated answer with your suggestion – ytiq Jun 18 '19 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.