Issue with history and casecomment batches
The execute method in the class brf_BatchableRetryJob will throw an exception when the batch operates on history sobjects i.e. accounthistory, claimhistory. CaseComment has the same issue
A sobject instance cannot be created for those types with the following call: //errorId points to either claimhistory, casecomment etc sObjectType.newSObject(errorId);
https://help.salesforce.com/s/articleView?id=000332227&type=1
I'm "solving" this with a switch case. However I'm not satisfied with this solution since I'll need to rewrite the brf_BatchableRetryJob.execute method each time a new batch is introduced that operates on the history objects/casecommments etc...
public void execute(Database.BatchableContext ctx, List<brf_BatchableError> scope) {
// One batch scope here represents an entire previously failed batch scope
brf_BatchableError error = scope[0];
// Construct an SObject scope to pass to the execute method
// TODO: Support non-SObject scopes
List<Id> scopeErrorIds = error.JobScope.split(',');
SObjectType sObjectType = scopeErrorIds[0].getSobjectType();
List<SObject> retryScope = new List<SObject>();
for (Id errorId : scopeErrorIds) {
SObject sObj = null;
switch on error.ApexClassName {
when 'BatchAccountHistory', 'BatchAccountHistory_2' {
sObj = Schema.getGlobalDescribe().get('AccountHistory').newSObject();
sObj.Id = errorId;
}
when 'ClaimHistory' {
sObj = Schema.getGlobalDescribe().get('ClaimHistory').newSObject();
sObj.Id = errorId;
}
when else {
sObj = sObjectType.newSObject(errorId);
}
}
if (sObj != null) {
retryScope.add(sObj);
}
}
// Invoke the execute method on the original batch apex job class
Type apexJobClassType = Type.forName(error.ApexClassName);
Database.Batchable<SObject> batchJob = (Database.Batchable<SObject>) apexJobClassType.newInstance();
batchJob.execute(ctx, retryScope);
}
An better solution for this issue would probably to extend the brf_BatchableErrorHandler interface with a method that returns the sobject type i.e. 'ClaimHistory' or 'Account' etc.. => then utilize that in the execute method