- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-30-2023 08:28 PM
Can any one help me @Vasantharajan N @Ankur Bawiskar
I'm trying to auto close Change Request after 2hrs when it is moved to Review Phase.
But the Close Code & Close Notes of Change Request should get auto fill based on Change Tasks Close Code & Close Notes.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-30-2023 09:31 PM
HI @Arjun Reddy Yer ,
I trust you are doing great.
Create a business rule or workflow in ServiceNow that triggers when a Change Request is moved to the Review Phase.
In the business rule or workflow, write a script to check if the Change Request has been in the Review Phase for 2 hours. You can use the current object to access the Change Request fields and the gs object to work with dates and times. Here's an example of the script:
(function executeRule(current, previous) {
var reviewPhase = 'Review'; // Set the Review Phase value here
var changeRequest = new GlideRecord('change_request');
// Check if the current Change Request is in the Review Phase
if (current.phase == reviewPhase) {
// Get the time when the Change Request entered the Review Phase
var reviewPhaseStart = current.sys_created_on;
// Calculate the time difference in milliseconds
var now = new GlideDateTime();
var elapsedTime = now.getNumericValue() - reviewPhaseStart.getNumericValue();
var twoHoursInMillis = 2 * 60 * 60 * 1000;
// Check if 2 hours have passed since the Change Request entered the Review Phase
if (elapsedTime >= twoHoursInMillis) {
// Set the Close Code and Close Notes of the Change Request based on Change Tasks
var changeTask = new GlideRecord('change_task');
changeTask.addQuery('change_request', current.sys_id);
changeTask.query();
while (changeTask.next()) {
if (changeTask.close_code && changeTask.close_notes) {
current.close_code = changeTask.close_code;
current.close_notes = changeTask.close_notes;
break;
}
}
// Close the Change Request
current.state = 3; // Set the state value for closed state here
current.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
05-30-2023 08:59 PM
so in that script itself get the change tasks for that CHG and update the fields and then close the CHG
where are you stuck?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-30-2023 09:06 PM
Change Tasks are getting created when the Change Request is in Implement Phase after the Change Tasks get closed then it will move from Implement phase to Review Phase.
While closing the Change Request Close Code & Close Notes are mandate those fields should get auto fill with the Change Tasks Close Code & Close Notes.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-30-2023 09:29 PM
1 CHG can have multiple Change tasks
so while closing CHG which change task record you want to use to fill the 2 fields on CHG?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2023 01:13 AM
it should use the below highlighted one