logo

NJP

Fixing a wrong update that attached two workflows in one task

Import · Aug 11, 2022 · article

Fixing a wrong update that attached two workflows in one task therefore it was needed to refresh the first workflow.

First things first - How to identify the workflows?

The workflows are storage in wf_context table.

https://<instance>.service-now.com/nav_to.do?uri=%2Fwf_context_list.do%3Fsysparm_query%3Dworkflow_versionISEMPTY%255EORDERBYDESCactive%26sysparm_first_row%3D1%26sysparm_view%3D

image

Coding

var crId = [];
// get the scope of the issue to obtain the sys_ids of the CRs
var gr = new GlideRecord('wf_context');
gr.addEncodedQuery('workflow_versionISEMPT^table=change_request');
gr.query();
if(gr.next()){
// Hold the sys_id of the CR we are about to modify 
crId.push(gr.id);
// we delete the ghost workflow
gr.deleteMultiple();
}
//gs.print(crId.toString().split(',').join('<br />'));
//gs.print('--------------------------------------------------------');

var tm = crId.lenght;
for (var i = 0; i < tm; i++){
var gr2 = new GlideRecord('wf_context');
//get the contextid of the valid context to execute the nudge (after the deletion of the issue)
gr2.addQuery('id', crId[i]);
gr2.query();
if (gr2.next()){
//get the sys_id of the wf context to execute the nudge
var contextId = gr2.sys_id
new Workflow().broadcastEvent(contextId, 'update');

gs.print(gr2.sys_id);
}
}

.

deleteMultiple?

All fields from the targeted table, including system fields, are used in query-by-example (QBE) fashion to locate records to be deleted. Query example fields can have special prefixes to constrain the search function.

What is nudge?

It is just a way to push the workflow.

image

image

image

runWorkflow_forContext();

function runWorkflow_forContext() {
   var contextId = current.sys_id;

   if (contextId != null) {
       new Workflow().broadcastEvent(contextId, 'update');
   }
}


image

Workflow stuck and nudge does not progress.

How to progress a stuck workflow

Overview: Workflows in Applications

Workflow activity 'Wait for Condition' always needs a 'Nudge'

Workflow intermittently hangs on Timer activity

Workflow Issue: Wait for Condition does not continue once condition is satisfied.

View original source

https://www.servicenow.com/community/developer-articles/fixing-a-wrong-update-that-attached-two-workflows-in-one-task/ta-p/2302448