Amit Gujarathi
Giga Sage
Giga Sage

Attach the workflow to the existing record

Symptoms

As a part of the data migration activity which I performed in my project . We have to migrate couple of thousands of change request from one instance to another. When we migrated the data and we tried to continue with the execution of the change to further state we found the change was not working as expected.

And the reason for that was we have migrated record but we didn't move workflow context for the same.

 

Diagnosis

We checked in the 'Active context' for those records and there was no workflow attach to those records . So the next step is to come up with a solution (in our case fix script) to attach the workflow to the existing records.

 

Solution

For this we will be using the Workflow API and in that start flow method.

Method signature : startFlow(String workflowId, GlideRecord current, String operation, Array vars)

 

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

 

/**
* @Description : Attach the workflow to the existing record
*  : Class for therecord
* @encodedQuery : Query for the records which we have to acted on
* @WorkflowSysID : Sys_id of the workflow which needs to be added
**/

var table = "<tableName>";
var encQuery = "<encodedQuery>";
var workflow_sys_id = "<WorkflowSysID>";

var grTask = new GlideRecord(table);
grTask.addEncodedQuery(encQuery);
grTask.query();

while (grTask.next()) {
  var wf = new Workflow();
  var context = wf.startFlow(workflow_sys_id, grTask, grTask.update());
}

 

Please be sure to bookmark this article as well as mark it as Helpful if you thought it was helpful.

 

Regards,

Amit Gujarathi

Technomonk Youtube 

Amit Gujarathi Linkedin 

TheTechnomonk.com 

ServiceNow Community Amit Gujarathi 

Version history
Last update:
‎10-18-2022 01:53 AM
Updated by:
Contributors