+ Start a Discussion
Timothy SmithTimothy Smith 

Batch Class on AsyncApex attach csv file of errors, Help?

This is my first batch class ever written, and I have finally gotten to this point.  I have been all over and can't find info to help me with the last TODOs in the Finish.  Any help or suggestions would be greatly appreceaited.

global class AriaBatchErrors implements Database.Batchable<sOBject>, Database.Stateful{
    
    global string excelHeader = 'ApexClassID, CompletedDate , ExtendedStatus, JobItemsProcessed, JobType, MethodName, NumberOfErrors, Status, TotalJobItems \n';
    global string finalstr = '';
    
    //constructor
    global AriaBatchErrors(){
        finalstr = excelHeader;
    }
    //start method
    global Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator('SELECT ApexClassID, CompletedDate, ExtendedStatus, JobItemsProcessed, JobType, MethodName, NumberOfErrors, Status, TotalJobItems FROM AsyncApexJob WHERE NumberOfErrors > 0');
    }

    //execute
    global void execute(Database.BatchableContext BC, List<sOBject> scope){
        string recordString;
        for(sOBject a: scope){
            // Type Cast sOBject to AsyncApexJob
            AsyncApexJob asynApexJobRecord = (AsyncApexJob) a;
            
            recordString = a.ApexClassID +',' + a.CompletedDate +','+ a.ExtendedStatus+',' + a.JobItemsProcessed + ',' + a.JobType + a.TotalJobItems + ','+ a.CreatedBy.Email + '\n';
            finalstr =+ finalstr + recordString;
            a.JobItemsProcessed',' +a.JobType+a.TotalJobItems+','+ a.CreatedBy.Email + '\n';
        }
    }

    
    //finish
    global void finish (Database.BatchableContext BC){
        
        system.debug('**** Final String *** ' + finalstr);
        //TODO: pass this string to create CSV in your CSV Method.
        //TODO: Create an generic method to send emails & pass this attachment. 
    }
}



THank you.
Sagar PareekSagar Pareek
Both of the TODOs are answered here -
1. https://developer.financialforce.com/customizations/importing-large-csv-files-via-batch-apex/
2. https://developer.salesforce.com/forums/?id=9060G000000I1BdQAK