Defining an on-cancel script

Canceling a workflow can leave records or scripts in an unresolved state. For example, canceling a service catalog workflow may leave catalog items in the requesting user's cart. An administrator can specify an On-cancel script that runs when the workflow transitions to the Canceled state. This script can notify users, log information, or resolve the state of any scripts run within a workflow activity. The sys_id of the workflow context is available in this script using the context_sys_id variable.

About this task

To define this script:

Procedure

  1. Navigate to Workflow > Workflow Versions.
  2. Select a workflow version that you have checked out.
    Workflow versions that are not checked out are not editable.
  3. Edit the On-cancel script field.
    You may need to configure the form to add this field.
  4. Click Update.

Example

This example script adds a comment to a Requested Item [sc_req_item] record indicating the workflow for that request has been canceled.
var grContext = new GlideRecord("wf_context"); 
grContext.get(context_sys_id); 
var grReq = new GlideRecord("sc_req_item");
 
// The current record may not exist, make sure it exists before modifying it.
if (grReq.get(grContext.id)) {
    grReq.comments = "The workflow processing this item was Canceled. Contact your system administrator for further information.";
    grReq.update();
}