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
@anilbathula@@anilbathula@ 

Sending a Record as Attachment

Hi guys,

 

I have a batch class which delete records from lead.

Before deleting these records i want all the query records should be send as a attchment to a email.

Is it possible.

-----Batch Class---

global class Del_leads implements Database.Batchable<sobject>
{
  
   public String query;  
   public date d=System.today();
   public integer i=7;
   Public Date z=d-i;
   String email;
  
   global Database.QueryLocator start(Database.BatchableContext BC){
      system.debug(query);
      return Database.getQueryLocator(query);
   }
   
   global void execute(Database.BatchableContext BC,List<Lead> Lds){
       delete Lds;       
   }  
   
   global void finish(Database.BatchableContext BC){
   
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();        
        mail.setToAddresses(new String[] {'anil@gmail.com'});
        mail.setReplyTo('batch@acme.com');
        mail.setSenderDisplayName('Batch Processing');
        mail.setSubject('Batch Process Completed');
        mail.setPlainTextBody('Batch Process has completed');        
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        System.debug(LoggingLevel.WARN,'Deleting leads Finished');
   }
}

---------------------------------------------------------------Schedule class----------------

global class bulkdelleads Implements Schedulable{
global void Execute(SchedulableContext SC){

Del_leads ld=new Del_leads();
ld.query='select id,name from lead where createddate<=:z and date_opened__c=null limit 10';
database.executebatch(ld);


}
}

 

Before Delete operation iwant to send All the ld.query records as attachment to a email.

 

 

 

Thanks
Anil.B

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
@anilbathula@@anilbathula@

Hi guys ,

 

Here is the solution for this:-

 

global class Del_leads implements Database.Batchable<sobject>
{

public String query;
public date d=System.today();
public integer i=7;
Public Date z=d-i;
Public String s;
public integer j;
Public string k='Name'+','+'Company'+','+'phone'+','+'Email';

global Database.QueryLocator start(Database.BatchableContext BC){
system.debug(query);
return Database.getQueryLocator(query);

}

global void execute(Database.BatchableContext BC,List<Lead> Lds){
for( j=0;j<lds.size();j++){
if(j==0){
s +=k+'\n'+ lds[j].Name+','+lds[j].Company+','+lds[j].phone+','+lds[j].Email;
} else{
s +=+'\n'+ lds[j].Name+','+lds[j].Company+','+lds[j].phone+','+lds[j].Email;
}
}

Blob b=Blob.valueOf(s);
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setFileName('attachment.csv');
efa.setBody(b);

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new String[] {'anil@gmail.com'});
mail.setSenderDisplayName('Batch Processing');
mail.setSubject('Batch Process Completed');
mail.setPlainTextBody('Please find the attachment of deleted records');
mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
delete Lds;
}

global void finish(Database.BatchableContext BC){
System.debug(LoggingLevel.WARN,'Deleting Leads Finished');
}

-----------------Schedule Class--------------------------------------------------

---------------------------------------------------------------Schedule class----------------

global class bulkdelleads Implements Schedulable{
global void Execute(SchedulableContext SC){

Del_leads ld=new Del_leads();
ld.query='select id,name from lead where createddate<=:z and date_opened__c=null limit 10';
database.executebatch(ld);


}
}

 

 

Thanks

Anil.B