+ Start a Discussion
Jim GentileJim Gentile 

What does this trigger do?

All,

I would like to ask for help determining what the following trigger does.  I inherited an instance and trying to break down some of the code.  I just can't figure this one out (probably because I don't know Apex).  There is a description half way through the code, but I'm wondering if that description includes the entire code.

Can you help me out with a brief explanation?


public without sharing class LeadTriggerHandler {
    
    public void onAfterUpdate(Lead[] newleads){
        //update MQL records
        updateMQLRecord(newleads);
        

    }
    
    public void onBeforeInsert(Lead[] newleads){
       for(Lead l :newleads){
            if(l.LeadSource == 'Inbound Sales'){
                l.Status = 'Marketing Qualified Lead';
            }
       }
    }
    
    public void onAfterInsert(Lead[] newleads){
      
      List<MQL_Record__c> mqlRecordsToInsert = new List<MQL_Record__c>();
      //Update Inbound Sales LEads to MQL
       for(Lead l :newleads){
            if(l.LeadSource == 'Inbound Sales'){
                MQL_Record__c InboundMQL = new MQL_Record__c();
        InboundMQL.Lead__c = l.id;
        InboundMQL.MQL_Date__c = system.now();
        InboundMQL.Lead_Source_Most_Recent__c = l.LeadSource;
        InboundMQL.Lead_Source_Original__c = l.LeadSource;
        mqlRecordsToInsert.add(InboundMQL);
            }
        }
      
      System.Debug('mqlRecordsToInsert' + mqlRecordsToInsert.size());
      insert mqlRecordsToInsert;
    }
    
    
    private void updateContactRType(Lead[] newleads){
        Map<string,id> map_contactRecordTypes = new Map<string,id>();
        Set<id> convertedContactIds = new Set<id>();
        List<Contact> contactsToBeUpdated = new List<Contact>();
        for(Lead l :newleads){
            if(l.IsConverted){
                convertedContactIds.add(l.ConvertedContactId);
            }
        }
        for(RecordType iterating_RecordType : [SELECT Id, Name, DeveloperName FROM RecordType WHERE sObjectType='Contact'])
        {
            map_contactRecordTypes.put(iterating_RecordType.DeveloperName, iterating_RecordType.Id);
        }
        for(List<Contact> c_List : [SELECT id, RecordTypeId from contact where id in:convertedContactIds]){
            for(contact c :c_List){
                c.RecordTypeId = map_contactRecordTypes.get('Sales_RT');
                contactsToBeUpdated.add(c);
            }
        }
        
        if(contactsToBeUpdated != null && contactsToBeUpdated.size()>0)
            update contactsToBeUpdated ;
    }
    

    
    /*
     * Description : To update the following fields in the MQL record when a lead is updated
        - Contact Id
        - Vertical Team
        - Country
        - Lead Source Most Recent
        - Import Source Most Recent
        - Lead Source Original (if blank)
        - Import Source Original (if blank)
    */
    private void updateMQLRecord(Lead[] newleads){
      
      
      //For Inbound Sales Lead process, we need to see if an MQL exists in the past 180 days, and if not, create one.
      //Eloqua is too slow to create the MQL, and it may get lost
      List<MQL_Record__c> mqlRecordsToUpsert = new List<MQL_Record__c>();
      
      //List to Map
      Map<Id, Lead> mapOfLeads = new Map<Id, Lead>();
      mapOfLeads.putAll(newleads);
        
        //Identify the MQL records related to the leads.
        Map<Id, MQL_Record__c> mapOfMQLRecords = new Map<Id, MQL_Record__c>([Select Id, MQL_Record__c.Contact__c,MQL_Record__c.MQL_Date__c,MQL_Record__c.Converted_Date__c,MQL_Record__c.Direct_Sales_Opp__c,MQL_Record__c.Is_Converted__c,MQL_Record__c.Opportunity__c,MQL_Record__c.Lead__c,MQL_Record__c.Lead_Source_Original__c,MQL_Record__c.Import_Source_Original__c from MQL_Record__c where Lead__c in :newleads]);
        
        //create a map of Lead Id to Most Recent MQL
        Map<Id, MQL_Record__c>  mapOfLeadIdToMQL = new Map<Id, MQL_Record__c>();
        for(MQL_Record__c mqlRec : mapOfMQLRecords.values()){
            if(!mapOfLeadIdToMQL.containsKey(mqlRec.Lead__c))
                mapOfLeadIdToMQL.put(mqlRec.Lead__c, mqlRec);
            else{
              MQL_Record__c prevMQL = mapOfLeadIdToMQL.get(mqlRec.Lead__c);
              if(prevMQL.MQL_Date__c < mqlRec.MQL_Date__c){
                mapOfLeadIdToMQL.remove(mqlRec.Lead__c);
                mapOfLeadIdToMQL.put(mqlRec.Lead__c, mqlRec);
              }
            }
        }
        
        //update MQL records based on the lead values
        
        for(MQL_Record__c mqlRec : mapOfLeadIdToMQL.values()){
          Lead MQLdLead = mapOfLeads.get(mqlRec.Lead__c);
            if (MQLdLead.IsConverted && (MQLdLead.Industry != 'LM-Public Sector' || MQLdLead.Industry != 'LM-Enterprise')) {
              //Update Lead Source - Original, Lead Source - Most Recent, Import Source - Original, Import Source - Most Recent
              if (mqlRec.Lead_Source_Original__c == null || mqlRec.Lead_Source_Original__c == ''){
                mqlRec.Lead_Source_Original__c = MQLdLead.LeadSource;
              }
              if(mqlRec.Import_Source_Original__c == null || mqlRec.Import_Source_Original__c == ''){
                mqlRec.Import_Source_Original__c = MQLdLead.Import_Source__c;
              }
              mqlRec.Lead_Source_Most_Recent__c = MQLdLead.LeadSource;
              mqlRec.Import_Source_Most_Recent__c = MQLdLead.Import_Source__c;
                mqlRec.Contact__c         = MQLdLead.ConvertedContactId;
                mqlRec.Vertical_Team__c   = MQLdLead.Industry;
                mqlRec.Country__c         = MQLdLead.Country;
                mqlRecordsToUpsert.add(mqlRec);
            }
        } 
        //update records
        Database.upsert(mqlRecordsToUpsert,true);
    }
}
 
David ZhuDavid Zhu
This is a class containing methods called by a trigger. You should be able to find the trigger(s) at Setup | Build |Customize | Leads | Triggers.

Basically, the mothod name tells you when it is called.  They could be used in one single trigger or different triggers on Lead object.

onAfterUpdate -- called by After Update trigger on Lead object
onAfterInsert - called by After Insert trigger  on Lead object
onBeforeInsert - called by Before Insert trigger  on Lead object

updateContactRType -- I don't see it is being called. It is private method and won't be able to called out side of this class.

Other methods from half way down have clear descriptions.