You need to sign in to do that
Don't have an account?
Timothy Smith
Where is the parentID? Batch class, email csv.
I have been all over, and I don't understand. What is it asking for, parent ID of what? What am I missing here. Thank you.
My code is as follows:
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 = AsyncApexJobRecord.ApexClassID +',' + AsyncApexJobRecord.CompletedDate +','+ AsyncApexJobRecord.ExtendedStatus+',' + AsyncApexJobRecord.JobItemsProcessed + ',' + AsyncApexJobRecord.JobType + ',' + AsyncApexJobRecord.MethodName + ',' + AsyncApexJobRecord.NumberOfErrors + ',' + AsyncApexJobRecord.TotalJobItems + ','+ AsyncApexJobRecord.JobItemsProcessed + ',' +AsyncApexJobRecord.JobType + ',' + AsyncApexJobRecord.TotalJobItems+ '\n';
} else {
recordString += AsyncApexJobRecord.ApexClassID +',' + AsyncApexJobRecord.CompletedDate +','+ AsyncApexJobRecord.ExtendedStatus+',' + AsyncApexJobRecord.JobItemsProcessed + ',' + AsyncApexJobRecord.JobType + ',' + AsyncApexJobRecord.MethodName + ',' + AsyncApexJobRecord.NumberOfErrors + ',' + AsyncApexJobRecord.TotalJobItems + ','+ AsyncApexJobRecord.JobItemsProcessed + ',' +AsyncApexJobRecord.JobType + ',' + AsyncApexJobRecord.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[] {'test@test.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 });
}
}
and the email received is:
Apex script unhandled exception by user/organization: 00518000001WfNj/00D180000008oBS
Source organization: 00D300000006IaD (null)
Failed to process batch for class 'BatchSyncErrors' for job id '7071800000gWEFg'
caused by: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [ParentId]: [ParentId]
Class.BatchSyncErrors.finish: line 48, column 1
Debug Log:
40.0 APEX_CODE,DEBUG;APEX_PROFILING,FINEST;CALLOUT,FINEST;DB,FINEST;SYSTEM,FINE;VALIDATION,INFO;VISUALFORCE,FINER;WAVE,FINEST;WORKFLOW,FINER
15:56:02.0 (258843)|USER_INFO|[EXTERNAL]|00518000001WfNj|all60388@adobe.com.signstage|Pacific Standard Time|GMT-07:00
15:56:02.0 (290487)|EXECUTION_STARTED
15:56:02.0 (294740)|CODE_UNIT_STARTED|[EXTERNAL]|01p18000000BBrV|BatchSyncErrors
15:56:02.0 (11144677)|SYSTEM_METHOD_ENTRY|[41]|System.debug(ANY)
15:56:02.0 (11171785)|USER_DEBUG|[41]|DEBUG|**** Final String *** ApexClassID, CompletedDate , ExtendedStatus, JobItemsProcessed, JobType, MethodName, NumberOfErrors, Status, TotalJobItems
01p18000000BBrVAAW,2017-09-08 17:16:32,null,1,BatchApex,null,0,1,1,BatchApex,1
null,null,null,0,ApexToken,null,0,0,0,ApexToken,0
01p1800000062WPAAY,2017-09-07 15:20:29,null,1,BatchApex,null,0,1,1,BatchApex,1
null,null,null,0,ApexToken,null,0,0,0,ApexToken,0
null,null,null,0,ApexToken,null,0,0,0,ApexToken,0
01p180000005zyLAAQ,2017-09-03 06:00:26,null,1,BatchApex,null,0,1,1,BatchApex,1
01p180000005zyLAAQ,2017-09-03 06:00:17,null,1,BatchApexWorker,null,0,1,1,BatchApexWorker,1
01p18000000BBrVAAW,2017-09-07 20:51:59,null,1,BatchApex,null,0,1,1,BatchApex,1
null,null,null,0,ApexToken,null,0,0,0,ApexToken,0
01p18000000BBrVAAW,2017-09-07 19:46:50,null,1,BatchApex,null,0,1,1,BatchApex,1
15:56:02.0 (11190603)|SYSTEM_METHOD_EXIT|[41]|System.debug(ANY)
15:56:02.0 (11352022)|SYSTEM_METHOD_ENTRY|[45]|Blob.valueOf(String)
15:56:02.0 (11392106)|SYSTEM_METHOD_EXIT|[45]|Blob.valueOf(String)
15:56:02.0 (11548575)|DML_BEGIN|[48]|Op:Insert|Type:Attachment|Rows:1
15:56:02.0 (11562050)|LIMIT_USAGE|[48]|DML|1|150
15:56:02.0 (11575389)|LIMIT_USAGE|[48]|DML_ROWS|1|10000
15:56:02.0 (425072145)|CODE_UNIT_STARTED|[EXTERNAL]|01q40000000Td0s|AttachmentTrigger on Attachment trigger event BeforeInsert for [new]
15:56:02.0 (425366692)|SYSTEM_METHOD_ENTRY|[13]|Limit.getLimitQueries()
15:56:02.0 (425414605)|SYSTEM_METHOD_EXIT|[13]|Limit.getLimitQueries()
15:56:02.0 (425428037)|SYSTEM_METHOD_ENTRY|[13]|Limit.getQueries()
15:56:02.0 (425434866)|SYSTEM_METHOD_EXIT|[13]|Limit.getQueries()
15:56:02.0 (425440962)|SYSTEM_METHOD_ENTRY|[13]|Limit.getLimitDmlRows()
15:56:02.0 (425445832)|SYSTEM_METHOD_EXIT|[13]|Limit.getLimitDmlRows()
15:56:02.0 (425451753)|SYSTEM_METHOD_ENTRY|[13]|Limit.getDmlRows()
15:56:02.0 (425455759)|SYSTEM_METHOD_EXIT|[13]|Limit.getDmlRows()
15:56:02.458 (458339540)|CUMULATIVE_LIMIT_USAGE
15:56:02.458 (458339540)|LIMIT_USAGE_FOR_NS|(default)|
Number of SOQL queries: 0 out of 200
Number of query rows: 0 out of 50000
Number of SOSL queries: 0 out of 20
Number of DML statements: 1 out of 150
Number of DML rows: 1 out of 10000
Maximum CPU time: 0 out of 60000
Maximum heap size: 0 out of 12000000
Number of callouts: 0 out of 0
Number of Email Invocations: 0 out of 10
Number of future calls: 0 out of 0
Number of queueable jobs added to the queue: 0 out of 1
Number of Mobile Apex push calls: 0 out of 10
15:56:02.458 (458339540)|TOTAL_EMAIL_RECIPIENTS_QUEUED|0
15:56:02.458 (458339540)|STATIC_VARIABLE_LIST|
char[]:DigitTens:0
long:serialVersionUID:0
long:serialVersionUID:0
String:_sfdcAdditionalCodeLocations:0
String:__sfdcParameterizedTypes:0
double:MAX_VALUE:0
Boolean:FALSE:0
long:serialVersionUID:0
String:_sfdcSuppressedCodeLocations:0
double:MIN_VALUE:0
int:SIZE:0
double:NEGATIVE_INFINITY:0
int:MAX_VALUE:0
String:_sfdcAdditionalCodeLocations:0
char[]:digits:0
double:NaN:0
Boolean:TRUE:0
String:_sfdcAdditionalCodeLocations:0
int:MIN_VALUE:0
char[]:DigitOnes:0
String:_sfdcAdditionalCodeLocations:0
int:BYTES:0
int:MIN_EXPONENT:0
int[]:sizeTable:0
int:MAX_EXPONENT:0
int:SIZE:0
double:POSITIVE_INFINITY:0
double:MIN_NORMAL:0
int:BYTES:0
15:56:02.458 (458339540)|CUMULATIVE_LIMIT_USAGE_END
15:56:02.0 (459420821)|CODE_UNIT_FINISHED|AttachmentTrigger on Attachment trigger event BeforeInsert for [new]
15:56:02.0 (470961184)|DML_END|[48]
15:56:02.0 (471029439)|EXCEPTION_THROWN|[48]|System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [ParentId]: [ParentId]
15:56:02.0 (471405705)|FATAL_ERROR|System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [ParentId]: [ParentId]
Class.BatchSyncErrors.finish: line 48, column 1
15:56:02.531 (531444350)|CUMULATIVE_PROFILING_BEGIN
15:56:02.531 (531444350)|CUMULATIVE_PROFILING|No profiling information for SOQL operations
15:56:02.531 (531444350)|CUMULATIVE_PROFILING|No profiling information for SOSL operations
15:56:02.531 (531444350)|CUMULATIVE_PROFILING|DML operations|
Class.BatchSyncErrors.finish: line 48, column 1: Insert: Attachment: executed 1 time in 460 ms
15:56:02.531 (531444350)|CUMULATIVE_PROFILING|method invocations|
External entry point: global void finish(Database.BatchableContext): executed 1 time in 460 ms
External entry point: public void invoke(): executed 1 time in 33 ms
Class.AttachmentTriggerHandler.executeOperations: line 19, column 1: private static void onBeforeInsert(List<Attachment>): executed 1 time in 0 ms
Class.BatchSyncErrors.finish: line 41, column 1: global public static void debug(ANY): executed 1 time in 0 ms
Class.BatchSyncErrors.finish: line 45, column 1: public static Blob valueOf(String): executed 1 time in 0 ms
Trigger.AttachmentTrigger: line 13, column 1: global public static Integer getLimitQueries(): executed 4 times in 0 ms
Trigger.AttachmentTrigger: line 14, column 1: public static void executeOperations(List<Attachment>, List<Attachment>, Map<Id,Attachment>, Map<Id,Attachment>, Boolean, Boolean, Boolean, Boolean, Boolean, Boolean, Boolean): executed 1 time in 0 ms
15:56:02.531 (531444350)|CUMULATIVE_PROFILING_END
My code is as follows:
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 = AsyncApexJobRecord.ApexClassID +',' + AsyncApexJobRecord.CompletedDate +','+ AsyncApexJobRecord.ExtendedStatus+',' + AsyncApexJobRecord.JobItemsProcessed + ',' + AsyncApexJobRecord.JobType + ',' + AsyncApexJobRecord.MethodName + ',' + AsyncApexJobRecord.NumberOfErrors + ',' + AsyncApexJobRecord.TotalJobItems + ','+ AsyncApexJobRecord.JobItemsProcessed + ',' +AsyncApexJobRecord.JobType + ',' + AsyncApexJobRecord.TotalJobItems+ '\n';
} else {
recordString += AsyncApexJobRecord.ApexClassID +',' + AsyncApexJobRecord.CompletedDate +','+ AsyncApexJobRecord.ExtendedStatus+',' + AsyncApexJobRecord.JobItemsProcessed + ',' + AsyncApexJobRecord.JobType + ',' + AsyncApexJobRecord.MethodName + ',' + AsyncApexJobRecord.NumberOfErrors + ',' + AsyncApexJobRecord.TotalJobItems + ','+ AsyncApexJobRecord.JobItemsProcessed + ',' +AsyncApexJobRecord.JobType + ',' + AsyncApexJobRecord.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[] {'test@test.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 });
}
}
and the email received is:
Apex script unhandled exception by user/organization: 00518000001WfNj/00D180000008oBS
Source organization: 00D300000006IaD (null)
Failed to process batch for class 'BatchSyncErrors' for job id '7071800000gWEFg'
caused by: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [ParentId]: [ParentId]
Class.BatchSyncErrors.finish: line 48, column 1
Debug Log:
40.0 APEX_CODE,DEBUG;APEX_PROFILING,FINEST;CALLOUT,FINEST;DB,FINEST;SYSTEM,FINE;VALIDATION,INFO;VISUALFORCE,FINER;WAVE,FINEST;WORKFLOW,FINER
15:56:02.0 (258843)|USER_INFO|[EXTERNAL]|00518000001WfNj|all60388@adobe.com.signstage|Pacific Standard Time|GMT-07:00
15:56:02.0 (290487)|EXECUTION_STARTED
15:56:02.0 (294740)|CODE_UNIT_STARTED|[EXTERNAL]|01p18000000BBrV|BatchSyncErrors
15:56:02.0 (11144677)|SYSTEM_METHOD_ENTRY|[41]|System.debug(ANY)
15:56:02.0 (11171785)|USER_DEBUG|[41]|DEBUG|**** Final String *** ApexClassID, CompletedDate , ExtendedStatus, JobItemsProcessed, JobType, MethodName, NumberOfErrors, Status, TotalJobItems
01p18000000BBrVAAW,2017-09-08 17:16:32,null,1,BatchApex,null,0,1,1,BatchApex,1
null,null,null,0,ApexToken,null,0,0,0,ApexToken,0
01p1800000062WPAAY,2017-09-07 15:20:29,null,1,BatchApex,null,0,1,1,BatchApex,1
null,null,null,0,ApexToken,null,0,0,0,ApexToken,0
null,null,null,0,ApexToken,null,0,0,0,ApexToken,0
01p180000005zyLAAQ,2017-09-03 06:00:26,null,1,BatchApex,null,0,1,1,BatchApex,1
01p180000005zyLAAQ,2017-09-03 06:00:17,null,1,BatchApexWorker,null,0,1,1,BatchApexWorker,1
01p18000000BBrVAAW,2017-09-07 20:51:59,null,1,BatchApex,null,0,1,1,BatchApex,1
null,null,null,0,ApexToken,null,0,0,0,ApexToken,0
01p18000000BBrVAAW,2017-09-07 19:46:50,null,1,BatchApex,null,0,1,1,BatchApex,1
15:56:02.0 (11190603)|SYSTEM_METHOD_EXIT|[41]|System.debug(ANY)
15:56:02.0 (11352022)|SYSTEM_METHOD_ENTRY|[45]|Blob.valueOf(String)
15:56:02.0 (11392106)|SYSTEM_METHOD_EXIT|[45]|Blob.valueOf(String)
15:56:02.0 (11548575)|DML_BEGIN|[48]|Op:Insert|Type:Attachment|Rows:1
15:56:02.0 (11562050)|LIMIT_USAGE|[48]|DML|1|150
15:56:02.0 (11575389)|LIMIT_USAGE|[48]|DML_ROWS|1|10000
15:56:02.0 (425072145)|CODE_UNIT_STARTED|[EXTERNAL]|01q40000000Td0s|AttachmentTrigger on Attachment trigger event BeforeInsert for [new]
15:56:02.0 (425366692)|SYSTEM_METHOD_ENTRY|[13]|Limit.getLimitQueries()
15:56:02.0 (425414605)|SYSTEM_METHOD_EXIT|[13]|Limit.getLimitQueries()
15:56:02.0 (425428037)|SYSTEM_METHOD_ENTRY|[13]|Limit.getQueries()
15:56:02.0 (425434866)|SYSTEM_METHOD_EXIT|[13]|Limit.getQueries()
15:56:02.0 (425440962)|SYSTEM_METHOD_ENTRY|[13]|Limit.getLimitDmlRows()
15:56:02.0 (425445832)|SYSTEM_METHOD_EXIT|[13]|Limit.getLimitDmlRows()
15:56:02.0 (425451753)|SYSTEM_METHOD_ENTRY|[13]|Limit.getDmlRows()
15:56:02.0 (425455759)|SYSTEM_METHOD_EXIT|[13]|Limit.getDmlRows()
15:56:02.458 (458339540)|CUMULATIVE_LIMIT_USAGE
15:56:02.458 (458339540)|LIMIT_USAGE_FOR_NS|(default)|
Number of SOQL queries: 0 out of 200
Number of query rows: 0 out of 50000
Number of SOSL queries: 0 out of 20
Number of DML statements: 1 out of 150
Number of DML rows: 1 out of 10000
Maximum CPU time: 0 out of 60000
Maximum heap size: 0 out of 12000000
Number of callouts: 0 out of 0
Number of Email Invocations: 0 out of 10
Number of future calls: 0 out of 0
Number of queueable jobs added to the queue: 0 out of 1
Number of Mobile Apex push calls: 0 out of 10
15:56:02.458 (458339540)|TOTAL_EMAIL_RECIPIENTS_QUEUED|0
15:56:02.458 (458339540)|STATIC_VARIABLE_LIST|
char[]:DigitTens:0
long:serialVersionUID:0
long:serialVersionUID:0
String:_sfdcAdditionalCodeLocations:0
String:__sfdcParameterizedTypes:0
double:MAX_VALUE:0
Boolean:FALSE:0
long:serialVersionUID:0
String:_sfdcSuppressedCodeLocations:0
double:MIN_VALUE:0
int:SIZE:0
double:NEGATIVE_INFINITY:0
int:MAX_VALUE:0
String:_sfdcAdditionalCodeLocations:0
char[]:digits:0
double:NaN:0
Boolean:TRUE:0
String:_sfdcAdditionalCodeLocations:0
int:MIN_VALUE:0
char[]:DigitOnes:0
String:_sfdcAdditionalCodeLocations:0
int:BYTES:0
int:MIN_EXPONENT:0
int[]:sizeTable:0
int:MAX_EXPONENT:0
int:SIZE:0
double:POSITIVE_INFINITY:0
double:MIN_NORMAL:0
int:BYTES:0
15:56:02.458 (458339540)|CUMULATIVE_LIMIT_USAGE_END
15:56:02.0 (459420821)|CODE_UNIT_FINISHED|AttachmentTrigger on Attachment trigger event BeforeInsert for [new]
15:56:02.0 (470961184)|DML_END|[48]
15:56:02.0 (471029439)|EXCEPTION_THROWN|[48]|System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [ParentId]: [ParentId]
15:56:02.0 (471405705)|FATAL_ERROR|System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [ParentId]: [ParentId]
Class.BatchSyncErrors.finish: line 48, column 1
15:56:02.531 (531444350)|CUMULATIVE_PROFILING_BEGIN
15:56:02.531 (531444350)|CUMULATIVE_PROFILING|No profiling information for SOQL operations
15:56:02.531 (531444350)|CUMULATIVE_PROFILING|No profiling information for SOSL operations
15:56:02.531 (531444350)|CUMULATIVE_PROFILING|DML operations|
Class.BatchSyncErrors.finish: line 48, column 1: Insert: Attachment: executed 1 time in 460 ms
15:56:02.531 (531444350)|CUMULATIVE_PROFILING|method invocations|
External entry point: global void finish(Database.BatchableContext): executed 1 time in 460 ms
External entry point: public void invoke(): executed 1 time in 33 ms
Class.AttachmentTriggerHandler.executeOperations: line 19, column 1: private static void onBeforeInsert(List<Attachment>): executed 1 time in 0 ms
Class.BatchSyncErrors.finish: line 41, column 1: global public static void debug(ANY): executed 1 time in 0 ms
Class.BatchSyncErrors.finish: line 45, column 1: public static Blob valueOf(String): executed 1 time in 0 ms
Trigger.AttachmentTrigger: line 13, column 1: global public static Integer getLimitQueries(): executed 4 times in 0 ms
Trigger.AttachmentTrigger: line 14, column 1: public static void executeOperations(List<Attachment>, List<Attachment>, Map<Id,Attachment>, Map<Id,Attachment>, Boolean, Boolean, Boolean, Boolean, Boolean, Boolean, Boolean): executed 1 time in 0 ms
15:56:02.531 (531444350)|CUMULATIVE_PROFILING_END
for Attachment Name, body, ownerId, parentId are required fields.
In your case for attachment you are missing parentId.
parent object can be
Account
Asset
Campaign
Case
Contact
Contract
Custom objects
EmailMessage
EmailTemplate
Event
Lead
Opportunity
Product2
Solution
Task
add parentId to attachment while inserting attachment as per requirement.
for more information see https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_attachment.htm
Alternatively if you don't want to store attachment under any object consider using Document object.