SALESFORCE BATCH CLASS GENERATING RESULTS AS CSV

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

To run the batch job..