Skip to content
Canceling Delayed Async Execution Example

Canceling Delayed Async Execution Example

Breezz allows you to revoke or cancel queued asynchronous jobs prior to execution using the cancelAsyncRequest() method. This is useful when a record no longer meets the necessary criteria after initial evaluation within a Step execution context.


Key Execution Rules

  1. Enable finishRecordProcessing: To perform post-evaluations and conditionally cancel requests, initRecordProcessing() must return true. This instructs the engine to invoke the finishRecordProcessing() lifecycle hook.
  2. Revoking Requests: Calling cancelAsyncRequest(recordId) removes the specified record ID from the pending DelayedAsyncJob queue before the scheduled worker runs.

Complete Apex Step Implementation

Below is a complete example of scheduling a delayed asynchronous task in initRecordProcessing() and conditionally canceling it during finishRecordProcessing() if the Account name matches 'CanceledAccount':

public class ConditionalContactGenerator extends forvendi.Step {

    public ConditionalContactGenerator() {
        super(ConditionalContactGenerator.class.getName());
    }

    public override Boolean initRecordProcessing(Object record, Object optionalOldRecord) {
        Account accountRecord = (Account) record;
        
        // Queue a delayed asynchronous job starting from current timestamp
        addDelayedAsyncJob(accountRecord.Id, Datetime.now());
        
        // Return true to enable finishRecordProcessing execution pass
        return true;
    }

    public override void finishRecordProcessing(Object record, Object optionalOldRecord) {
        Account tempAccount = (Account) record;
        
        // Revoke async request if record name matches cancellation criteria
        if (tempAccount.Name == 'CanceledAccount') {
            cancelAsyncRequest(tempAccount.Id);
        }
    }

    public override void executeAsyncProcess(Map<String, forvendi.AsyncJobInfo> asyncJobsByRecordKey) {
        List<Contact> contactsToCreate = new List<Contact>();
        
        // Process remaining, non-canceled queued jobs
        for (String recordId : asyncJobsByRecordKey.keySet()) {
            contactsToCreate.add(new Contact(
                LastName = 'Contact ' + System.now(), 
                AccountId = recordId
            ));
        }
        
        getContext().addToInsert(contactsToCreate);
    }
}

Execution Walkthrough

  1. Initialization: When an Account is inserted or updated, initRecordProcessing() executes and adds the Account ID to the delayed job queue (addDelayedAsyncJob).
  2. Conditional Validation: Because initRecordProcessing() returns true, the engine executes finishRecordProcessing(). If Account.Name == 'CanceledAccount', cancelAsyncRequest(tempAccount.Id) immediately removes the ID from the queue.
  3. Background Worker Execution: Any records remaining in the queue after the delay window expires will be passed to executeAsyncProcess() for bulk creation of related Contacts.

💡 Apex API Reference: To explore all lifecycle methods (initRecordProcessing, finishRecordProcessing, cancelAsyncRequest), visit Breezz APEX API - Step Reference.