- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-27-2023 03:26 PM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-28-2023 01:52 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-27-2023 04:33 PM
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
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();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-28-2023 01:02 AM - edited ‎11-28-2023 01:10 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-28-2023 01:52 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-28-2023 01:53 AM
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/