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 

If you are doing the setValue of any field you have to give the backend value here like 

current.setValue('stage', 'backend_value');

You can get the backend value from configure dictionary. 

 

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

 

Thanks and Regards

Sarthak 

Community Alums
Not applicable

Be sure you are doing this thing in before business rule not after business rule.

 

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

 

Thanks and Regards

Sarthak