In attempting to "Bulkify" my trigger, it was suggested that I use Schema for getting RecordTypeId. As I'm not really a developer (I'm just taking a stab at updating the code that was created by a developer no longer with our company), can anyone help me out with this?


Is my Trigger properly "Bulkified"?

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;
  • ytiq has a suggestion for you on their answer to the previous question (at the end). – David Reed Jun 18 '19 at 19:09

When "bulkifying" things, the other question correctly pointed you in the right direction - you want all of your database operations outside of loops, and dealing with Lists or Maps, as you are. The Schema reference I believe is so that you can be rid of hardcoding Ids. The documentation is here.

Schema.SObjectType.<Object Name>.getRecordTypeInfosByName().get('<Record Type Name>').getRecordTypeId() will get you the Id of the RecordType dynamically, rather than relying on it always being the hardcoded value. So if I have a Contact object, and want the Dealer RecordTyped contacts, it would be Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Dealer').getRecordTypeId() in place of all your hardcoded IDs. Better yet, store that as a String and use it repeatedly, e.g.:

String dealerRecordType = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Dealer').getRecordTypeId();
if(a.RecordTypeId != dealerRecordType) {
// do stuff with non-dealers here
| improve this answer | |

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.