+ Start a Discussion
Soundar Raj PonpandiSoundar Raj Ponpandi 

How to get manager email via MAP

HI, 

I have created one batch class, This is sending a no activity reminder to record owner CC with manager.

Right now i am getting a manager email by additional field, GD_Sales_Manager__c

But i want to take this manager email via Query only. But it should not inside of the for loop.

Can you please guide me in a proper way, Please review the below code.
global class GD_NoActivityReminder implements Database.Batchable<sObject>, Database.Stateful{
    
    list<GD_Lead__c> leadListToUpdate_Task = new list<GD_Lead__c>();
    list<GD_Lead__c> leadListToUpdate_Event = new list<GD_Lead__c>();
    list<GD_Lead__c> leadListToUpdate_ET = new list<GD_Lead__c>();
    GD_No_Activity_Reminder__mdt reminderDays = [select id,GD_Days__c from GD_No_Activity_Reminder__mdt Limit 1];     
    Integer days =integer.valueOf(reminderDays.GD_Days__c);
    global final String leadQuery ='Select Id,OwnerId,Name,GD_Lead_Status__c,Owner.Email,GD_Sales_Manager_Email__c  from GD_Lead__c where GD_Lead_Status__c != \'Converted\' and GD_Lead_Status__c != \'Unqualified\' and GD_Lead_Status__c != \'Retired\' LIMIT 2';
    set<id> leadId = new set<id>();
    List<GD_Lead__c> noDupes;
    
    global Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator(leadQuery); 
    }
    
    global void execute(Database.BatchableContext BC,List<GD_Lead__c> scope){
        Map<Id,GD_Lead__c> originalLeadMap = new Map<Id,GD_Lead__c>(scope);
        Map<Id,List<Task>> taskByLead = new Map<Id,List<Task>>();
        Map<Id,List<Event>> eventByLead = new Map<Id,List<Event>>();
        Date d = System.today() - days;
        
        //Task
        for(Task t : [select id,status,lastModifiedDate, WhatId from Task where WhatId =:originalLeadMap.keySet() and status != 'Completed' and LastModifiedDate <:d]){
            if(taskByLead.containsKey(t.WhatId)){
                taskByLead.get(t.WhatId).add(t);
            }else{
                taskByLead.put(t.WhatId,new List<Task>{t});
            }
        }        
        
        for(Id leadId : originalLeadMap.keySet()){
            if(/*!*/taskByLead.containsKey(leadId)){
                leadListToUpdate_Task.add(originalLeadMap.get(leadId));
            }
        }
        
        //Event
        for(Event e : [select id,WhatId,endDate from Event where WhatId =:originalLeadMap.keySet()  and EndDate <:d /*days*/]){
            system.debug('Entered ***'  );
            if(eventByLead.containsKey(e.WhatId)){
                eventByLead.get(e.WhatId).add(e);
            }else{
                eventByLead.put(e.WhatId,new List<Event>{e});
            }
        }
        
        for(Id leadId : originalLeadMap.keySet()){
            if(/*!*/eventByLead.containsKey(leadId)){
                leadListToUpdate_Event.add(originalLeadMap.get(leadId));
            }
        }
        
        leadListToUpdate_ET.addAll(leadListToUpdate_Task);
        leadListToUpdate_ET.addAll(leadListToUpdate_Event);
        //Remove The Duplicate Records
        noDupes = new List<GD_Lead__c>(new Set<GD_Lead__c>(leadListToUpdate_ET));
        
        map<id, user> ownerId = new map<id, user>([select id,name from user]);
        
    }
    
    global void finish(Database.BatchableContext BC){  
        EmailTemplate template = [select id,body from EmailTemplate where DeveloperName='GD_Lead_No_Activity_Email_Notification_Template' limit 1];
        contact con = [select id, Email from contact where email <> null limit 1];
        List<Messaging.SingleEmailMessage> lstMsgs = new List<Messaging.SingleEmailMessage>();
        for(GD_Lead__c leadRecord : noDupes){
            list<string> ownerEmail = new list<string>();
            list<string> cCEmails = new list<string>();
            ownerEmail.add(leadRecord.owner.email);
            cCEmails.add(leadRecord.GD_Sales_Manager_Email__c);
            Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
            message.setTreatTargetObjectAsRecipient(false);
            message.setSaveAsActivity(false); 
            message.setTemplateId(template.id);
            message.setWhatId(leadRecord.id);
            message.setTargetObjectId(con.id); 
            message.setToAddresses(ownerEmail);
            message.setCcAddresses(cCEmails);
            lstMsgs.add(message);
        }
        
        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);   
    }
}

Thanks,
Soundar.