Business rule to move Change tasks on hold if the change request is on hold

Wasd123
Tera Expert

I'm trying to create a business rule that would put all the related change tasks on hold when the change requests is on hold. (All related which ones are active, so if a task is already closed then it should only put on hold the ones which ones are still open.). What is the best way to achieve this?

 

I have tried with script like this:

(function executeRule(current, previous /*null when async*/ ) {
    var changeTask = new GlideRecord('change_task');
    var ChangeTaskFields = changeTask.change_request.sys_id;

    ChangeTaskFields.addCondition('sys_id', current.sys_id);
    changeTask.query();

    if (changeTask.change_request.on_hold.getDisplayValue() == 'true') {
        while (changeTask.next()) {
            changeTask.on_hold = 'true';
        }
    } else {
        while (changeTask.next()) {
            changeTask.on_hold = 'false';
        }
    }
})(current, previous);

but whoever I try I can't seem to get the desired results. Could you point me in the correct direction?

The business rule is referring to the Change Request [change_request] table, and the condition is that it should run whenever the on_hold reason changes on the change request and should set the same value for the change tasks.

1 ACCEPTED SOLUTION

Amit Gujarathi
Giga Sage
Giga Sage

HI @Wasd123 ,
I trust you are doing great.
Here's an updated version of your script:

(function executeRule(current, previous /*null when async*/) {
    // Check if the change request is on hold
    var isOnHold = current.on_hold.toString() === 'true';

    // Query to find all active change tasks related to the current change request
    var changeTask = new GlideRecord('change_task');
    changeTask.addQuery('change_request', current.sys_id);
    changeTask.addQuery('state', '!=', '3'); // Assuming '3' is the state code for 'Closed'
    changeTask.query();

    while (changeTask.next()) {
        changeTask.on_hold = isOnHold;
        changeTask.update();
    }
})(current, previous);

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



View solution in original post

4 REPLIES 4

aaron47
Mega Guru

 

Hello @Wasd123 

My approach would be as follows:

 

Step 1

Create a business rule to run on the change_request table (as you have done).

 

Step 2

Set the when to run so it only activates when on hold is changes to true

aaron47_1-1701131059625.png

 

 

Step 3

In the glide record on the change_task table, do an encoded query to filter by the parent change request.

 

It would be something like this:

parent.sys_id=b0dbda5347c12200e0ef563dbb9a718f^active=true

 

ie:

var changeTaskGR = new GlideRecord('change_task');

changeTaskGR.addEncodedQuery("parent.sys_id=" + current.parent.sys_id + "^active=true");

 

Step 4

In the while loop, set the change_task record so it is also on_hold = true.

while(changeTaskGR.next()) {

   changeTaskGR.setValue('on_hold', 'true');

   changeTaskGR.update();
}

 

 

 

 

 

Akshata T
Tera Guru
Hello @Wasd123 Create Business rule on Change request. apply condition as a attached ss. Use script as below. 
 
    var uniqueVal = current.getUniqueValue();
    var changeTask = new GlideRecord('change_task');
    changeTask.addQuery('change_request', uniqueVal);
    changeTask.addQuery('Active', true);
    changeTask.query();
    if (changeTask.next()) {
        changeTask.on_hold = true;
        changeTask.on_hold_reason = current.on_hold_reason;
        changeTask.update();
    }

Amit Gujarathi
Giga Sage
Giga Sage

HI @Wasd123 ,
I trust you are doing great.
Here's an updated version of your script:

(function executeRule(current, previous /*null when async*/) {
    // Check if the change request is on hold
    var isOnHold = current.on_hold.toString() === 'true';

    // Query to find all active change tasks related to the current change request
    var changeTask = new GlideRecord('change_task');
    changeTask.addQuery('change_request', current.sys_id);
    changeTask.addQuery('state', '!=', '3'); // Assuming '3' is the state code for 'Closed'
    changeTask.query();

    while (changeTask.next()) {
        changeTask.on_hold = isOnHold;
        changeTask.update();
    }
})(current, previous);

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



Ravi Gaurav
Giga Sage
Giga Sage

Your script looks on the right track, but there are a few modifications needed. Here's an updated version of your script:

 

(function executeRule(current, previous /*null when async*/) {
// Check if the on_hold field has changed
if (current.on_hold.changes()) {
// Query change tasks related to the current change request
var changeTask = new GlideRecord('change_task');
changeTask.addQuery('change_request', current.sys_id);
changeTask.query();

// Set on_hold field for each related change

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

ï”— YouTube: https://www.youtube.com/@learnservicenowwithravi
ï”— LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/