I've created a schedulable class that retrieves info from SetupAuditTrail everyday and creates a big object records on a daily basis. The schedulable class works fine. But when I run the test class, it is giving me the following error.
System.UnexpectedException: A callout was unsuccessful because of pending uncommitted work related to a process, flow, or Apex operation. Commit or roll back the work, and then try again.
Here is my test class:
@isTest
private class BM_BO_SetUpAuditTrail_BackUpTest {
@isTest(SeeAllData=true)
public static void testScheduledApex() {
String sch = '0 0 1 * * ? 2022';
Test.startTest();
String JobId = System.schedule('Process AuditTrail Backup', sch, new BM_BO_SetUpAuditTrail_BackUp());
Test.stopTest();
System.assert(JobId != null);
}
}
Here is my class itself:
global class BM_BO_SetUpAuditTrail_BackUp implements Schedulable {
global void execute(SchedulableContext SC) {
List<SetupAuditTrail> listSAT = [SELECT createdbyid, responsiblenamespaceprefix, action, section, delegateuser, createddate
FROM SetupAuditTrail
WHERE createddate = YESTERDAY ];
List<BM_BO_SetUpAuditTrail_BackUp__b> listBMSAT = new List<BM_BO_SetUpAuditTrail_BackUp__b>();
try{
for(SetupAuditTrail SATBD : listSAT){
BM_BO_SetUpAuditTrail_BackUp__b sat = new BM_BO_SetUpAuditTrail_BackUp__b();
sat.BM_DateTime__c = SATBD.createddate;
sat.BM_UserId__c = SATBD.createdbyid;
sat.BM_SrcNameSpace__c = SATBD.responsiblenamespaceprefix;
sat.BM_Action__c = SATBD.action;
sat.BM_Section__c = SATBD.section;
sat.BM_Delegation__c = SATBD.delegateuser;
listBMSAT.add(sat);
}
database.insertimmediate(listBMSAT);
} catch(DMLException e){
System.debug('The following exception has occurred: ' + e.getMessage());
}
}
}
Could you please tell me what I did wrong here?