Skip to content

Run Apex

Snippets to execute and monitor Apex tasks.

Apex Tests

CLI command to run the test classes Class1_Test and Class2_Test (including code coverage):

sf apex run test --class-names Class1_Test --class-names Class2_Test --code-coverage

SOQL query (on ApexTestRunResult) to monitor the progress (use Id returned in previous step):

SELECT Id, ClassesEnqueued, ClassesCompleted, MethodsCompleted, MethodsFailed, EndTime, Status FROM ApexTestRunResult WHERE AsyncApexJobId = ''

Batch Apex

Anonymous Apex to run the (batchable) class BatchableApexClass with a batch size of 200:

Id batchJobId = Database.executeBatch(new BatchableApexClass(), 200);
System.debug(batchJobId);

SOQL query (on AsyncApexJob) to monitor the progress (use Id returned in previous step):

SELECT Status, TotalJobItems, JobItemsProcessed, NumberOfErrors, ExtendedStatus FROM AsyncApexJob WHERE Id = ''