Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

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

Arjun Reddy Yer
Mega Sage

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

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

@Arjun Reddy Yer 

so where have you written the logic of auto-closure?

there only you can pick the CHG task with short description = Implement and set in CHG record

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

To auto close of Change Request tried with BR but it's not working 

 

Business Rule 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 = 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);

 

@Arjun Reddy Yer 

you should use scheduled job for this and not BR

add that logic in it

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

You are saying to use this Schedule Job

ArjunReddyYer_0-1685592732894.png

but entire script need to use or any modifications required in 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 = 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);

Amit Gujarathi
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