Abort RITM Closure if Pending Approvals are present

Arijit Saikia
Tera Contributor

Hi,

I have a requirement that any RITM state cannot be changed to Closed Complete manually if there are pending Approvals for the RITM.

I have created a Before Business Rule on the sc_req_item table and used the following script, however it is still allowing me to close a RITM. PLease advise what am I missing.

 

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


//Checks for any Active approval and aborts the update if active
var gr = new GlideRecord('sysapproval_approver');
gr.addQuery('document_id', current.sys_id);
gr.addQuery('state', 'requested');
gr.query();
if (gr.hasNext()) {
gs.addInfoMessage("There are open approvals which must be completed before this record can be closed");
current.setAbortAction(true);
}

})(current, previous);

2 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron
Tera Patron

@Arijit Saikia 

Did you add gs.info() to check if BR ran?

whether query is working fine?

try this

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


//Checks for any Active approval and aborts the update if active
var gr = new GlideRecord('sysapproval_approver');
gr.addQuery('document_id', current.sys_id).addOrCondition('sysapproval', current.sys_id);
gr.addQuery('state', 'requested');
gr.query();
if (gr.hasNext()) {
gs.addInfoMessage("There are open approvals which must be completed before this record can be closed");
current.setAbortAction(true);
}

})(current, previous);

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

Hi,

I'm glad you found an answer that worked for you.

I'd still recommend reviewing my reply as similar suggestions were stated.

I hope at a minimum, it was helpful. Keep in mind that if multiple solutions assisted you, you can accept more than one solution as well 🙂

Take care!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

6 REPLIES 6

Allen Andreas
Administrator
Administrator

Hi,

Can you double-check that your business rule is executing? Do you have the condition with something like: state changes to Closed Complete? And then the Update/Create checkboxes checked for the BR to run?

Otherwise, your business rule isn't even executing. You can also add log statements to your BR to check that it's running, etc.


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hi Allen,

Please check the screenshots. I have included the filter condition to check for State Changes to Closed Complete. It is a Before BR for Inserts and Updates.

Hi,

In your addEncodedQuery line, you need to allow some JavaScript there to grab the current object.

The script differs from what you shared in your original post as well...please try to let us know exactly what you are using. With that said, I'm glad my original reply is Helpful as it prompted you to provide screenshots to clarify what is actually going on...

For the adjustment to script see this example:

addEncodedQuery('query_paste_here...document_id='+current.sys_id);


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hi,

I'm glad you found an answer that worked for you.

I'd still recommend reviewing my reply as similar suggestions were stated.

I hope at a minimum, it was helpful. Keep in mind that if multiple solutions assisted you, you can accept more than one solution as well 🙂

Take care!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!