+ Start a Discussion
Soundar Raj PonpandiSoundar Raj Ponpandi 

Help For Code Optimization

HI,

I am sending a escalation email based on Level and few conditions. Same logics are repeated with few changes.

1. For level 1 - sending to two persons 

2. For Level 2 - sending to three persons.

3. For Level 3 - Sending to four persons.

Can you please check my code and reduce the code by using Map.
 
global class GD_LeadEscalation implements Database.Batchable<sObject>,Database.Stateful {
    
    //Level 1
    GD_Lead_Escalation_Setup__mdt escMonitor1 = [select id,GD_Escalation_Emails__c,GD_Escalation_Days__c,GD_Level_Of_Escalation__c from GD_Lead_Escalation_Setup__mdt where GD_Level_Of_Escalation__c = 'Level 1'];
    string level1Days = escMonitor1.GD_Escalation_Days__c;
    List<GD_Lead__c> firstLevelEscalation = [Select Id,Name,GD_Assigned_To_Email__c,GD_Level_3_Manager__c,GD_Level_2_Manager__c,GD_Level_1_Manager__c,GD_Sales_Rep_Name__c,Lead_Unique_Number__c,GD_Lead_Priority__c,GD_Lead_Status__c,GD_Revenue__c,GD_Expected_Opportunity_Date__c,GD_First_Expected_Opportunity_Date__c,GD_Organisation__c,GD_Territory__c,GD_Department__c,GD_Business_Unit__r.name,GD_Account__r.Name,GD_Parent_Account__c,GD_Contact__r.name,GD_Escalated__c 
                                             from GD_Lead__c where GD_Escalated__c =: Decimal.valueOf(level1Days) and GD_Stop_Escalation__c = false and (GD_Lead_Status__c != 'Converted' OR GD_Lead_Status__c != 'Unqualified')];
    
    //Level 2
    GD_Lead_Escalation_Setup__mdt escMonitor2 = [select id,GD_Escalation_Emails__c,GD_Escalation_Days__c,GD_Level_Of_Escalation__c from GD_Lead_Escalation_Setup__mdt where GD_Level_Of_Escalation__c = 'Level 2'];
    string level2Days = escMonitor2.GD_Escalation_Days__c;
    List<GD_Lead__c> secondLevelEscalation = [Select Id,Name,GD_Assigned_To_Email__c,GD_Level_3_Manager__c,GD_Level_2_Manager__c,GD_Level_1_Manager__c,GD_Sales_Rep_Name__c,Lead_Unique_Number__c,GD_Lead_Priority__c,GD_Lead_Status__c,GD_Revenue__c,GD_Expected_Opportunity_Date__c,GD_First_Expected_Opportunity_Date__c,GD_Organisation__c,GD_Territory__c,GD_Department__c,GD_Business_Unit__r.name,GD_Account__r.Name,GD_Parent_Account__c,GD_Contact__r.name,GD_Escalated__c 
                                              from GD_Lead__c where GD_Escalated__c =: Decimal.valueOf(level2Days) and GD_Stop_Escalation__c = false and (GD_Lead_Status__c != 'Converted' OR GD_Lead_Status__c != 'Unqualified')];
    
    //Level 3
    GD_Lead_Escalation_Setup__mdt escMonitor3 = [select id,GD_Escalation_Emails__c,GD_Escalation_Days__c,GD_Level_Of_Escalation__c from GD_Lead_Escalation_Setup__mdt where GD_Level_Of_Escalation__c = 'Level 3'];
    string level3Days = escMonitor3.GD_Escalation_Days__c;
    List<GD_Lead__c> thirdLevelEscalation = [Select Id,Name,GD_Assigned_To_Email__c,GD_Level_3_Manager__c,GD_Level_2_Manager__c,GD_Level_1_Manager__c,GD_Sales_Rep_Name__c,Lead_Unique_Number__c,GD_Lead_Priority__c,GD_Lead_Status__c,GD_Revenue__c,GD_Expected_Opportunity_Date__c,GD_First_Expected_Opportunity_Date__c,GD_Organisation__c,GD_Territory__c,GD_Department__c,GD_Business_Unit__r.name,GD_Account__r.Name,GD_Parent_Account__c,GD_Contact__r.name,GD_Escalated__c 
                                             from GD_Lead__c where GD_Escalated__c =: Decimal.valueOf(level3Days) and GD_Stop_Escalation__c = false and (GD_Lead_Status__c != 'Converted' OR GD_Lead_Status__c != 'Unqualified')];
    
    //Dummy Query for start locator
    global final String query_leadEsc1 ='Select id from GD_Lead__c where GD_Lead_Status__c != \'Converted\'';
    
    global Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator(query_leadEsc1); 
    }
    
    /* Execute Method */
    global void execute(Database.BatchableContext BC,List<GD_Lead__c> scope){
        //for(GD_Lead__c lead : scope){}
    }
    
    /* Finish Method */
    global void finish(Database.BatchableContext BC){   
        //Level 1 Escalation
        system.debug('firstLevelEscalation.size() ***'  + firstLevelEscalation.size());
        if(firstLevelEscalation.size() > 0 && firstLevelEscalation.size() != null){ // Check Empty
            EmailTemplate template = [select id from EmailTemplate where DeveloperName='GD_Lead_Escalation_Template' limit 1];
            for(GD_Lead__c lead : firstLevelEscalation){
                //Dummy Contact for Email Template
                //Contact con = [select id, Email from Contact where email <> null limit 1];
                List<Messaging.SingleEmailMessage> lstMsgs = new List<Messaging.SingleEmailMessage>();
                Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage();
                msg.setTemplateId(template.id);
                msg.setWhatId(lead.id);  /// Check without whatId
                //msg.setTargetObjectId(con.id); 
                String[] repEamail = new String[] {lead.GD_Assigned_To_Email__c};
                    String[] ccAddresses = new String[] {lead.GD_Level_1_Manager__c};
                        system.debug('ccAddresses' + ccAddresses);
                //msg.setToAddresses(new List<String>{repEamail});
                msg.setToAddresses(repEamail); 
                msg.setCcAddresses(ccAddresses);
                lstMsgs.add(msg);
                system.debug('lstMsgs' + lstMsgs);
                Savepoint sp = Database.setSavepoint();
                Messaging.sendEmail(lstMsgs);
                Database.rollback(sp);
                List<Messaging.SingleEmailMessage> lstMsgsToSend = new List<Messaging.SingleEmailMessage>();
                for (Messaging.SingleEmailMessage email : lstMsgs) {
                    Messaging.SingleEmailMessage emailToSend = new Messaging.SingleEmailMessage();
                    emailToSend.setToAddresses(email.getToAddresses());
                    emailToSend.setCcAddresses(email.getccAddresses());
                    emailToSend.setPlainTextBody(email.getPlainTextBody());
                    emailToSend.setHTMLBody(email.getHTMLBody());
                    emailToSend.setSubject(email.getSubject());
                    lstMsgsToSend.add(emailToSend);
                }
                Messaging.sendEmail(lstMsgsToSend);
                
            }
        }
        
        //Level 2 Escalation
        system.debug('secondLevelEscalation.size() ***'  + secondLevelEscalation.size());
        if(secondLevelEscalation.size() > 0 && secondLevelEscalation.size() != null){
            for(GD_Lead__c lead : secondLevelEscalation){
                //Dummy Contact for Email Template
                Contact con = [select id, Email from Contact where email <> null limit 1];
                List<Messaging.SingleEmailMessage> lstMsgs = new List<Messaging.SingleEmailMessage>();
                Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage();
                EmailTemplate template = [select id from EmailTemplate where DeveloperName='GD_Lead_Escalation_Template' limit 1];
                msg.setTemplateId(template.id);
                msg.setWhatId(lead.id);
                msg.setTargetObjectId(con.id); 
                String[] repEamail = new String[] {lead.GD_Assigned_To_Email__c};
                    String[] ccAddresses = new String[] {lead.GD_Level_1_Manager__c, lead.GD_Level_2_Manager__c};
                        msg.setToAddresses(repEamail); 
                msg.setCcAddresses(ccAddresses);
                lstMsgs.add(msg);
                Savepoint sp = Database.setSavepoint();
                Messaging.sendEmail(lstMsgs);
                Database.rollback(sp);
                List<Messaging.SingleEmailMessage> lstMsgsToSend = new List<Messaging.SingleEmailMessage>();
                for (Messaging.SingleEmailMessage email : lstMsgs) {
                    Messaging.SingleEmailMessage emailToSend = new Messaging.SingleEmailMessage();
                    emailToSend.setToAddresses(email.getToAddresses());
                    emailToSend.setCcAddresses(email.getccAddresses());
                    emailToSend.setPlainTextBody(email.getPlainTextBody());
                    emailToSend.setHTMLBody(email.getHTMLBody());
                    emailToSend.setSubject(email.getSubject());
                    lstMsgsToSend.add(emailToSend);
                }
                Messaging.sendEmail(lstMsgsToSend);
                
            }
        }
        
        //Level 3 Escalation
        system.debug('thirdLevelEscalation.size() ***'  + thirdLevelEscalation.size());
        if(thirdLevelEscalation.size() > 0 && thirdLevelEscalation.size() != null){
            for(GD_Lead__c lead : thirdLevelEscalation){
                //Dummy Contact for Email Template
                Contact con = [select id, Email from Contact where email <> null limit 1];
                List<Messaging.SingleEmailMessage> lstMsgs = new List<Messaging.SingleEmailMessage>();
                Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage();
                EmailTemplate template = [select id from EmailTemplate where DeveloperName='GD_Lead_Escalation_Template' limit 1];
                msg.setTemplateId(template.id);
                msg.setWhatId(lead.id);
                msg.setTargetObjectId(con.id); 
                String[] repEamail = new String[] {lead.GD_Assigned_To_Email__c};
                    String[] ccAddresses = new String[] {lead.GD_Level_1_Manager__c,lead.GD_Level_2_Manager__c,lead.GD_Level_3_Manager__c};
                        msg.setToAddresses(repEamail); 
                msg.setCcAddresses(ccAddresses);
                lstMsgs.add(msg);
                Savepoint sp = Database.setSavepoint();
                Messaging.sendEmail(lstMsgs);
                Database.rollback(sp);
                List<Messaging.SingleEmailMessage> lstMsgsToSend = new List<Messaging.SingleEmailMessage>();
                for (Messaging.SingleEmailMessage email : lstMsgs) {
                    Messaging.SingleEmailMessage emailToSend = new Messaging.SingleEmailMessage();
                    emailToSend.setToAddresses(email.getToAddresses());
                    emailToSend.setCcAddresses(email.getccAddresses());
                    emailToSend.setPlainTextBody(email.getPlainTextBody());
                    emailToSend.setHTMLBody(email.getHTMLBody());
                    emailToSend.setSubject(email.getSubject());
                    lstMsgsToSend.add(emailToSend);
                }
                Messaging.sendEmail(lstMsgsToSend);
                
            }
        }
    }
    }

Regards,
Soundar Rajan.​​​​​​​