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!!!!!
2 Answers
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 = '';
}
}
}
-
@David thanks..great idea I will work on it and let you know the result- thanks again for the beautiful idea!!!! Commented Apr 2, 2014 at 13:56
-
-
Hi @brovasi..below is the code which I have written as per your suggestion, everything is working fine but the excel output is not getting updated for evry batch ,How to update created .csv file for each batch(I mean-how to add next batch records to created file). Am i doing wrong anywere, please correct me: Commented Apr 7, 2014 at 9:39
-
1if (String.isNotBlank(deletedAccountIds) == true) { list<Document> doc= [select id,name from Document where name ='PersonAccBackup.csv' limit 1]; if(doc.size()>0){doc[0].FolderId = '00l800000016NkM'; doc[0].Body = lob.valueOf(deletedAccountIds); update doc; deletedAccountIds = '';} else{Document doc1 = new Document (Name = 'PersonAccBackup'+'.csv'); doc1.Description = ''; doc1.FolderId = '00l800000016NkM'; doc1.Body = Blob.valueOf(deletedAccountIds); insert doc1; deletedAccountIds = ''; } Commented Apr 7, 2014 at 10:17
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,
- you need to take care of the governors of dealing while creating your attachment.
- Take a look of your org data storage limits.
- 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