How to make business rule run after flow state is completed

Community Alums
Not applicable

Hello Experts,

 

I have written a business rule to check whether all the tasks connected to RITM are closed complete or closed skipped.

The business rules is working fine when there is one task or multiple tasks getting triggered simultaneously, but the issue is when tasks triggered depending upon the closure of previous task, business rule does not wait. So, is there any method that can be used to make business rule wait until flow stage is completed and then run?

 

Business rule:

Tejas12_0-1724955988922.png

 

(function executeRule(current, previous /*null when async*/ ) {

    var grSCTask = new GlideRecord('sc_task');
    grSCTask.addQuery('request_item', current.getValue('request_item'));
    grSCTask.addActiveQuery(); // added to check for active tasks
    grSCTask.query();
    if (!grSCTask.hasNext()) // if there still are active SCtasks connected to this RITM, there is no need to set any state on the parent RITM yet
    {
        var closeCompleteCount = 0;
        var closeSkippedCount = 0;
        var closedOtherCount = 0;

        grSCTask = new GlideRecord('sc_task');
        grSCTask.addQuery('request_item', current.getValue('request_item'));
        grSCTask.query();
        var totalCount = grSCTask.getRowCount();
        while (grSCTask.next()) {
            if (grSCTask.getValue('state') == 3) {
                closeCompleteCount++;
            } else if (grSCTask.getValue('state') == 7) {
                closeSkippedCount++;
            } else {
                closedOtherCount++;
            }
        }

        var ritm = new GlideRecord('sc_req_item');
        if (ritm.get(current.getValue('request_item'))) {

            if (closeSkippedCount == totalCount) {
                ritm.setValue('state', 7); // ritm is closed skipped
                ritm.update();
            }
            else if (closeCompleteCount == totalCount) {
                ritm.setValue('state', 3); // ritm is closed complete
                ritm.update();
            }
            else if(closeCompleteCount+closeSkippedCount == totalCount) {
                ritm.setValue('state', 3); // ritm is closed complete
                ritm.update();
            }
        }
    }

})(current, previous);

 

5 REPLIES 5

Deepti21
Tera Contributor

Did you find a solution to this problem?