{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":1131,"path":"api-reference/server-api-reference/c_Workflow_api.md","title":"Workflow - Global","bundle":null,"doc_type":null,"release":"australia","body":"\n# Workflow- Global\n\nThe Workflow script include provides methods that interface with the Workflow engine.\n\nUse these methods to manipulate workflows.\n\n**Parent Topic:**[Server API reference](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/api-server.md)\n\n## Workflow - broadcastEvent\\(String contextId, String eventName\\)\n\nSends the specified event \\(message\\) into the workflow context to pass along to the executing activities.\n\nTypical 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://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/build-workflows/r_WorkflowEventSpecificFunctions.md).\n\nFor a list of the available OOB events, refer to [Workflow events in the base system](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/build-workflows/r_WorkflowEventsInTheBaseSystem.md).\n\n|Name|Type|Description|\n|----|----|-----------|\n|contextId|String|The context ID.|\n|eventName|String|The name of the event.|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\n```\n//where current is a task record with a workflow context\n      var wf = new Workflow().getRunningFlows(current);\n      while(wf.next()) {     \n          new Workflow().broadcastEvent(wf.sys_id, 'resume');\n      }\n```\n\n## Workflow - cancel\\(GlideRecord record\\)\n\nCancels all running workflows on this record by broadcasting the **cancel** event to activities in all running workflows on this record.\n\n|Name|Type|Description|\n|----|----|-----------|\n|record|GlideRecord|GlideRecord on any table. All workflows running on this record will be cancelled.|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\n```\n//get workflow helper\n      var workflow = new Workflow();\n      //cancel all the workflows, where current is a task record with a workflow context\n      workflow.cancel(current);\n      gs.addInfoMessage(gs.getMessage(\"Workflows for {0} have been cancelled\", current.getDisplayValue()));\n```\n\n## Workflow - cancelContext\\(GlideRecord context\\)\n\nCancels this running context by broadcasting a **cancel** event to activities in this workflow context.\n\n|Name|Type|Description|\n|----|----|-----------|\n|context|GlideRecord|GlideRecord of the running context to cancel.|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\n```\n// If a workflow has started for this item, cancel it, where current is a task record with a workflow context\n      if ((current.stage == 'Request Cancelled') && current.context \n          && !current.context.nil()) {\n      var w = new Workflow();\n      var now_GR = new GlideRecord('wf_context');\n      \n      if (now_GR.get(current.context)) \n          w.cancelContext(now_GR);\n      }\n```\n\n## Workflow - deleteWorkflow\\(GlideRecord current\\)\n\nDeletes all the workflows on the record.\n\n|Name|Type|Description|\n|----|----|-----------|\n|current|GlideRecord|GlideRecord for which the caller wants to delete all workflows. This can be any record on any table.|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\n```\n//where current is a task record with a workflow context\n      var wkfw = new Workflow();\n      wkfw.deleteWorkflow(current);\n    \n```\n\n## Workflow - fireEvent\\(GlideRecord eventRecord, String eventName, Object eventParms\\)\n\nFires the named event of a running workflow on the input record.\n\nUsed in Activities **Approval Coordinator**, **Timer**, **Lock**, and some others.\n\n|Name|Type|Description|\n|----|----|-----------|\n|eventRecord|GlideRecord|Event record listed in the Workflow Executing Activites \\[wf\\_executing\\] table.|\n|eventName|String|The name of the event to send to the executing workflow.|\n|eventParms|Object|Optional. Parameters in JSON format used by the event.|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\n```\n// where current is a task record with a workflow context\n  var w = new Workflow(); \n  w.fireEvent(current, 'execute');\n```\n\n## Workflow - fireEventById\\(String eventRecordId, String eventName\\)\n\nFires the named event on the record specified by record ID.\n\nUsed in Activities **Approval Coordinator**, **Timer**, **Lock**, and some others.\n\n|Name|Type|Description|\n|----|----|-----------|\n|eventRecordId|String|The sys\\_id of the glide record.|\n|eventName|String|The name of the event to send to the executing workflow.|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\n```\nvar wkfw = new Workflow();\n      wkfw.fireEventById('f2400ec10b0a3c1c00ca5bb5c6fae427','Timer');\n```\n\n## Workflow - getContexts\\(GlideRecord record\\)\n\nReturns all workflow contexts for a specified record.\n\n|Name|Type|Description|\n|----|----|-----------|\n|record|[GlideRecord](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/c_GlideRecordAPI.md)|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.|\n\n|Type|Description|\n|----|-----------|\n|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\\).|\n\n```\n//where current is a task record with a workflow context\nvar wkfw = new Workflow();\nvar context = wkfw.getContexts(current);\nwhile (context.next())\n  gs.print(context.started);\n```\n\n## Workflow - getEstimatedDeliveryTime\\(String workflowId\\)\n\nGets the estimated time for a workflow to complete.\n\n|Name|Type|Description|\n|----|----|-----------|\n|workflowId|String|Sys\\_id of the workflow \\(table wf\\_workflow\\) to get the estimated run time.|\n\n|Type|Description|\n|----|-----------|\n|String|Display value from a GlideDuration \\(e.g., 3 days\\), or blank if unknown.|\n\n```\nvar wkfw = new Workflow();\n      gs.print(wkfw.getEstimatedDeliveryTime('b99a866a4a3623120074c033e005418f'));\n```\n\n2 Days\n\n## Workflow - getEstimatedDeliveryTimeFromWFVersion\\(GlideRecord wfVersion\\)\n\nGet the estimated elapsed execution time for the workflow version.\n\n|Name|Type|Description|\n|----|----|-----------|\n|wfVersion|GlideRecord|GlideRecord on table wf\\_workflow\\_version of a specific workflow version for which the caller wants the estimated during of executing.|\n\n|Type|Description|\n|----|-----------|\n|String|Display value from a GlideDuration \\(e.g., 3 days\\), or blank if unknown.|\n\n```\n//where current is a task record with a workflow context\n      var wkfw = new Workflow();\n      var context = wkfw.getContexts(current);\n      gs.print(wkfw.getEstimatedDeliveryTimeFromWFVersion(context.wf_version));\n```\n\n## Workflow - getReturnValue\\(String workflowID, Number amount, Boolean result\\)\n\nGets 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.\n\nThis 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.\n\n|Name|Type|Description|\n|----|----|-----------|\n|workflowID|String|The sys\\_id of the workflow \\(table wf\\_workflow\\)|\n|amount|Number|amount|\n|result|Boolean|**True**, if true|\n\n|Type|Description|\n|----|-----------|\n|???|The return value of the workflow as specified by the Return Value activity. Workflows without a Return Value activity return a null value.|\n\n```\nvar wkfw = new Workflow();\n      wkfw.getReturnValue('context');\n```\n\nOutput:\n\n```\n*** Script: b99a866a4a3623120074c033e005418f\n      \n```\n\n## Workflow - getRunningFlows\\(GlideRecord record\\)\n\nGets all the currently running workflow contexts for the input record.\n\nThe input record is any record on any table for which the caller wants the running workflow contexts.\n\n|Name|Type|Description|\n|----|----|-----------|\n|record|GlideRecord|GlideRecord of the record for which the caller wants a list of all running workflows.|\n\n|Type|Description|\n|----|-----------|\n|GlideRecord|GlideRecord on table wf\\_context and filtered for all executing workflow contexts.|\n\n```\n//where current is a task record with a workflow context\n      var wf = new Workflow().getRunningFlows(current);\n      while(wf.next()) {     \n          new Workflow().broadcastEvent(wf.sys_id, 'pause');\n      }\n```\n\n## Workflow - getVersion\\(String workflowID\\)\n\nGets 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.\n\nThis is either the workflow checked out by the current user or the published workflow with the most recent date.\n\n|Name|Type|Description|\n|----|----|-----------|\n|workflowID|String|The sys\\_id of the workflow \\(table wf\\_workflow\\)|\n\n|Type|Description|\n|----|-----------|\n|none| |\n\n```\nvar wkfw = new Workflow();\n      wkfw.getVersion('b99a866a4a3623120074c033e005418f');\n```\n\n## Workflow - getVersionFromName\\(String workflowName\\)\n\nReturns the appropriate workflow version for the input workflow name.\n\nSee getVersion\\(\\) for more information.\n\n|Name|Type|Description|\n|----|----|-----------|\n|workflowName|String|Name of the workflow \\(table wf\\_workflow\\)|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\n```\nvar wkfw = new Workflow();\n      wkfw.getVersionFromName('Emergency Change');\n```\n\n## Workflow - getWorkflowFromName\\(String workflowName\\)\n\nReturns the sys\\_id of the workflow associated with the specified workflow name.\n\n|Name|Type|Description|\n|----|----|-----------|\n|workflowName|String|Name of the workflow.|\n\n|Type|Description|\n|----|-----------|\n|String|The sys\\_id of the workflow associated with the passed in name.|\n\n```\nvar wflw = new Workflow();\n      gs.print(wflw.getWorkflowFromName('Emergency Change'));\n```\n\n## Workflow - hasWorkflow\\(GlideRecord record\\)\n\nDetermines if a specified record has any workflow contexts associated to it.\n\nThis includes running and completed workflow contexts.\n\n|Name|Type|Description|\n|----|----|-----------|\n|record|GlideRecord|GlideRecord under scrutiny. This GlideRecord can be from any table.|\n\n|Type|Description|\n|----|-----------|\n|Boolean|True, if record has associated workflow; otherwise, returns False.|\n\n```\nvar wkfw = new Workflow();\n      gs.print(wkfw.hasWorkflow('f2400ec10b0a3c1c00ca5bb5c6fae427'));\n```\n\nfalse\n\n## Workflow - restartWorkflow\\(GlideRecord current, Boolean maintainStateFlag\\)\n\nRecalculates the approvals and tasks for a workflow by adding new approvals and tasks, while not resetting current approvals and tasks.\n\nYou 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.\n\n<table id=\"table_up5_d1s_nt\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\ncurrent\n\n</td><td>\n\nGlideRecord\n\n</td><td>\n\nGlideRecord of the record this workflow is executing. This can by any record on any table.\n\n</td></tr><tr><td>\n\nmaintainStateFlag\n\n</td><td>\n\nBoolean\n\n</td><td>\n\nFlag that indicates whether to maintain all approvals and tasks in their current state.Valid values:\n\n-   true: Maintain all approvals and tasks in their current state.\n-   false: Update all approval and task states.\n\n</td></tr></tbody>\n</table>|Type|Description|\n|----|-----------|\n|void| |\n\nThis example shows the workflow being restarted with the approval file changing from Rejected to Requested.\n\n```\n(function(){\n  var comment = 'Workflow Restarted - the Approval Field changing from Rejected to Requested';\n  var gLock = new GlideRecordLock(current);\n    gLock.setSpinWait(50);\n  if (gLock.get()) {\n    new Workflow().restartWorkflow(current, false);\n    current.setDisplayValue('approval_history', comment);\n  }\n})\n```\n\n## Workflow - runFlows\\(GlideRecord record, String operation\\)\n\nRuns all workflows for a given record in a given table and its descendant tables.\n\nSample usage can be seen in the Script Includes \"SNC - Run parent workflows\", and \"SNC - Run parent workflows \\(Approval\\)\".\n\n<table id=\"table_u2y_bjp_mt\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nrecord\n\n</td><td>\n\nGlideRecord\n\n</td><td>\n\nGlideRecord to run workflows against.\n\n</td></tr><tr><td>\n\noperation\n\n</td><td>\n\nString\n\n</td><td>\n\nDatabase operation.Valid values:\n\n-   insert\n-   update\n-   delete\n\n</td></tr></tbody>\n</table>|Type|Description|\n|----|-----------|\n|void| |\n\n```\nvar now_GR = new GlideRecord('wf_test');\nnow_GR.addQuery('parent', current.parent);\nnow_GR.addQuery('sys_id','!=',current.sys_id);\nnow_GR.query();\nwhile(now_GR.next()) {\n    new Workflow().runFlows(now_GR, 'update');\n}\n```\n\n## Workflow - startFlow\\(String workflowId, GlideRecord current, String operation, Array vars\\)\n\nStarts a specified workflow.\n\nSee script include WorkflowScheduler and Business Rule \"Start Workflow\" on table sc\\_req\\_item for examples of use.\n\n|Name|Type|Description|\n|----|----|-----------|\n|workflowId|String|The sys\\_id of the workflow to start. This sys\\_id refers to table wf\\_workflow.|\n|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.|\n|operation|String|The operation to perform on **current**. Possible values: **insert**, **update**, **delete**.|\n|vars|Array|Collection of variables to add to the workflow|\n\n```\n////where current is a task record with a workflow context\n      var w = new Workflow();\n      var context = w.startFlow(id, current, current.operation(), getVars());\n```\n\n## Workflow - startFlowFromContextInsert\\(GlideRecord context, String operation\\)\n\nHelper method for business rule **Auto start on context**.\n\n|Name|Type|Description|\n|----|----|-----------|\n|context|GlideRecord|GlideRecord on table wf\\_context of a new record \\(the \"current\" record in the business rule\\).|\n|operation|String|Database operation being performed. One of **insert**, **update**, **delete**.|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\n```\n//where current is a task record with a workflow context\n      current.name = current.workflow_version.name;\n      current.started_by.setValue(gs.userID());\n      \n      if (gs.nil(current.id)) {\n        var now_GR = new GlideRecord('wf_workflow_execution');\n        now_GR.name = current.name;\n        now_GR.insert();\n      \n        current.table = 'wf_workflow_execution';\n        current.id = now_GR.sys_id;\n      }\n      \n      var wf = new Workflow();\n      wf.startFlowFromContextInsert(current, current.operation())\n```\n\n## Workflow - startFlowRetroactive\\(String workflowId, Number retroactiveMSecs, GlideRecord current, String operation, Array, ???\\)\n\nUsed 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.\n\n|Name|Type|Description|\n|----|----|-----------|\n|workflowID|String|The sys\\_id of the workflow to start. This sys\\_id refers to table wf\\_workflow.|\n|retroactiveMSecs|Number|Delay in milliseconds used by Activity **Timer**.|\n|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|\n|operation|String|Database operation being performed.One of **insert**, **update**, **delete**.|\n|vars|Array|Collection of variables to add to the workflow.|\n|withSchedule|???|Schedule used by Activity **Timer**|\n\n|Type|Description|\n|----|-----------|\n|GlideRecord|A GlideRecord on table wf\\_context on the inserted record for this newly created workflow context.|\n\n```\n// is this a retroactive start?\n      ////where current is a task record with a workflow context\n      var msecs = new GlideDateTime().getNumericValue() - current.start_time.getGlideObject().getNumericValue();\n      \n      // treat this as a retroactive workflow start if the SLA started more than 5 seconds ago\n      var w = new Workflow();\n      if (msecs <= 5000)\n        w.startFlow(id, current, current.operation());\n      else\n        w.startFlowRetroactive(id, msecs, current, current.operation());\n      \n      // update the record in case the workflow changed some values\n      current.update();\n      \n    \n```\n\n## Workflow - Workflow\\(\\)\n\nConstructor for Workflow class.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|void| |\n\n```\nvar w = new Workflow();\n```\n\n"}}