The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Business rules don't work the way we expect them to work.

Yugo Sakuma
Tera Contributor

I am currently creating a Business Rule to check if a Catalog Task is 'Closed Completed', 'Closed Incompleted' or 'Closed Skipped' if the 'State' field in 'sc_req_item' table is changed during a 'Confirmation of Completion'.

 

The purpose of this business rule is to prevent the status of request item table from being changed while the catalog task is not closed.

 

I have tried the following script, but the 'State' field of 'sc_req_item' table is not automatically updated by the business rule.

 

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

    /* Check if 'State' field in Catalog Task table is 
	   “Closed Completed”, “Closed Incompleted”, or “Closed Skipped”. */
    var taskGR = new GlideRecord('sc_task');
    taskGR.addQuery('request_item', current.sys_id);
    taskGR.addQuery('state', '!=', '3'); //state is not Closed Completed
    taskGR.addOrCondition('state', '!=', '4'); //state is not Closed Incompleted
    taskGR.addOrCondition('state', '!=', '7'); //state is not Closed Skipped
    taskGR.query();

    /* If any Catalog Task is not “Closed Completed”, “Closed Incompleted”, or “Closed Skipped” 
	   restore the 'state' field of 'sc_req_item' to 'Open' */
    if (taskGR.next()) {
        current.state = '-5'; //-5 is 'Open'
    }

})(current, previous);

 

I would like to know if anyone can tell me what the problem is.

2 ACCEPTED SOLUTIONS

James Chun
Kilo Patron

Hi @Yugo Sakuma,

 

Let's simplify the script, try the following:

 

var taskGr = new GlideAggregate('sc_task');
taskGr.addAggregate('COUNT');
taskGr.addQuery('request_item', current.getUniqueValue());
taskGr.addActiveQuery();
taskGr.query();
taskGr.next();
if (taskGr.getAggregate('COUNT') > 0) {
    current.setAbortAction(true);
}

 

Cheers

View solution in original post

That is the expected behaviour, the Invlid Update message is from the last line of script - 

current.setAbortAction(true);

The script prevents any changes to the record, so although the 'state' looks as if it has changed on the client-side, the underlying data has not been saved/updated.

If you refresh the page, you will see that the 'state' has not been updated.

 

You can also add a message (e.g. gs.addErrorMessage("Close all the child SCTASKs") ) within the script. 

View solution in original post

6 REPLIES 6

Hello James.

 Thank you for helping me the other day. If you don't mind, let me ask an additional question.

 

For the script you suggested, can you add a restriction that RITM's [State] cannot be changed to [Open] or [Work in Progress] when all SCTASK's [Status] are [Closed(Complete, Incomplete, Skipped)], and a rule that automatically updates RITM's [State] to [Work in Progress] if even one SCTASK's [State] is not [Closed(Complete, Incomplete, Skipped)]?

Maddysunil
Kilo Sage

@Yugo Sakuma 

It seems like the issue might lie in the logic of your GlideRecord query. Instead of checking if the state is not equal to '3', '4', or '7', you should check if the state is not any of these values.

 

 

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

    /* Check if 'State' field in Catalog Task table is 
	   “Closed Completed”, “Closed Incompleted”, or “Closed Skipped”. */
    var taskGR = new GlideRecord('sc_task');
    taskGR.addQuery('request_item', current.sys_id);
    taskGR.addQuery('state', 'IN', ['3', '4', '7']); //state is Closed Completed, Closed Incompleted, or Closed Skipped
    taskGR.query();

    /* If any Catalog Task is not “Closed Completed”, “Closed Incompleted”, or “Closed Skipped” 
	   restore the 'state' field of 'sc_req_item' to 'Open' */
    if (!taskGR.next()) {
        current.state = '-5'; //-5 is 'Open'
    }

})(current, previous);

 

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks