Workflow - Global

Workflow scripts provide an interface with the workflow engine..

Use these methods to manipulate workflows.

Workflow - broadcastEvent(String contextId, String eventName)

Broadcasts the named event for the specified context ID.

Table 1. Parameters
Name Type Description
contextId String The context ID.
eventName String The name of the event.
Table 2. Returns
Type Description
void

//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)

Cancels all running workflows on this record by broadcasting the cancel event to activities in all running workflows on this record.

Table 3. Parameters
Name Type Description
record GlideRecord GlideRecord on any table. All workflows running on this record will be cancelled.
Table 4. Returns
Type Description
void

//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)

Cancels this running context by broadcasting a cancel event to activities in this workflow context.

Table 5. Parameters
Name Type Description
context GlideRecord GlideRecord of the running context to cancel.
Table 6. Returns
Type Description
void

// 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 gr = new GlideRecord('wf_context');
      
      if (gr.get(current.context)) 
          w.cancelContext(gr);
      }

Workflow - deleteWorkflow(GlideRecord current)

Deletes all the workflows on the record.

Table 7. Parameters
Name Type Description
current GlideRecord GlideRecord for which the caller wants to delete all workflows. This can be any record on any table.
Table 8. Returns
Type Description
void

//where current is a task record with a workflow context
      var wkfw = new Workflow();
      wkfw.deleteWorkflow(current);
    

Workflow - fireEvent(GlideRecord eventRecord, String eventName)

Fires the named event on the input record.

Used in Activities Approval Coordinator, Timer, Lock, and some others.

Table 9. Parameters
Name Type Description
eventRecord GlideRecord The event record.
eventName String The name of the event to send to the executing workflow.
Table 10. Returns
Type Description
void

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

Workflow - fireEventById(String eventRecordId, String eventName)

Fires the named event on the record specified by record ID.

Used in Activities Approval Coordinator, Timer, Lock, and some others.

Table 11. Parameters
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 12. Returns
Type Description
void

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

Workflow - getContexts(GlideRecord record)

Gets all workflow contexts for the input record.

The input record is any record on any table for which the caller wants the running workflow contexts.

Table 13. Parameters
Name Type Description
record GlideRecord GlideRecord for which the caller wants a list of all workflow contexts .
Table 14. Returns
Type Description
GlideRecord GlideRecord on table wf_context filtered for all workflow contexts for the input record (in any state, e.g. running, cancelled, finished).

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

Workflow - getEstimatedDeliveryTime(String workflowId)

Gets the estimated time for a workflow to complete.

Table 15. Parameters
Name Type Description
workflowId String Sys_id of the workflow (table wf_workflow) to get the estimated run time.
Table 16. Returns
Type Description
String Display value from a GlideDuration (e.g., 3 days), or blank if unknown.

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

2 Days

Workflow - getEstimatedDeliveryTimeFromWFVersion(GlideRecord wfVersion)

Get the estimated elapsed execution time for the workflow version.

Table 17. Parameters
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 18. Returns
Type Description
String Display value from a GlideDuration (e.g., 3 days), or blank if unknown.

//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)

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.

Table 19. Parameters
Name Type Description
workflowID String The sys_id of the workflow (table wf_workflow)
amount Number amount
result Boolean True, if true
Table 20. Returns
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.
var wkfw = new Workflow();
      wkfw.getReturnValue('context');
Output:
*** Script: b99a866a4a3623120074c033e005418f
      

Workflow - getRunningFlows(GlideRecord record)

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.

Table 21. Parameters
Name Type Description
record GlideRecord GlideRecord of the record for which the caller wants a list of all running workflows.
Table 22. Returns
Type Description
GlideRecord GlideRecord on table wf_context and filtered for all executing workflow contexts.

//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)

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.

Table 23. Parameters
Name Type Description
workflowID String The sys_id of the workflow (table wf_workflow)
Table 24. Returns
Type Description
none
var wkfw = new Workflow();
      wkfw.getVersion('b99a866a4a3623120074c033e005418f');

Workflow - getVersionFromName(String workflowName)

Gets the appropriate workflow version for the input workflow name.

See getVersion() for more information.

Table 25. Parameters
Name Type Description
workflowName String The name of the workflow (table wf_workflow)
Table 26. Returns
Type Description
void
var wkfw = new Workflow();
      wkfw.getWorkflowVersionFromName('Emergency Change');

Workflow - getWorkflowFromName(String workflowName)

Gets the appropriate workflow for the input workflow name.

Table 27. Parameters
Name Type Description
workflowName String The name of the workflow.
Table 28. Returns
Type Description
void
var wflw = new Workflow();
      gs.print(wflw.getWorkflowFromName('Emergency Change'));

Workflow - hasWorkflow(GlideRecord record)

Determines if a specified record has any workflow contexts associated to it.

This includes running and completed workflow contexts.

Table 29. Parameters
Name Type Description
record GlideRecord GlideRecord under scrutiny. This GlideRecord can be from any table.
Table 30. Returns
Type Description
Boolean True, if record has associated workflow; otherwise, returns False.

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

false

Workflow - restartWorkflow(GlideRecord current, Boolean maintainStateFlag)

Recalculates the approvals and tasks for a workflow by adding new approvals and tasks while not resetting current approvals and tasks.

Use this script to add an additional company to a change request, without reseting current approvals for companies already in the workflow.

Table 31. Parameters
Name Type Description
current GlideRecord GlideRecord of the record this workflow is executing. This can by any record on any table.
maintainStateFlag Boolean If True, maintains all approvals and tasks in their current state.
Table 32. Returns
Type Description
void

//mark all existing approvals for the change as 'cancelled' and restart the workflow to create new approvals
      //where current is a task record with a workflow context
      new WorkflowApprovalUtils().cancelAll(current, comment);
      new Workflow().restartWorkflow(current);
    

Workflow - runFlows(GlideRecord record, String operation)

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)".

Table 33. Parameters
Name Type Description
record GlideRecord The record to run workflows against.
operation String Database operation. One of insert, update, delete.
Table 34. Returns
Type Description
void
var wkfw = new Workflow();
      wkfw.runFlows('f2400ec10b0a3c1c00ca5bb5c6fae427','update');

Workflow - startFlow(String workflowId, GlideRecord current, String operation, Array vars)

Starts a specified workflow. See script include WorkflowScheduler and Business Rule "Start Workflow" on table sc_req_item for examples of use.

Table 35. Parameters
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
>
////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)

Helper method for business rule Auto start on context.

Table 36. Parameters
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 37. Returns
TypeDescription
void
//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 gr = new GlideRecord('wf_workflow_execution');
        gr.name = current.name;
        gr.insert();
      
        current.table = 'wf_workflow_execution';
        current.id = gr.sys_id;
      }
      
      var wf = new Workflow();
      wf.startFlowFromContextInsert(current, current.operation())

Workflow - startFlowRetroactive(String workflowId, Number retroactiveMSecs, GlideRecord current, String operation, Array, ???)

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.

Table 38. Parameters
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 39. Returns
Type Description
GlideRecord A GlideRecord on table wf_context on the inserted record for this newly created workflow context.
// 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()

Constructor for Workflow class.

Table 40. Parameters
Name Type Description
None
Table 41. Returns
Type Description
void

var w = new Workflow();