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

tried via BR but it's not working 

with the below Before BR able to capture the Close Code & Close Notes

 

ArjunReddyYer_0-1685517607505.png

 

Tried with below Schedule Job but not working

ArjunReddyYer_0-1685612025812.png