Sunday, October 10, 2021

How to Execute, Monitor and Abort a batch class in Salesforce

Lets understand how to execute, monitor and abort a batch


How to execute a Batch

Syntax:

<Class name> <object name> = new <class name> ();

Database.executebatch(object name, size of batch);


For example:

Batch class name: Calci

Open execute anonymous window in developer console and write:

Calci obj = new Calci();

Database.executebatch(obj,200);

Click execute!


How to Monitor the batch Job

Search Apex jobs in the setup and open it.

All the batch jobs will come in this page with completed/running/on hold.


Abort the Running Batch Job

To kill long running future Apex jobs from Workbench

  1. Go to Workbench.
  2. Select the Environment.
  3. Choose a lower API version to 32.0. 
  4. Enter username and password.
  5. Go to Query | SOQL Query. Run the following query to find a queued status scheduled or future Apex job id to abort:
  6. SELECT ApexClassId,CompletedDate,CreatedById,CreatedDate,ExtendedStatus,Id,JobItemsProcessed,JobType,LastProcessed,LastProcessedOffset,MethodName,NumberOfErrors,ParentJobId,Status,TotalJobItems FROM AsyncApexJob where status = 'queued'
  7. Once you find the jobId to delete: Go to Utilities | Apex Execute.
  8. Replace text "JobID" in the following line of code with the Job Id you found in the query in step 5, then click Execute.
  9. System.abortJob('JobID');
  10. The scheduled Apex job should get deleted.

Let me know your thoughts in the comment section!!

No comments:

Post a Comment