---
sourceDocument: Xanadu API Reference
sourceDocumentLink: https://servicenow-prod.fluidtopics.net/r/xanadu/api-reference

 Release :

    - xanadu

ft:locale :

    - en-US

ft:publication_title :

    - Xanadu API Reference

ft:clusterId :

    - crapiref

bundleId :

    - crapiref

workflow :

    - Creator


---

# Workflow - Global

# Workflow - Global {#ariaid-title1}

* Release version: Xanadu
* 
* Updated August 1, 2024
* 
* ![](https://www.servicenow.com/docs/portal-asset/ico-clock) 9 minutes to read

The Workflow script include provides methods that interface with the Workflow engine.

Use these methods to manipulate workflows.

## Workflow - broadcastEvent(String contextId, String eventName) {#ariaid-title2}

Sends the specified event (message) into the workflow context to pass along to the
executing activities.
Typical use of this method is to enable activities that wait for some action to occur
before proceeding. For additional information on using broadcastEvent,
refer to [Workflow event-specific
functions](https://www.servicenow.com/docs/access?context=r_WorkflowEventSpecificFunctions&version=xanadu&pubname=xanadu-build-workflows&ft:locale=en-US).

For a list of the available OOB events, refer to [Workflow events in the base
system](https://www.servicenow.com/docs/access?context=r_WorkflowEventsInTheBaseSystem&version=xanadu&pubname=xanadu-build-workflows&ft:locale=en-US).
{#r_WF-broadcastEvent_S_S__table_oqb_tqr_nt__entry__3}

| Name | Type | Description |
|-|-|-|
| contextId | String | The context ID. |
| eventName | String | The name of the event. |
[Table 1. Parameters]

{#r_WF-broadcastEvent_S_S__table_oqb_tqr_nt} {#r_WF-broadcastEvent_S_S__table_pqb_tqr_nt__entry__2}

| Type | Description |
|-|-|
| void |   |
[Table 2. Returns]

{#r_WF-broadcastEvent_S_S__table_pqb_tqr_nt}  

    //where current is a task record with a workflow context
          var wf = new Workflow().getRunningFlows(current);
          while(wf.next()) {     
              new Workflow().broadcastEvent(wf.sys_id, 'resume');
          }

## Workflow - cancel(GlideRecord record) {#ariaid-title3}

Cancels all running workflows on this record by broadcasting the
cancel event to activities in all running workflows on this
record.
{#r_WF-cancel_GR__table_qqb_tqr_nt__entry__3}

| Name | Type | Description |
|-|-|-|
| record | GlideRecord | GlideRecord on any table. All workflows running on this record will be cancelled. |
[Table 3. Parameters]

{#r_WF-cancel_GR__table_qqb_tqr_nt} {#r_WF-cancel_GR__table_rqb_tqr_nt__entry__2}

| Type | Description |
|-|-|
| void |   |
[Table 4. Returns]

{#r_WF-cancel_GR__table_rqb_tqr_nt}  

    //get workflow helper
          var workflow = new Workflow();
          //cancel all the workflows, where current is a task record with a workflow context
          workflow.cancel(current);
          gs.addInfoMessage(gs.getMessage("Workflows for {0} have been cancelled", current.getDisplayValue()));

## Workflow - cancelContext(GlideRecord context) {#ariaid-title4}

Cancels this running context by broadcasting a cancel event to
activities in this workflow context.
{#r_WF-cancelContext_GR__table_sqb_tqr_nt__entry__3}

| Name | Type | Description |
|-|-|-|
| context | GlideRecord | GlideRecord of the running context to cancel. |
[Table 5. Parameters]

{#r_WF-cancelContext_GR__table_sqb_tqr_nt} {#r_WF-cancelContext_GR__table_tqb_tqr_nt__entry__2}

| Type | Description |
|-|-|
| void |   |
[Table 6. Returns]

{#r_WF-cancelContext_GR__table_tqb_tqr_nt}  

    // If a workflow has started for this item, cancel it, where current is a task record with a workflow context
          if ((current.stage == 'Request Cancelled') && current.context 
              && !current.context.nil()) {
          var w = new Workflow();
          var now_GR = new GlideRecord('wf_context');
          
          if (now_GR.get(current.context)) 
              w.cancelContext(now_GR);
          }

## Workflow - deleteWorkflow(GlideRecord current) {#ariaid-title5}

Deletes all the workflows on the record.
{#r_WF-deleteWorkflow_GR__table_wp5_d1s_nt__entry__3}

| Name | Type | Description |
|-|-|-|
| current | GlideRecord | GlideRecord for which the caller wants to delete all workflows. This can be any record on any table. |
[Table 7. Parameters]

{#r_WF-deleteWorkflow_GR__table_wp5_d1s_nt} {#r_WF-deleteWorkflow_GR__table_xp5_d1s_nt__entry__2}

| Type | Description |
|-|-|
| void |   |
[Table 8. Returns]

{#r_WF-deleteWorkflow_GR__table_xp5_d1s_nt}  

    //where current is a task record with a workflow context
          var wkfw = new Workflow();
          wkfw.deleteWorkflow(current);
        
## Workflow - fireEvent(GlideRecord eventRecord, String eventName, Object eventParms) {#ariaid-title6}

Fires the named event of a running workflow on the input record.
Used in Activities Approval Coordinator, Timer, Lock, and some others.
{#r_WF-fireEvent_GR_S__table_kqb_tqr_nt__entry__3}

| Name | Type | Description |
|-|-|-|
| eventRecord | GlideRecord | Event record listed in the Workflow Executing Activites \[wf_executing\] table. |
| eventName | String | The name of the event to send to the executing workflow. |
| eventParms | Object | Optional. Parameters in JSON format used by the event. |
[Table 9. Parameters]

{#r_WF-fireEvent_GR_S__table_kqb_tqr_nt} {#r_WF-fireEvent_GR_S__table_lqb_tqr_nt__entry__2}

| Type | Description |
|-|-|
| void |   |
[Table 10. Returns]

{#r_WF-fireEvent_GR_S__table_lqb_tqr_nt}  

    // where current is a task record with a workflow context
      var w = new Workflow(); 
      w.fireEvent(current, 'execute');

## Workflow - fireEventById(String eventRecordId, String eventName) {#ariaid-title7}

Fires the named event on the record specified by record ID.
Used in Activities Approval Coordinator,
Timer, Lock, and some others.
{#r_WF-fireEventById_S_S__table_mqb_tqr_nt__entry__3}

| Name | Type | Description |
|-|-|-|
| eventRecordId | String | The sys_id of the glide record. |
| eventName | String | The name of the event to send to the executing workflow. |
[Table 11. Parameters]

{#r_WF-fireEventById_S_S__table_mqb_tqr_nt} {#r_WF-fireEventById_S_S__table_nqb_tqr_nt__entry__2}

| Type | Description |
|-|-|
| void |   |
[Table 12. Returns]

{#r_WF-fireEventById_S_S__table_nqb_tqr_nt}  

    var wkfw = new Workflow();
          wkfw.fireEventById('f2400ec10b0a3c1c00ca5bb5c6fae427','Timer');

## Workflow - getContexts(GlideRecord record) {#ariaid-title8}

Returns all workflow contexts for a specified record.
{#r_WF-getContexts_GR__table_sp5_d1s_nt__entry__3}

| Name | Type | Description |
|-|-|-|
| record | [GlideRecord](https://servicenow-prod.fluidtopics.net/nRmUiaC7a08cr2N5iZGqoQ#c_GlideRecordAPI "The GlideRecord API is used for database operations.") | GlideRecord for which the caller wants a list of all workflow contexts. This can be any record on any table for which the caller wants the running workflow contexts. |
[Table 13. Parameters]

{#r_WF-getContexts_GR__table_sp5_d1s_nt} {#r_WF-getContexts_GR__table_tp5_d1s_nt__entry__2}

| Type | Description |
|-|-|
| GlideRecord | GlideRecord in the Workflow context \[wf_context\] table filtered for all workflow contexts for the specified record (in any state, such as running, cancelled, finished). |
[Table 14. Returns]

{#r_WF-getContexts_GR__table_tp5_d1s_nt}  

    //where current is a task record with a workflow context
    var wkfw = new Workflow();
    var context = wkfw.getContexts(current);
    while (context.next())
      gs.print(context.started);

## Workflow - getEstimatedDeliveryTime(String workflowId) {#ariaid-title9}

Gets the estimated time for a workflow to complete.
{#r_WF-getEstimatedDeliveryTime_S__table_yp5_d1s_nt__entry__3}

| Name | Type | Description |
|-|-|-|
| workflowId | String | Sys_id of the workflow (table wf_workflow) to get the estimated run time. |
[Table 15. Parameters]

{#r_WF-getEstimatedDeliveryTime_S__table_yp5_d1s_nt} {#r_WF-getEstimatedDeliveryTime_S__table_zp5_d1s_nt__entry__2}

| Type | Description |
|-|-|
| String | Display value from a GlideDuration (e.g., 3 days), or blank if unknown. |
[Table 16. Returns]

{#r_WF-getEstimatedDeliveryTime_S__table_zp5_d1s_nt}  

    var wkfw = new Workflow();
          gs.print(wkfw.getEstimatedDeliveryTime('b99a866a4a3623120074c033e005418f'));

2 Days

## Workflow - getEstimatedDeliveryTimeFromWFVersion(GlideRecord wfVersion) {#ariaid-title10}

Get the estimated elapsed execution time for the workflow version.
{#r_WF-getEstimatedDeliveryTimeFromWFVersion_GR__table_aq5_d1s_nt__entry__3}

| Name | Type | Description |
|-|-|-|
| wfVersion | GlideRecord | GlideRecord on table wf_workflow_version of a specific workflow version for which the caller wants the estimated during of executing. |
[Table 17. Parameters]

{#r_WF-getEstimatedDeliveryTimeFromWFVersion_GR__table_aq5_d1s_nt} {#r_WF-getEstimatedDeliveryTimeFromWFVersion_GR__table_bq5_d1s_nt__entry__2}

| Type | Description |
|-|-|
| String | Display value from a GlideDuration (e.g., 3 days), or blank if unknown. |
[Table 18. Returns]

{#r_WF-getEstimatedDeliveryTimeFromWFVersion_GR__table_bq5_d1s_nt}  

    //where current is a task record with a workflow context
          var wkfw = new Workflow();
          var context = wkfw.getContexts(current);
          gs.print(wkfw.getEstimatedDeliveryTimeFromWFVersion(context.wf_version));

## Workflow - getReturnValue(String workflowID, Number amount, Boolean result) {#ariaid-title11}

Gets the appropriate workflow return value for the input workflow ID.
This is either the workflow checked out by the current user or the published workflow with the most recent date.
This is either the workflow checked out by the current user or the published workflow with
the most recent date. This method is available starting with the Fuji release.
{#r_WF-getReturnValue_S_N_B__table_w2y_bjp_mt__entry__3}

| Name | Type | Description |
|-|-|-|
| workflowID | String | The sys_id of the workflow (table wf_workflow) |
| amount | Number | amount |
| result | Boolean | True, if true |
[Table 19. Parameters]

{#r_WF-getReturnValue_S_N_B__table_w2y_bjp_mt} {#r_WF-getReturnValue_S_N_B__table_x2y_bjp_mt__entry__2}

| Type | Description |
|-|-|
| ??? | The return value of the workflow as specified by the Return Value activity. Workflows without a Return Value activity return a null value. |
[Table 20. Returns]

{#r_WF-getReturnValue_S_N_B__table_x2y_bjp_mt}  

    var wkfw = new Workflow();
          wkfw.getReturnValue('context');

Output:

    *** Script: b99a866a4a3623120074c033e005418f
          
## Workflow - getRunningFlows(GlideRecord record) {#ariaid-title12}

Gets all the currently running workflow contexts for the input record.
The input record is any record on any table for which the caller wants the running workflow
contexts.
{#r_WF-getRunningFlows_GR__table_qp5_d1s_nt__entry__3}

| Name | Type | Description |
|-|-|-|
| record | GlideRecord | GlideRecord of the record for which the caller wants a list of all running workflows. |
[Table 21. Parameters]

{#r_WF-getRunningFlows_GR__table_qp5_d1s_nt} {#r_WF-getRunningFlows_GR__table_rp5_d1s_nt__entry__2}

| Type | Description |
|-|-|
| GlideRecord | GlideRecord on table wf_context and filtered for all executing workflow contexts. |
[Table 22. Returns]

{#r_WF-getRunningFlows_GR__table_rp5_d1s_nt}  

    //where current is a task record with a workflow context
          var wf = new Workflow().getRunningFlows(current);
          while(wf.next()) {     
              new Workflow().broadcastEvent(wf.sys_id, 'pause');
          }

## Workflow - getVersion(String workflowID) {#ariaid-title13}

Gets the appropriate workflow version for the input workflow ID.
This is either the workflow checked out by the current user or the published workflow with the most recent date.
This is either the workflow checked out by the current user or the published workflow with
the most recent date.
{#r_WF-getVersion_S__table_y2y_bjp_mt__entry__3}

| Name | Type | Description |
|-|-|-|
| workflowID | String | The sys_id of the workflow (table wf_workflow) |
[Table 23. Parameters]

{#r_WF-getVersion_S__table_y2y_bjp_mt} {#r_WF-getVersion_S__table_z2y_bjp_mt__entry__2}

| Type | Description |
|-|-|
| none |   |
[Table 24. Returns]

{#r_WF-getVersion_S__table_z2y_bjp_mt}  

    var wkfw = new Workflow();
          wkfw.getVersion('b99a866a4a3623120074c033e005418f');

## Workflow - getVersionFromName(String workflowName) {#ariaid-title14}

Returns the appropriate workflow version for the input workflow name.
See getVersion() for more information.
{#r_WF-getVersionFromName_S__table_y2y_bjp_mt__entry__3}

| Name | Type | Description |
|-|-|-|
| workflowName | String | Name of the workflow (table wf_workflow) |
[Table 25. Parameters]

{#r_WF-getVersionFromName_S__table_y2y_bjp_mt} {#r_WF-getVersionFromName_S__table_z2y_bjp_mt__entry__2}

| Type | Description |
|-|-|
| void |   |
[Table 26. Returns]

{#r_WF-getVersionFromName_S__table_z2y_bjp_mt}  

    var wkfw = new Workflow();
          wkfw.getVersionFromName('Emergency Change');

## Workflow - getWorkflowFromName(String workflowName) {#ariaid-title15}

Returns the sys_id of the workflow associated with the specified workflow
name.
{#r_WF-getWorkflowFromName_S__table_afy_bjp_mt__entry__3}

| Name | Type | Description |
|-|-|-|
| workflowName | String | Name of the workflow. |
[Table 27. Parameters]

{#r_WF-getWorkflowFromName_S__table_afy_bjp_mt} {#r_WF-getWorkflowFromName_S__table_bfy_bjp_mt__entry__2}

| Type | Description |
|-|-|
| String | The sys_id of the workflow associated with the passed in name. |
[Table 28. Returns]

{#r_WF-getWorkflowFromName_S__table_bfy_bjp_mt}  

    var wflw = new Workflow();
          gs.print(wflw.getWorkflowFromName('Emergency Change'));

## Workflow - hasWorkflow(GlideRecord record) {#ariaid-title16}

Determines if a specified record has any workflow contexts associated to it.
This includes running and completed workflow contexts.
{#r_WF-hasWorkflow_GR__table_iqb_tqr_nt__entry__3}

| Name | Type | Description |
|-|-|-|
| record | GlideRecord | GlideRecord under scrutiny. This GlideRecord can be from any table. |
[Table 29. Parameters]

{#r_WF-hasWorkflow_GR__table_iqb_tqr_nt} {#r_WF-hasWorkflow_GR__table_jqb_tqr_nt__entry__2}

| Type | Description |
|-|-|
| Boolean | True, if record has associated workflow; otherwise, returns False. |
[Table 30. Returns]

{#r_WF-hasWorkflow_GR__table_jqb_tqr_nt}  

    var wkfw = new Workflow();
          gs.print(wkfw.hasWorkflow('f2400ec10b0a3c1c00ca5bb5c6fae427'));

false

## Workflow - restartWorkflow(GlideRecord current, Boolean maintainStateFlag) {#ariaid-title17}

Recalculates the approvals and tasks for a workflow by adding new approvals and tasks,
while not resetting current approvals and tasks.
You can use this method to perform such tasks as adding a company to a change request,
without resetting the current approvals for companies already in the workflow.
{#r_WF-restartWorkflow_GR_B__table_up5_d1s_nt__entry__3}

| Name | Type | Description |
|-|-|-|
| current | GlideRecord | GlideRecord of the record this workflow is executing. This can by any record on any table. |
| maintainStateFlag | Boolean | Flag that indicates whether to maintain all approvals and tasks in their current state. Valid values: * true: Maintain all approvals and tasks in their current state. * false: Update all approval and task states. {#r_WF-restartWorkflow_GR_B__ul_fwb_fh3_2nb} |
[Table 31. Parameters]

{#r_WF-restartWorkflow_GR_B__table_up5_d1s_nt} {#r_WF-restartWorkflow_GR_B__table_vp5_d1s_nt__entry__2}

| Type | Description |
|-|-|
| void |   |
[Table 32. Returns]

{#r_WF-restartWorkflow_GR_B__table_vp5_d1s_nt}  
This example shows the workflow being restarted with the approval file changing from Rejected to
Requested.

    (function(){
      var comment = 'Workflow Restarted - the Approval Field changing from Rejected to Requested';
      var gLock = new GlideRecordLock(current);
        gLock.setSpinWait(50);
      if (gLock.get()) {
        new Workflow().restartWorkflow(current, false);
        current.setDisplayValue('approval_history', comment);
      }
    })

## Workflow - runFlows(GlideRecord record, String operation) {#ariaid-title18}

Runs all workflows for a given record in a given table and its descendant tables.
Sample usage can be seen in the Script Includes "SNC - Run parent workflows", and "SNC -
Run parent workflows (Approval)".
{#r_WF-runFlows_GR_S__table_u2y_bjp_mt__entry__3}

| Name | Type | Description |
|-|-|-|
| record | GlideRecord | GlideRecord to run workflows against. |
| operation | String | Database operation. Valid values: * insert * update * delete {#r_WF-runFlows_GR_S__ul_mt2_yds_ckb} |
[Table 33. Parameters]

{#r_WF-runFlows_GR_S__table_u2y_bjp_mt} {#r_WF-runFlows_GR_S__table_v2y_bjp_mt__entry__2}

| Type | Description |
|-|-|
| void |   |
[Table 34. Returns]

{#r_WF-runFlows_GR_S__table_v2y_bjp_mt}  

    var now_GR = new GlideRecord('wf_test');
    now_GR.addQuery('parent', current.parent);
    now_GR.addQuery('sys_id','!=',current.sys_id);
    now_GR.query();
    while(now_GR.next()) {
        new Workflow().runFlows(now_GR, 'update');
    }

## Workflow - startFlow(String workflowId, GlideRecord current, String operation, Array
vars) {#ariaid-title19}

Starts a specified workflow.
See script include WorkflowScheduler and Business Rule "Start Workflow" on table
sc_req_item for examples of use.
{#r_WF-startFlow_S_GR_S_A__table_f3h_hx4_mt__entry__3}

| Name | Type | Description |
|-|-|-|
| workflowId | String | The sys_id of the workflow to start. This sys_id refers to table wf_workflow. |
| current | GlideRecord | The record to use as current in this workflow. This is normally from the Table field of the workflow properties for this workflow. |
| operation | String | The operation to perform on current. Possible values: insert, update, delete. |
| vars | Array | Collection of variables to add to the workflow |
[Table 35. Parameters]

{#r_WF-startFlow_S_GR_S_A__table_f3h_hx4_mt}  

    ////where current is a task record with a workflow context
          var w = new Workflow();
          var context = w.startFlow(id, current, current.operation(), getVars());

## Workflow - startFlowFromContextInsert(GlideRecord context, String operation) {#ariaid-title20}

Helper method for business rule Auto start on context.
{#r_WF-startFlowFromContextInsert_GR_S__table_h3h_hx4_mt__entry__3}

| Name | Type | Description |
|-|-|-|
| context | GlideRecord | GlideRecord on table wf_context of a new record (the "current" record in the business rule). |
| operation | String | Database operation being performed. One of insert, update, delete. |
[Table 36. Parameters]

{#r_WF-startFlowFromContextInsert_GR_S__table_h3h_hx4_mt} {#r_WF-startFlowFromContextInsert_GR_S__table_i3h_hx4_mt__entry__2}

| Type | Description |
|-|-|
| void |   |
[Table 37. Returns]

{#r_WF-startFlowFromContextInsert_GR_S__table_i3h_hx4_mt}  

    //where current is a task record with a workflow context
          current.name = current.workflow_version.name;
          current.started_by.setValue(gs.userID());
          
          if (gs.nil(current.id)) {
            var now_GR = new GlideRecord('wf_workflow_execution');
            now_GR.name = current.name;
            now_GR.insert();
          
            current.table = 'wf_workflow_execution';
            current.id = now_GR.sys_id;
          }
          
          var wf = new Workflow();
          wf.startFlowFromContextInsert(current, current.operation())

## Workflow - startFlowRetroactive(String workflowId, Number retroactiveMSecs, GlideRecord
current, String operation, Array, ???) {#ariaid-title21}

Used by business rule Start Workflow on table task_sla. This starts
a workflow and the extra arguments to this method are used by activity "Timer" to pause the
execution of the workflow for some duration.
{#r_WF-startFlowRetroactive_S_N_GR_S_A__table_j3h_hx4_mt__entry__3}

| Name | Type | Description |
|-|-|-|
| workflowID | String | The sys_id of the workflow to start. This sys_id refers to table wf_workflow. |
| retroactiveMSecs | Number | Delay in milliseconds used by Activity Timer. |
| current | GlideRecord | GlideRecord of the record to use as current in this workflow. This is normally from the Table field of the workflow properties for this workflow |
| operation | String | Database operation being performed.One of insert, update, delete. |
| vars | Array | Collection of variables to add to the workflow. |
| withSchedule | ??? | Schedule used by Activity Timer |
[Table 38. Parameters]

{#r_WF-startFlowRetroactive_S_N_GR_S_A__table_j3h_hx4_mt} {#r_WF-startFlowRetroactive_S_N_GR_S_A__table_k3h_hx4_mt__entry__2}

| Type | Description |
|-|-|
| GlideRecord | A GlideRecord on table wf_context on the inserted record for this newly created workflow context. |
[Table 39. Returns]

{#r_WF-startFlowRetroactive_S_N_GR_S_A__table_k3h_hx4_mt}  

    // is this a retroactive start?
          ////where current is a task record with a workflow context
          var msecs = new GlideDateTime().getNumericValue() - current.start_time.getGlideObject().getNumericValue();
          
          // treat this as a retroactive workflow start if the SLA started more than 5 seconds ago
          var w = new Workflow();
          if (msecs <= 5000)
            w.startFlow(id, current, current.operation());
          else
            w.startFlowRetroactive(id, msecs, current, current.operation());
          
          // update the record in case the workflow changed some values
          current.update();
          
        
## Workflow - Workflow() {#ariaid-title22}

Constructor for Workflow class.
{#r_WF_Workflow__table_t3b_vwl_nt__entry__3}

| Name | Type | Description |
|-|-|-|
| None |   |   |
[Table 40. Parameters]

{#r_WF_Workflow__table_t3b_vwl_nt} {#r_WF_Workflow__table_u3b_vwl_nt__entry__2}

| Type | Description |
|-|-|
| void |   |
[Table 41. Returns]

{#r_WF_Workflow__table_u3b_vwl_nt}  

    var w = new Workflow();


