Business Rule triggered two times, updating the work twice

Saurav Bhardwa2
Tera Contributor

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

var comment = new GlideRecord('sc_req_item');
comment.addEncodedQuery('request=' + current.request + '^cat_item=7d3212551b68a1d0923e6311f54bcbda');
comment.query();
if (comment.next()) {

comment.work_notes = current.number + ' has been marked as Closed Complete';
comment.update();
gs.info('doctorDoom ' + current.number + ' has been marked as Closed Complete');
}

})(current, previous);

This is the code I am using, but it is updating the work notes twice in the RITM table. May I know why this is happening, is this something I need to change in my code? I have checked logs as well it is populating the notes twice

7 REPLIES 7

Swapna Abburi
Mega Sage
Mega Sage

@Saurav Bhardwa2 

On which table this business rule is getting executed? and Is it an onbefore/onafter Business rule?

It is getting triggered on sc_req_item

It is before update. There's another item in the same request, so the trigger condition is Item is that another Item and State is closed complete

Ahmmed Ali
Mega Sage

try below script:

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

var comment = new GlideRecord('sc_req_item');
comment.addEncodedQuery('request=' + current.request + '^cat_item=7d3212551b68a1d0923e6311f54bcbda');

comment.addEncodedQuery("sys_id!=" + current.sys_id);
comment.query();
if (comment.next()) {

comment.work_notes = current.number + ' has been marked as Closed Complete';
comment.update();
gs.info('doctorDoom ' + current.number + ' has been marked as Closed Complete');
}

})(current, previous);

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali