You need to sign in to do that
Don't have an account?
Timothy Smith
Not getting data in email from batch class
I am only getting the field names, not the data sent to my email. Please help.
global class BatchSyncErrors implements Database.Batchable<sOBject>, Database.Stateful{
global string excelHeader = 'ApexClassID, CompletedDate , ExtendedStatus, JobItemsProcessed, JobType, MethodName, NumberOfErrors, Status, TotalJobItems \n';
global string finalstr = '';
//constructor
global BatchSyncErrors(){
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 Limit 10');
}
//execute
global void execute(Database.BatchableContext BC, List<sOBject> scope){
string recordString;
for(sOBject a: scope){
// Type Cast sOBject to AsyncApexJob
AsyncApexJob asyncApexJobRecord = (AsyncApexJob) a;
if(recordString == NULL){
recordString = AsyncApexJob.ApexClassID +',' + AsyncApexJob.CompletedDate +','+ AsyncApexJob.ExtendedStatus+',' + AsyncApexJob.JobItemsProcessed + ',' + AsyncApexJob.JobType + ',' + AsyncApexJob.MethodName + ',' + AsyncApexJob.NumberOfErrors + ',' + AsyncApexJob.TotalJobItems + ','+ AsyncApexJob.JobItemsProcessed + ',' +AsyncApexJob.JobType + ',' + AsyncApexJob.TotalJobItems+ '\n';
} else {
recordString += AsyncApexJob.ApexClassID +',' + AsyncApexJob.CompletedDate +','+ AsyncApexJob.ExtendedStatus+',' + AsyncApexJob.JobItemsProcessed + ',' + AsyncApexJob.JobType + ',' + AsyncApexJob.MethodName + ',' + AsyncApexJob.NumberOfErrors + ',' + AsyncApexJob.TotalJobItems + ','+ AsyncApexJob.JobItemsProcessed + ',' +AsyncApexJob.JobType + ',' + AsyncApexJob.TotalJobItems+ '\n';
}
}
finalstr += recordString;
}
//finish
global void finish (Database.BatchableContext BC){
system.debug('**** Final String *** ' + finalstr);
//TODO: pass this string to create CSV in your CSV Method.
// String csvContent = finalstr;
// Attachment attachment = new Attachment();
// attachment.Body = Blob.valueOf(csvContent);
// attachment.Name = 'csvFile.csv';
// insert attachment;
//TODO: Create an generic method to send emails & pass this attachment.
AsyncApexJob a = [SELECT Id, Status, NumberOfErrors, JobItemsProcessed, TotalJobItems, CreatedBy.Email,ExtendedStatus,JobType,ApexClassId,MethodName
FROM AsyncApexJob WHERE Id =
:BC.getJobId()];
Messaging.Singleemailmessage mail = new Messaging.Singleemailmessage();
mail.setToAddresses((new String[] {'all60388@adobe.com'}));
//mail.setReplyTo('noreply@privatebudget.com');
mail.setSenderDisplayName('Batch Class Errors');
mail.setSubject('Batch Class Errors');
mail.setBccSender(false);
mail.setUseSignature(false);
mail.setPlainTextBody(finalstr);
system.debug('@@@@ sendEmail - mail : ' + mail);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
In my email I get:
ApexClassID, CompletedDate , ExtendedStatus, JobItemsProcessed, JobType, MethodName, NumberOfErrors, Status, TotalJobItems
ApexClassId,CompletedDate,ExtendedStatus,JobItemsProcessed,JobType,MethodName,NumberOfErrors,TotalJobItems,JobItemsProcessed,JobType,TotalJobItems
ApexClassId,CompletedDate,ExtendedStatus,JobItemsProcessed,JobType,MethodName,NumberOfErrors,TotalJobItems,JobItemsProcessed,JobType,TotalJobItems
ApexClassId,CompletedDate,ExtendedStatus,JobItemsProcessed,JobType,MethodName,NumberOfErrors,TotalJobItems,JobItemsProcessed,JobType,TotalJobItems
ApexClassId,CompletedDate,ExtendedStatus,JobItemsProcessed,JobType,MethodName,NumberOfErrors,TotalJobItems,JobItemsProcessed,JobType,TotalJobItems
ApexClassId,CompletedDate,ExtendedStatus,JobItemsProcessed,JobType,MethodName,NumberOfErrors,TotalJobItems,JobItemsProcessed,JobType,TotalJobItems
ApexClassId,CompletedDate,ExtendedStatus,JobItemsProcessed,JobType,MethodName,NumberOfErrors,TotalJobItems,JobItemsProcessed,JobType,TotalJobItems
ApexClassId,CompletedDate,ExtendedStatus,JobItemsProcessed,JobType,MethodName,NumberOfErrors,TotalJobItems,JobItemsProcessed,JobType,TotalJobItems
ApexClassId,CompletedDate,ExtendedStatus,JobItemsProcessed,JobType,MethodName,NumberOfErrors,TotalJobItems,JobItemsProcessed,JobType,TotalJobItems
ApexClassId,CompletedDate,ExtendedStatus,JobItemsProcessed,JobType,MethodName,NumberOfErrors,TotalJobItems,JobItemsProcessed,JobType,TotalJobItems
ApexClassId,CompletedDate,ExtendedStatus,JobItemsProcessed,JobType,MethodName,NumberOfErrors,TotalJobItems,JobItemsProcessed,JobType,TotalJobItems
Thank you
global class BatchSyncErrors implements Database.Batchable<sOBject>, Database.Stateful{
global string excelHeader = 'ApexClassID, CompletedDate , ExtendedStatus, JobItemsProcessed, JobType, MethodName, NumberOfErrors, Status, TotalJobItems \n';
global string finalstr = '';
//constructor
global BatchSyncErrors(){
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 Limit 10');
}
//execute
global void execute(Database.BatchableContext BC, List<sOBject> scope){
string recordString;
for(sOBject a: scope){
// Type Cast sOBject to AsyncApexJob
AsyncApexJob asyncApexJobRecord = (AsyncApexJob) a;
if(recordString == NULL){
recordString = AsyncApexJob.ApexClassID +',' + AsyncApexJob.CompletedDate +','+ AsyncApexJob.ExtendedStatus+',' + AsyncApexJob.JobItemsProcessed + ',' + AsyncApexJob.JobType + ',' + AsyncApexJob.MethodName + ',' + AsyncApexJob.NumberOfErrors + ',' + AsyncApexJob.TotalJobItems + ','+ AsyncApexJob.JobItemsProcessed + ',' +AsyncApexJob.JobType + ',' + AsyncApexJob.TotalJobItems+ '\n';
} else {
recordString += AsyncApexJob.ApexClassID +',' + AsyncApexJob.CompletedDate +','+ AsyncApexJob.ExtendedStatus+',' + AsyncApexJob.JobItemsProcessed + ',' + AsyncApexJob.JobType + ',' + AsyncApexJob.MethodName + ',' + AsyncApexJob.NumberOfErrors + ',' + AsyncApexJob.TotalJobItems + ','+ AsyncApexJob.JobItemsProcessed + ',' +AsyncApexJob.JobType + ',' + AsyncApexJob.TotalJobItems+ '\n';
}
}
finalstr += recordString;
}
//finish
global void finish (Database.BatchableContext BC){
system.debug('**** Final String *** ' + finalstr);
//TODO: pass this string to create CSV in your CSV Method.
// String csvContent = finalstr;
// Attachment attachment = new Attachment();
// attachment.Body = Blob.valueOf(csvContent);
// attachment.Name = 'csvFile.csv';
// insert attachment;
//TODO: Create an generic method to send emails & pass this attachment.
AsyncApexJob a = [SELECT Id, Status, NumberOfErrors, JobItemsProcessed, TotalJobItems, CreatedBy.Email,ExtendedStatus,JobType,ApexClassId,MethodName
FROM AsyncApexJob WHERE Id =
:BC.getJobId()];
Messaging.Singleemailmessage mail = new Messaging.Singleemailmessage();
mail.setToAddresses((new String[] {'all60388@adobe.com'}));
//mail.setReplyTo('noreply@privatebudget.com');
mail.setSenderDisplayName('Batch Class Errors');
mail.setSubject('Batch Class Errors');
mail.setBccSender(false);
mail.setUseSignature(false);
mail.setPlainTextBody(finalstr);
system.debug('@@@@ sendEmail - mail : ' + mail);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
In my email I get:
ApexClassID, CompletedDate , ExtendedStatus, JobItemsProcessed, JobType, MethodName, NumberOfErrors, Status, TotalJobItems
ApexClassId,CompletedDate,ExtendedStatus,JobItemsProcessed,JobType,MethodName,NumberOfErrors,TotalJobItems,JobItemsProcessed,JobType,TotalJobItems
ApexClassId,CompletedDate,ExtendedStatus,JobItemsProcessed,JobType,MethodName,NumberOfErrors,TotalJobItems,JobItemsProcessed,JobType,TotalJobItems
ApexClassId,CompletedDate,ExtendedStatus,JobItemsProcessed,JobType,MethodName,NumberOfErrors,TotalJobItems,JobItemsProcessed,JobType,TotalJobItems
ApexClassId,CompletedDate,ExtendedStatus,JobItemsProcessed,JobType,MethodName,NumberOfErrors,TotalJobItems,JobItemsProcessed,JobType,TotalJobItems
ApexClassId,CompletedDate,ExtendedStatus,JobItemsProcessed,JobType,MethodName,NumberOfErrors,TotalJobItems,JobItemsProcessed,JobType,TotalJobItems
ApexClassId,CompletedDate,ExtendedStatus,JobItemsProcessed,JobType,MethodName,NumberOfErrors,TotalJobItems,JobItemsProcessed,JobType,TotalJobItems
ApexClassId,CompletedDate,ExtendedStatus,JobItemsProcessed,JobType,MethodName,NumberOfErrors,TotalJobItems,JobItemsProcessed,JobType,TotalJobItems
ApexClassId,CompletedDate,ExtendedStatus,JobItemsProcessed,JobType,MethodName,NumberOfErrors,TotalJobItems,JobItemsProcessed,JobType,TotalJobItems
ApexClassId,CompletedDate,ExtendedStatus,JobItemsProcessed,JobType,MethodName,NumberOfErrors,TotalJobItems,JobItemsProcessed,JobType,TotalJobItems
ApexClassId,CompletedDate,ExtendedStatus,JobItemsProcessed,JobType,MethodName,NumberOfErrors,TotalJobItems,JobItemsProcessed,JobType,TotalJobItems
Thank you
I think that the issue is due to an error when you are filling the "recordString" variable.
You are casting the record into the variable "asyncApexJobRecord" here: However, when you assign to the "recordString" variable here, you are not using the variable that you just cast it to, but rather are using the object name "AsyncApexJob".
Let me know if that helps!
- Joe