{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":3798,"path":"build-workflows/legacy-workflow/t_CancelingAWorkflow.md","title":"Cancel a workflow","bundle":null,"doc_type":null,"release":"australia","body":"\n# Cancel a workflow\n\nCanceling a workflow stops the workflow from executing and sets the workflow context **State** to **Canceled**. To cancel an executing workflow, you can use the cancelContext\\(context\\) script. You can define an onCancel script to clean up unresolved workflow activities.\n\n## Before you begin\n\nRole required: snc\\_required\\_script\\_writer\\_permission and either workflow\\_admin or admin\n\n## About this task\n\nCanceling a workflow attempts to stop the workflow gracefully by injecting a cancel command into the workflow engine.\n\n## Procedure\n\n1.  Navigate to **All** &gt; **Workflow** &gt; **Active Contexts**.\n\n2.  Select a workflow context record.\n\n3.  Configure form layout to add **On-cancel script** field to form.\n\n    For detailed information about configuring form layout, see [Configuring the form layout](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/platform-administration/configure-form-layout.md).\n\n4.  Select the **Cancel** related link.\n\n    A confirmation appears.\n\n    \\[Omitted image \"CancelConfirmation.png\"\\] Alt text: Cancel Confirmation\n\n5.  Click **OK**.\n\n    The workflow engine attempts to cancel the workflow gracefully.\n\n    \\[Omitted image \"WaitForCancel.png\"\\] Alt text: Wait for Cancel\n\n    If the workflow does not respond to the cancel command, the Force Cancel option appears.\n\n    \\[Omitted image \"ForceCancel.png\"\\] Alt text: Force Cancel\n\n6.  Click **Force cancel** to interrupt the thread the workflow is actively executing or click **Continue waiting** to continue waiting for the workflow to cancel gracefully.\n\n    **Warning:** Whenever possible, allow a workflow to cancel gracefully. Forcing a workflow to cancel can leave related workflows and scripts in an unresolved state. You can use an on-cancel script to clean up unresolved artifacts from a cancelled workflow.\n\n\n**Parent Topic:**[Administering workflow contexts](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/build-workflows/legacy-workflow/r_AdministeringWorkflowContexts.md)\n\n## Cancel a workflow with the cancelContext\\(context\\) script\n\nTo cancel an executing workflow, you can use the cancelContext\\(context\\) script. This script can be useful in cases where a workflow must be canceled in response to an event or where a user must manually cancel a workflow.\n\n### Before you begin\n\nRole required: admin\n\n### About this task\n\nFor more information, see the [Workflow - cancelContext\\(GlideRecord context\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/api-reference/server-api-reference/c_Workflow_api.md).\n\n## Define an on-cancel script\n\nCanceling 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.\n\n### About this task\n\nOn-cancel scripts run asynchronously from the global scope. Your instance workload determines when the system schedules and runs the on-cancel script.\n\n**Important:** Since the system runs on-cancel scripts from the global scope, they cannot call or run scoped script includes.\n\n### Procedure\n\n1.  Navigate to **All** &gt; **Workflow** &gt; **Workflow Versions**.\n\n2.  Select a workflow version that you have checked out.\n\n    Workflow versions that are not checked out are not editable.\n\n3.  Edit the **On-cancel script** field.\n\n    You may need to configure the form to add this field.\n\n4.  Click **Update**.\n\n\n### Example\n\nThis example script adds a comment to a Requested Item \\[sc\\_req\\_item\\] record indicating the workflow for that request has been canceled.\n\n```\nvar grContext = new GlideRecord(\"wf_context\"); \ngrContext.get(context_sys_id); \nvar grReq = new GlideRecord(\"sc_req_item\");\n \n// The current record may not exist, make sure it exists before modifying it.\nif (grReq.get(grContext.id)) {\n    grReq.comments = \"The workflow processing this item was Canceled. Contact your system administrator for further information.\";\n    grReq.update();\n}\n```\n\n"}}