Here is my current Batchable apex code,currently this batch job copies the data over from the new custom object ERT_Case_Type__c to the old custom object Case_Type__c
But problem is this job not copying when the Level2 or Level3 are not there. I mean say Level2 or Level3 is NULL
How can i make sure that Batchable Salesforce apex code report the records which didn't got copied because there needs to be a matching set of L1, L2 and L3s in Case Type.
global class ertcopybatch3pm implements Database.Batchable<sObject> {
global Database.QueryLocator start(Database.BatchableContext BC) {
// collect the batches of records or objects to be passed to execute
String query = 'select Case__c, Level_1__c, Level_2__c,Level_3__c FROM ERT_Case_Type__c';
System.debug('ERT Case No is =====>' +query);
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<ERT_Case_Type__c> exeList) {
// process each batch of records
List<Case_Type__c> listCTD = new List<Case_Type__c>();
System.debug('ERT Case No is =====>' +exeList);
for(ERT_Case_Type__c exe : exeList)
{
listCTD.add(new Case_Type__c(Case__c=exe.Case__c,Level_1__c=exe.Level_1__c,Level_2__c=exe.Level_2__c,Level_3__c=exe.Level_3__c));
// System.debug('ERT Case No is =====>' +Case__c);
}
try {
System.debug('ERT Case No is =====>' +listCTD);
insert listCTD;
} catch(Exception e) {
System.debug(e);
}
}
global void finish(Database.BatchableContext BC) {
// execute any post-processing operations
}
}
Thanks in advance, Carolyn
It's not really clear right now why you're having an issue, or indeed what the issue is. There is no logic that is conditional on the values of the fields Level_1__c
, Level_2__c
, and Level_3__c
, nor is there any possibility of a NullPointerException
on any of these fields.
If the fields on the Case_Type__c
object are required, or if there is a trigger or declarative automation on the Case_Type__c
object that expects them to be non-null
, you might be generating errors there. You do have a swallowed exception:
} catch(Exception e) {
System.debug(e);
}
that you should remove, as it is hiding any real errors that occur upon insert
. This may guide you to the real underlying problem.
Note additionally that you really do not need Batch Apex to do this operation. You could accomplish it much more easily using any Salesforce data loader to extract the one object and then load data into the other.
Batch Apex does not inherently report records that fail, particularly when you explicitly suppress exceptions. To do so, you'd need to use partial-success methods like Database.insert()
to insert records, iterate over the results, and persist information about any failures to the database (possibly on the original source records). The code would be quite a bit more complex, but again, I really don't think you need to do this. You need to locate and fix the issue that is probably occurring on insert, and/or switch to using a data loader.