Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

Cancel Workflow

brianlan25
Kilo Patron

I'm trying to cancel a workflow associated with a story. I'm following this community post which has a correct answer.

https://www.servicenow.com/community/itsm-forum/how-can-i-cancel-a-running-workflow-on-a-single-ritm...

However I'm getting an error when using script background and enabling execute in sandbox. The error message is KittyScript validation failed for null.null.script with error: Syntax error: Expected end of expression but found 'gr' at position 4.

Any idea on why this code is not working?

var gr = new GlideRecord("rm_story");
gr.addQuery("number", "STRY517"); //full story number is in the actual code I'm trying to use.
gr.query();
if (gr.next()) {
	// cancel old running workflow
	var flows = new Workflow().getRunningFlows(gr);
	while(flows.next()) {
		new Workflow().cancelContext(flows);
	}  
}

@Ankur Bawiskar 

9 REPLIES 9

Deepak Shaerma
Mega Sage

hi @brianlan25 
Run this diagnose in background script:

var gr = new GlideRecord("rm_story");
gr.addQuery("number", "STRY6517"); 
gr.query();

if (gr.next()) {
    gs.log("Found Story sys_id: " + gr.sys_id);
    var ctx = new GlideRecord('wf_context');
    ctx.addQuery('id', gr.sys_id); 
    ctx.query();
    
    gs.log("Total legacy workflow contexts found: " + ctx.getRowCount());
    
    while(ctx.next()) {
        gs.log("Found Workflow: " + ctx.workflow_version.name + " | State: " + ctx.state);
        
        // If you want to force cancel it regardless of state, you can uncomment the line below:
        // new Workflow().cancelContext(ctx);
    }
}

It gives me this and when I uncomment the last line of code it doesn't do anything.

brianlan25_0-1787931674596.png

Context you may have missed if you have not look at the other comments. We have it setup so that if there are no CIs added or the CIs don't have an approver we set the story to ready and end the workflow. So when I move it back to draft then try to add the CIs it still will not allow you to request approval. It seems I need to remove the workflow not to cancel it so I can attach a new one.

Vishal Jaswal
Tera Sage

Hello @brianlan25 

Try below - if it works, then uncomment the last line

var gr = new GlideRecord("rm_story");
gr.addQuery("number", "STRY6517");
gr.query();
if (gr.next()) {
    var storyId = gr.getUniqueValue();
    gs.info("Story sys_id: " + storyId);
    
    // Find running activities where the parent context's id field = story sys_id
    var grExecuting = new GlideRecord('wf_executing');
    grExecuting.addQuery('context.id', storyId);
grExecuting.addQuery('state', 'IN', 'executing,waiting');
    grExecuting.query();
    gs.info("Running Workflow Executing activities found: " + grExecuting.getRowCount());
    
    // Collect unique parent contexts (reference records are in wf_context)
    var contextMap = {};
    while (grExecuting.next()) {
        var contextId = grExecuting.getValue('context');
        contextMap[contextId] = true;
        gs.info("Activity: " + grExecuting.getDisplayValue('activity') + " | state=" + grExecuting.getValue('state') + " | context=" + contextId);
    }
    
    // Cancel each unique parent context
    var workflow = new Workflow();
    for (var contextId in contextMap) {
        var grContext = new GlideRecord('wf_context');
        if (grContext.get(contextId)) {
            gs.info("Cancelling context: " + grContext.getValue('name') + " | state=" + grContext.getValue('state'));
            //workflow.cancelContext(grContext); //Uncomment me if code works
        }
    }
}

 


Hope that helps!

I think I found the root cause.

We capture Service, Service Offering, and CI on Agile stories. Because not all teams have their CIs fully defined yet, these fields are not mandatory while the story is in Draft.

Our workflow is configured to generate approvals based on the selected Service Offering and Configuration Item (restricted to Technical CIs such as Service Instances). This ensures each story receives both a business approval and a technical approval.

The issue occurs when users forget to populate the required CI fields. In that case, the workflow finds no approvers, automatically moves the story to Ready, and then ends. If I manually change the state back to Draft, the system will not start a new workflow because the previous workflow context has already completed.

So I need to remove the context of an already completed workflow. Is there a way to do that?

CharlesW8781780
Giga Contributor

This error looks more like a syntax/validation issue caused by the Script Background sandbox than a problem with the workflow logic itself. The sandbox can restrict certain server-side APIs, so I’d first test the script without Execute in Sandbox or validate the specific line around the reported position 4.