function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
amit wagaskaramit wagaskar 

please help me with this code

    global class Job_Application_India_Mail_To_Hr implements Database.Batchable<sobject> {
    
    global Database.QueryLocator start(Database.BatchableContext BC)    
    {
    return Database.getQueryLocator([select id,LeadSource from lead where leadsource ='Job Application India' AND Email_sent__c=false]);
    }
    
    
    OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address ='careers@Cenduit.com'];
    
    global void execute(Database.BatchableContext BC,List<lead>GetLeads)
    
    {
    EmailTemplate et=[Select id from EmailTemplate where name=:'Mail To Hr India']; 
    List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
    List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
    List <id> LeadId = new List<id>();
    List <id> Email_Sent_update = new List<id>();
    List <Lead> Lead_flag_Update = new List<Lead>();
    for(lead GetLeadId:GetLeads)
    { 
    LeadId.add(GetLeadId.id); 
    } 
    
    List<Attachment> getattachment=[select id,parentid,name,body from Attachment where parentid in :LeadId];
    
    
    //send mail to Hr for Non India
    try{
    for(Attachment SendMailOnLead:getattachment)
    {
    Email_Sent_update.add(SendMailOnLead.ParentId); 
    Messaging.SingleEmailMessage singleMail = new Messaging.SingleEmailMessage();
    String[] sendingTo = new String[]{'wagaskar.amit@gmail.com'};
    
    singleMail.setTargetObjectId(SendMailOnLead.ParentId);
    
    singleMail.setTemplateId(et.Id);
    singleMail.setToAddresses(sendingTo);
    singleMail.setTreatTargetObjectAsRecipient(false); 
    if (owea.size()>0)
    {
    singleMail.setOrgWideEmailAddressId(owea.get(0).Id);
    }
    
    
    
    Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
    efa.setFilename(SendMailOnLead.Name);
    efa.setbody(SendMailOnLead.Body);
    fileAttachments.add(efa);
    singleMail.setFileAttachments(fileAttachments);
    fileAttachments.clear();
    emails.add(singleMail);
    
    }
    Messaging.sendEmail(emails);
    fileAttachments.clear();
    List <Lead> Update_flag = [select id,Email_Sent__c from lead where id in : Email_Sent_update ];
    for(Lead Email_sent_flag_update : Update_flag)
    {
    system.debug('********');
    Email_sent_flag_update.Email_Sent__c=True;
    Lead_flag_Update.add(Email_sent_flag_update);
    }
    update Lead_flag_Update;
    }
    catch(Exception ex)
    {
    System.debug(ex.getMessage());
    }
    }
    global void finish(Database.BatchableContext BC)
    
    {
    system.debug('batch completed successfully');
    
    }
    }

//here i am not able to update field as well as not able to send an email