Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Business Rule on "sc_req_item" Table Not Setting Stage

Maurice Murphy
Tera Guru

I have an ask to automate the cascading closure of related records when a RITM is manually closed.

I would usually rather do this through a Flow but given my org's specific circumstance the easiest way I've determined the broadly implement this is via a business rule.

 

In short, I'm trying to make it so when someone manually sets the state of a RITM to "Closed Incomplete" or "Closed Skipped", the following actions happen:

  1. The stage is set to "Request Cancelled" so the associated REQ is closed.
  2. FlowAPI is called to cancel the associated Flow to avoid issues with the following action.
  3. All associated tasks are set to Closed Incomplete.

 

2 and 3 are working perfectly fine, the Flow is cancelled and all tasks automatically close, however for whatever reason I can't get the Stage to set correctly, and that's meaning we have REQ's that are being left open even with the RITM being closed.

 

Here's my BR, any idea what might need to be fixed? I know the BR is actually executing because again, Flow is cancelled and tasks are closed. It's just the stage that isn't setting. I've looked at so many other community posts with similar issues, but I haven't been able to find any success with the solutions.

 

(function(current, previous) {
    if (current.sys_id == '')
        return;

	//Have also tried just "current.stage = 'Request Cancelled'".
	current.setValue('stage', 'Request Cancelled');

    var flow = new GlideRecord('sys_flow_context');
    flow.addQuery('source_record', current.sys_id);
    flow.query();
    while (flow.next()) {
        sn_fd.FlowAPI.cancel(flow.getUniqueValue(), 'Parent RITM cancelled.');
    }

    var tasks = new GlideRecord('sc_task');
    tasks.addQuery('parent', current.sys_id);
    tasks.orderBy('order');
    tasks.query();
    while (tasks.next()) {
        tasks.state = 4;
        tasks.update();
    }
})(current, previous);

 

6 REPLIES 6

Community Alums
Not applicable

Hi @Maurice Murphy ,

When I tried that GlideRecord on my PDI it works fine, can you please check that current.sys_id is really present in sc_task table in parent field. For example your current RITM is 001 you have to check like 001 RITM reference present in sc_task table in parent feild 

SarthakKashyap_1-1714763808079.png

 

SarthakKashyap_0-1714763730815.png

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards 

Sarthak

Community Alums
Not applicable

@Maurice Murphy ,

You can do one more thing like you can run that part of script GlideRecord sc_task table on backgrount script 

like I do you can refer below code 

var tasks = new GlideRecord('sc_task');
    tasks.addQuery('parent', 'd4b3ba4f936182100d5170918bba102a');
    tasks.orderBy('order');
    tasks.query();
    while (tasks.next()) {
        //tasks.state = 4;
		gs.print("here") // Check how many times here is printing
       // tasks.update();
    }

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards 

Sarthak

The fact that it fully works in a PDI and not on our instance leads me to believe there may be something underlying with our configuration that's causing an issue. I'm not having any problems with the task or Flow cancelling, and those both reference the same sys_id and it's closing/cancelling the correct records, so I don't think that's an issue.

 

I'll see if I can't determine if there's something else which might be interfering with the BR.

Just to be clear, it's JUST this section of the BR that's not working.

	//Have also tried just "current.stage = 'Request Cancelled'".
	current.setValue('stage', 'Request Cancelled');