Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to cancel all change Tasks linked to a Change record when the Approval is Cancelled.

denisdoucet
Tera Guru

Hello,

I need to create a business rule (enlighten me if there's a better approach) in order to change the state of all tasks associated to a Change record to a Cancelled State and inactive when the related change record's Approval is Cancelled.

At the moment, I have a business rule set on the Change Request table to run when approval changes to Cancelled.
When: After; Insert and Update selected.

The script I have on the Advanced tab is as follows:

cancelTasks();

function cancelTasks() {

if (current.sys_id == '')

return;

var tasks = new GlideRecord('change_task');

tasks.addQuery('parent', current.sys_id);
tasks.query();

while (tasks.next()) {

if (tasks.active == true) {
tasks.state = -4;
tasks.active = false;
tasks.update();

}
}
}

Not certain what I'm missing, any help would help.

find_real_file.png

find_real_file.png

Regards,
Denis

1 ACCEPTED SOLUTION

denisdoucet
Tera Guru

I was able to fix this.
I was running my Business Rule on the Change Request table, I had to change my rule in order for it to run on the sysapproval_approver table.

That change in addition to the script below allowed me to perform was I was looking for.

find_real_file.png

View solution in original post

2 REPLIES 2

dvp
Mega Sage

Please update the below line to

tasks.addQuery('change_request', current.sys_id);

 

Also make sure that approval field on change request is setting to cancelled

denisdoucet
Tera Guru

I was able to fix this.
I was running my Business Rule on the Change Request table, I had to change my rule in order for it to run on the sysapproval_approver table.

That change in addition to the script below allowed me to perform was I was looking for.

find_real_file.png