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

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

Thank you for your response.

 

I tried changing 'state' of 'sc_req_item' table from 'Open' to 'Confirmation of Completion' while the catalog task was 'Open' and executing a business rule, but the update was not interrupted and 'state' field of the 'sc_req_item' did not return to 'Open'. I also received an Invalid Update message.

What do you think is the problem?

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. 

Thank you so much!

It worked perfectly.