Workflow - Global

  • Release version: Yokohama
  • Updated January 30, 2025
  • 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)

    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.

    For a list of the available OOB events, refer to Workflow events in the base system.

    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 now_GR = new GlideRecord('wf_context');
          
          if (now_GR.get(current.context)) 
              w.cancelContext(now_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, Object eventParms)

    Fires the named event of a running workflow on the input record.

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

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

    Returns all workflow contexts for a specified record.

    Table 13. Parameters
    Name Type Description
    record GlideRecord 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 14. Returns
    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).
    //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)

    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)

    Returns the appropriate workflow version for the input workflow name.

    See getVersion() for more information.

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

    Workflow - getWorkflowFromName(String workflowName)

    Returns the sys_id of the workflow associated with the specified workflow name.

    Table 27. Parameters
    Name Type Description
    workflowName String Name of the workflow.
    Table 28. Returns
    Type Description
    String The sys_id of the workflow associated with the passed in name.
    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.

    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.

    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 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.
    Table 32. Returns
    Type Description
    void

    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)

    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 GlideRecord to run workflows against.
    operation String Database operation.
    Valid values:
    • insert
    • update
    • delete
    Table 34. Returns
    Type Description
    void
    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)

    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
    Type Description
    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 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, ???)

    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();