---
schema_version: "1"
id: d528c469cb51d63d414ba3f7a7faedc146362c89bc53ba39a039e2bdfabae1d7
title: Update Batch Run Status Records After an Abort
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/retail_api/cgtpm_abort_job_chain_update_status.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.retail_api.meta/retail_api/cgtpm_abort_job_chain_update_status.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: 9b20a5f9ced9b2a68b9b5b1f17c46858a06ea330947e3e5d374448a0871a1f8f
description: Change the state of Batch Run Status records that stay in the Running state after an abort.
---
# Update Batch Run Status Records After an Abort

Change the state of Batch Run Status records that stay in the Running state after an abort.

An abort does not update the Batch Run Status records of the stopped run. They stay in the Running state. This step is optional. CG Cloud does not do it for you.

The Batch\_State\_\_c field is a restricted picklist. Its values are Running, Error, Done, and Fatal. No value means aborted. The closest value for a stopped run is Fatal.

Run this update only after step 2 returns 200.

## Which Records to Update

A job chain worker controls two kinds of records.

- Job chain controller records have a Batch\_Name\_\_c value that starts with ProcessQueueWorker\_.
- Job records have a Job\_Name\_\_c value that starts with JobChainWorker:, such as JobChainWorker:Promotions.

The query uses two limits.

- A created date limit. The batch reads only records created on or after the date that you choose. This keeps the batch from reading the full history.
- An idle time limit. The batch reads only records whose Last\_Update\_Date\_\_c is older than the number of minutes that you choose. A running job chain worker updates this field about once a minute, so a running chain is never changed.

The query does not filter by sales org. One job chain worker runs the chains of all sales orgs on a platform. After an abort, stale records can belong to any of those sales orgs.

## Apex Batch Class

public class JobChainAbortCleanupBatch implements Database.Batchable<SObject> {
    private final Datetime createdFrom;
    private final Datetime staleCutoff;

    public JobChainAbortCleanupBatch(Datetime createdFrom, Integer staleMinutes) {
        this.createdFrom = createdFrom;
        this.staleCutoff = System.now().addMinutes(-1 \* staleMinutes);
    }

    public Database.QueryLocator start(Database.BatchableContext ctx) {
        return Database.getQueryLocator(\[
            SELECT Id
            FROM cgcloud\_\_Batch\_Run\_Status\_\_c
            WHERE cgcloud\_\_Batch\_State\_\_c = 'Running'
              AND CreatedDate >= :createdFrom
              AND cgcloud\_\_Last\_Update\_Date\_\_c < :staleCutoff
              AND (cgcloud\_\_Batch\_Name\_\_c LIKE 'ProcessQueueWorker%'
                   OR cgcloud\_\_Job\_Name\_\_c LIKE 'JobChainWorker:%')
        \]);
    }

    public void execute(Database.BatchableContext ctx, List<cgcloud\_\_Batch\_Run\_Status\_\_c> scope) {
        for (cgcloud\_\_Batch\_Run\_Status\_\_c brs : scope) {
            brs.cgcloud\_\_Batch\_State\_\_c = 'Fatal';
        }
        update scope;
    }

    public void finish(Database.BatchableContext ctx) {
        System.debug('JobChainAbortCleanupBatch finished.');
    }
}

The class uses the cgcloud namespace for objects and fields.

## Run the Batch

This example reads records created in the last 7 days. It updates records that were idle for more than 10 minutes.

Datetime createdFrom = System.now().addDays(-7);
Database.executeBatch(new JobChainAbortCleanupBatch(createdFrom, 10), 200);