• Ganesh Vinnakota
  • NEWBIE
  • 0 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
Hello,
I am trying to build a batch that can be scheduled daily and sends email to all the lead owners if they have any past due date leads.
Each owner should get their own email with an attachment containing only leads assigned to them.
My code below is failing to get an attachment containing only leads assigned to them.
Can any one help me?

global class BatchableLeadEmailAlert  implements Database.Batchable<sObject>, Database.AllowsCallOuts, Database.Stateful {
    global   String  query;
    String YES = 'Yes';   
    //  ---------------------------------------------------------------------
    //  CONSTRUCTOR
    //  ---------------------------------------------------------------------
    global BatchableLeadEmailAlert() {
      query = 'SELECT Id,Name,FirstName,LastName,Phone,Secondary_Lead_Owner__c from Lead where Secondary_Lead_Owner__c != null AND Test_Record__c = \''+YES+'\'';                    
    }    
    //  ---------------------------------------------------------------------
    //  INTERFACE METHOD: start
    //  ---------------------------------------------------------------------
    global Database.QueryLocator start(Database.BatchableContext bc){
        return Database.getQueryLocator(query);
    }
    
    //  ---------------------------------------------------------------------
    //  INTERFACE METHOD: execute 
    //  ---------------------------------------------------------------------
    global void execute(Database.BatchableContext bc, List<sObject> scope) {
    
        Set<Id> userid = new Set<Id>();
        Set<Id> LdId = new Set<Id>();
        for (SObject s: scope) {    
            Lead LdsIds = (Lead) s;  
            LdId.add(LdsIds.Id);
            userid.add(LdsIds.Secondary_Lead_Owner__c);                            
          }     
                    
             List<User> Addresses = new List<User>();
             Addresses = [select Email,LastName from User where Id =: userid];
              List<String> toAddresses = new List<String>();              
              for(User usr: Addresses){
                 toAddresses.add(usr.Email);
              }      
    
          List<Lead> leadlist = [SELECT Id,Name,FirstName,LastName,Phone,Secondary_Lead_Owner__c from Lead where Id =:LdId ORDER BY Secondary_Lead_Owner__c];         
                   
          String header = 'Name, First Name, Last Name, Phone,Secondary_Lead_Owner__c \n';
          String finalstr = header;           
          map<Id, String> userleadsmap = new map<Id, String>();

        for(Lead a: leadlist)
        {                     
               string recordString = a.Name+','+a.FirstName+','+a.LastName+','+a.Phone+','+a.Secondary_Lead_Owner__c+'\n';
               finalstr = finalstr +recordString;               
               userleadsmap.put(a.Secondary_Lead_Owner__c, finalstr);                           
        }             
         List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
            for(User portalUser :Addresses) {
                    Messaging.EmailFileAttachment csvAttc = new Messaging.EmailFileAttachment();
                    if(userleadsmap.containskey(portalUser.Id)){ 
                    blob csvBlob = Blob.valueOf(userleadsmap.get(portalUser.Id));                    
                    string csvname= 'Lead.csv';
                    csvAttc.setFileName(csvname);
                    csvAttc.setBody(csvBlob);
                    }
                    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                    string body = 'Hi '+ portalUser.LastName;
                    String[] ccAddresses = new list<string> {'gvinnakota@ten-x.com'}; 
                    mail.setCcAddresses( ccAddresses );
                    mail.setSubject('Test Subject');
                    mail.setTargetObjectId(portalUser.Id); 
                    mail.setSaveAsActivity(false);
                    mail.setHtmlBody(body); mails.add(mail);
                    mail.setFileAttachments(new Messaging.EmailFileAttachment[]{csvAttc});
               }
            Messaging.sendEmail(mails);
             }

   global void finish(Database.BatchableContext bc){       }
}
 
Hello,
I am trying to build a batch that can be scheduled daily and sends email to all the lead owners if they have any past due date leads.
Each owner should get their own email with an attachment containing only leads assigned to them.
My code below is failing to get an attachment containing only leads assigned to them.
Can any one help me?

global class BatchableLeadEmailAlert  implements Database.Batchable<sObject>, Database.AllowsCallOuts, Database.Stateful {
    global   String  query;
    String YES = 'Yes';   
    //  ---------------------------------------------------------------------
    //  CONSTRUCTOR
    //  ---------------------------------------------------------------------
    global BatchableLeadEmailAlert() {
      query = 'SELECT Id,Name,FirstName,LastName,Phone,Secondary_Lead_Owner__c from Lead where Secondary_Lead_Owner__c != null AND Test_Record__c = \''+YES+'\'';                    
    }    
    //  ---------------------------------------------------------------------
    //  INTERFACE METHOD: start
    //  ---------------------------------------------------------------------
    global Database.QueryLocator start(Database.BatchableContext bc){
        return Database.getQueryLocator(query);
    }
    
    //  ---------------------------------------------------------------------
    //  INTERFACE METHOD: execute 
    //  ---------------------------------------------------------------------
    global void execute(Database.BatchableContext bc, List<sObject> scope) {
    
        Set<Id> userid = new Set<Id>();
        Set<Id> LdId = new Set<Id>();
        for (SObject s: scope) {    
            Lead LdsIds = (Lead) s;  
            LdId.add(LdsIds.Id);
            userid.add(LdsIds.Secondary_Lead_Owner__c);                            
          }     
                    
             List<User> Addresses = new List<User>();
             Addresses = [select Email,LastName from User where Id =: userid];
              List<String> toAddresses = new List<String>();              
              for(User usr: Addresses){
                 toAddresses.add(usr.Email);
              }      
    
          List<Lead> leadlist = [SELECT Id,Name,FirstName,LastName,Phone,Secondary_Lead_Owner__c from Lead where Id =:LdId ORDER BY Secondary_Lead_Owner__c];         
                   
          String header = 'Name, First Name, Last Name, Phone,Secondary_Lead_Owner__c \n';
          String finalstr = header;           
          map<Id, String> userleadsmap = new map<Id, String>();

        for(Lead a: leadlist)
        {                     
               string recordString = a.Name+','+a.FirstName+','+a.LastName+','+a.Phone+','+a.Secondary_Lead_Owner__c+'\n';
               finalstr = finalstr +recordString;               
               userleadsmap.put(a.Secondary_Lead_Owner__c, finalstr);                           
        }             
         List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
            for(User portalUser :Addresses) {
                    Messaging.EmailFileAttachment csvAttc = new Messaging.EmailFileAttachment();
                    if(userleadsmap.containskey(portalUser.Id)){ 
                    blob csvBlob = Blob.valueOf(userleadsmap.get(portalUser.Id));                    
                    string csvname= 'Lead.csv';
                    csvAttc.setFileName(csvname);
                    csvAttc.setBody(csvBlob);
                    }
                    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                    string body = 'Hi '+ portalUser.LastName;
                    String[] ccAddresses = new list<string> {'gvinnakota@ten-x.com'}; 
                    mail.setCcAddresses( ccAddresses );
                    mail.setSubject('Test Subject');
                    mail.setTargetObjectId(portalUser.Id); 
                    mail.setSaveAsActivity(false);
                    mail.setHtmlBody(body); mails.add(mail);
                    mail.setFileAttachments(new Messaging.EmailFileAttachment[]{csvAttc});
               }
            Messaging.sendEmail(mails);
             }

   global void finish(Database.BatchableContext bc){       }
}