Find your content:

Search form

You are here

Creating attachements in batch apex

 
Share

How can I create a attchment for each batch data in batch apex- I have written batch class to delete 1 million records through batchapex, now I want to have the backup of all the data before deleting the data- each batch in one attachment or all the batch jobs in one attachment?? Please guys if anyone has the idea-let me know..waiting for your reply friends!!!!!


Attribution to: sfdcpavan

Possible Suggestion/Solution #1

you may need to create a custom object "Batch" which will have Batch Id, Batch Name, No.of success records, No. of failure records, BatchDateTime. Write logic to add every record that you are deleting into a csv file. That would be your attachment. Now for every batch create one record in Batch table and create an attachment record and attach your csv to the the Attachments record with the parent id as the new batch record id. The following points would worth,

  1. you need to take care of the governors of dealing while creating your attachment.
  2. Take a look of your org data storage limits.
  3. it is advisable and governor safe to execute a batch with appropriate batch size.

do the attachment stuff inside execute method of Batch interface.

Create CSV file from SObject records - How to Optimize Script Statments


Attribution to: Bforce

Possible Suggestion/Solution #2

If you delete the data I don't see the point to create a new custom object. I would create Document that I would store in a backup folder. You could create any type of file(csv,txt,pdf,...), to make it easy I choose .txt in my example. I didn't test my code myself (my apologies), but here is the idea :

global class MyTestBatch implements 
    Database.Batchable<sObject>, Database.Stateful{
   String deletedAccountIds;
   global MyTestBatch(){
      deletedAccountIds = '';
   }
   global Database.QueryLocator start(Database.BatchableContext BC){
      return Database.getQueryLocator('select id from account');
   }
   global void execute(
                Database.BatchableContext BC, 
                List<sObject> scope){
      for(Account s : (list<Account>)scope){
         deletedAccountId += s.Id+'';
      }
      //we reach 5Mb, let's insert the document into backup folder
      if (Blob.valueOf(deletedAccountIds).size() >= 5000000){
        insertDocument();
      }
      delete scope;
   }

    global void finish(Database.BatchableContext BC){
      insertDocument();
    }
    private void insertDocument(){
      if (String.isNotBlank(deletedAccountIds) == true){
        Folder folder = [select id from Folder where name='Data_Backup' LIMIT 1];
        Document doc = new Document (Name = 'backup_'+DateTime.now().format() + '.txt');
        doc.Description = ''; // Insert description
        doc.FolderId = folder.Id;
        doc.Body = Blob.valueOf(deletedAccountIds);
        insert doc;
        deletedAccountIds = '';
      }
    }
}

Attribution to: Cloud Ninja
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/31620

My Block Status

My Block Content