Salesforce Batch class generating results as CSV
A Database.Stateful batch Apex class on Account that collects success and failure records as CSV attachments and emails them from the finish method.
From the archive: this post is from 2016, when I worked as a Salesforce developer. Some details are dated.
Requirement: We need to capture all the failed and successful records during a batch process, build an CSV for successful and failed records and send email in the finish method.
Batch Apex: Let’s say you’re running a batch job with 20000 records, salesforce divides this batch into 100 transactions of 200 records each automatically. (Default batch size is 200 records). Let’s say we’ve 1000 erroneous records across those 100 transactions and we don’t know which transactions had errors. How do you maintain the state between these transactions? Database.Stateful allows to maintain state between transactions of a batch job. When using Database.Stateful, only instance member variables retain their values between transactions. Static member variables don’t and are reset between transactions.
To start on batch class basics: Batch Apex
Implementation: Create a batch process on Accounts, capture the failures in a CSV file and send an email to the batch job creator.
Note: Maximum size for email attachment is 3MB.
AccountUpdateBatch.cls
global class AccountUpdateBatch implements Database.Batchable<SObject>, Database.Stateful{
global Map<Id, String> errorMap;
global Map<Id, SObject> IdToSObjectMap;
global Map<Id, String> successMap;
global String baseURL;
global AccountUpdateBatch(){
errorMap = new Map<Id, String>();
successMap = new Map<Id, String>();
IdToSObjectMap = new Map<Id, SObject>();
baseURL = URL.getSalesforceBaseUrl().toExternalForm();
}
global Database.QueryLocator start(Database.BatchableContext BC) {
return Database.getQueryLocator('Select Id, OwnerId, Name From Account');
}
global void execute(Database.BatchableContext BC, List<SObject> scope) {
List<Account> accountList = new List<Account>();
for(SObject s : scope){
Account acct = (Account) s;
//Add Logic to update fields on the Account
accountList.add(acct);
}
if(accountList.size() > 0) {
List<Database.SaveResult> dsrs = Database.Update(accountList, false);
Integer index = 0;
for(Database.SaveResult dsr : dsrs){
if(!dsr.isSuccess()){
String errMsg = dsr.getErrors()[0].getMessage();
errorMap.put(accountList[index].Id, errMsg);
} else {
String sucMsg = baseURL + '/' + accountList[index].Id;
successMap.put(accountList[index].Id, sucMsg);
}
IdToSObjectMap.put(accountList[index].Id, accountList[index]);
index++;
}
}
}
global void finish(Database.BatchableContext BC) {
if(! IdToSObjectMap.isEmpty()) {
//Send an email to the User after your batch completes
AsyncApexJob a = [SELECT id, ApexClassId,
JobItemsProcessed, TotalJobItems,
NumberOfErrors, CreatedBy.Email
FROM AsyncApexJob
WHERE id = :BC.getJobId()];
String body = 'Your batch job '
+ 'AccountUpdateBatch '
+ 'has finished. \n'
+ 'There were total'
+ IdToSObjectMap.size()
+ ' records. Please find the attached success and error records CSV.';
String subject = 'Account - Apex Batch Result for AccountUpdateBatch';
// Define the email
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
// Define email file attachment list
Messaging.EmailFileAttachment[] emailAttList = new List<Messaging.EmailFileAttachment>();
if(!errorMap.isEmpty()){
// Creating the CSV file for error
String finalstr = 'Id, Name, Error \n';
String attName = 'AccountErrors' + system.now().format('YYYYMMDDhhmm') + '.csv';
for(Id id : errorMap.keySet()){
string err = errorMap.get(id);
Account acct = (Account) IdToSObjectMap.get(id);
string recordString = '"'+id+'","'+acct.Name+'","'+err+'"\n';
finalstr = finalstr +recordString;
}
// Create the email attachment
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setFileName(attName);
efa.setBody(Blob.valueOf(finalstr));
emailAttList.add(efa);
}
if(! successMap.isEmpty()) {
// Creating the CSV file for successful updates
String finalstr = 'Id, Name, Link \n';
String attName = 'AccountSuccess' + system.now().format('YYYYMMDDhhmm') + '.csv';
for(Id id : successMap.keySet()){
string suc = successMap.get(id);
Account acct = (Account) IdToSObjectMap.get(id);
string recordString = '"'+id+'","'+acct.Name+'","'+suc+'"\n';
finalstr = finalstr +recordString;
}
// Create the email attachment
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setFileName(attName);
efa.setBody(Blob.valueOf(finalstr));
emailAttList.add(efa);
}
// Sets the paramaters of the email
email.setSubject( subject );
email.setToAddresses( new String[] {a.CreatedBy.Email} );
email.setPlainTextBody( body );
email.setFileAttachments(emailAttList);
// Sends the email
Messaging.SendEmailResult [] r =
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
}
}
}
To run the batch job..
AccountUpdateBatch updateAcct = new AccountUpdateBatch();
Id batchApexCommId = Database.executeBatch(updateAcct);