1

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?

1
  • Where is the callout happening? Is there a testSetup available ? May 26 at 9:33

1 Answer 1

1

The reason why it only happens in a Test class is because query on big object is being done as a callout, and a callout is not allowed in the Test class in general.

Per the documentation:

To contain test DML calls to the target big object, use a mocking framework with the batch Apex stub API instead.

Documentation also mentions that it is also not allowed in Apex tests to use mixed DML calls on standard/custom object and Big object (for example, inserting an Account record and then query a Big Object) and they would fail with an error.

See documentation on Building a Mocking Framework with the Stub API

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.