Auto close Change Request after 2 hrs when it is moved to Review phase

Arjun Reddy Yer
Tera Guru

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.

 

1 ACCEPTED SOLUTION

Amit Gujarathi
Giga Sage
Giga Sage

HI @Arjun Reddy Yer ,
I trust you are doing great.

  1. Create a business rule or workflow in ServiceNow that triggers when a Change Request is moved to the Review Phase.

  2. 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



View solution in original post

10 REPLIES 10

Ankur Bawiskar
Tera Patron
Tera Patron

@Arjun Reddy Yer 

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?

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

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.

@Arjun Reddy Yer 

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?

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

it should use the below highlighted one

ArjunReddyYer_0-1685520805597.png