How to enforce a 48-hour hold before closing a Change based on Planned End Date/Time?

Kishore47
Tera Contributor

Hi Team,

I’m working on a requirement to enforce a 48-hour closure hold on Change Requests and would appreciate guidance on the best-practice approach in ServiceNow.

Requirement details:

  • Users must not be able to move a Change to the “Closed” state until 48 hours have elapsed after the Planned End Date/Time.

  • The rule should apply to the following Change types:

    • Normal

    • Standard

    • Emergency

  • Until the 48-hour window is completed, the system should block the update and display a user-friendly error message, such as:

    “You cannot close this Change until 48 hours after the Planned End Date/Time.”

Thanks in Advance

1 ACCEPTED SOLUTION

Tejas Adhalrao
Tera Guru

Hi @Kishore47  ,

Use a Business Rule (before update) on the change_request table.It runs server-side, so it’s enforced regardless of UI (Classic, Service Portal, API).Can block the update before it happens.Can show a friendly error message.

Condition:state changes to (Closed) AND type IN Normal, Standard, Emergency

 

(function executeRule(current, previous /*null when async*/) {

    // Only for Normal, Standard, Emergency changes
    var changeTypes = ['Normal', 'Standard', 'Emergency'];
    if (changeTypes.indexOf(current.type.toString()) == -1)
        return;

    // Only when user is trying to move to Closed (state = 7)
    if (current.state != 7)
        return;

    // Calculate 48 hours after Planned End Date/Time
    var plannedEnd = current.planned_end_date; // Make sure this is the correct field
    if (!plannedEnd) {
        gs.addErrorMessage("Planned End Date/Time is missing. Cannot close the Change.");
        current.setAbortAction(true);
        return;
    }

    var now = new GlideDateTime();
    var plannedEndDateTime = new GlideDateTime(plannedEnd);
    plannedEndDateTime.addSeconds(48 * 60 * 60); // add 48 hours

    if (now < plannedEndDateTime) {
        gs.addErrorMessage("You cannot close this Change until 48 hours after the Planned End Date/Time.");
        current.setAbortAction(true); // blocks the update
    }

})(current, previous);

check your backend values  and repalce it

 

 If you found my solution helpful, please mark it as Helpful or Accepted Solution...!

thanks,

tejas

Email: adhalraotejas1018@gmail.com

LinkedIn: https://www.linkedin.com/in/tejas1018

 

 

View solution in original post

6 REPLIES 6

Dr Atul G- LNG
Tera Patron

Hi @Kishore47 

 

https://www.servicenow.com/community/itsm-forum/i-need-to-have-the-maximum-time-of-48-hours-between-...

 

See @Goran WitchDoc  answer.

 

https://www.servicenow.com/community/itsm-forum/end-date-should-automatically-to-be-set-up-48-hours-...

 

*************************************************************************************************************
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/dratulgrover [ Connect for 1-1 Session]

****************************************************************************************************************

Ankur Bawiskar
Tera Patron

@Kishore47 

do this

-> Create before update BR on change_request and stop record update

Condition: 

current.state.changes() && current.state==3 && ['normal','standard','emergency'].indexOf(current.type.toString()) > -1

Script:

(function executeRule(current, previous /*null when async*/ ) {
    if (!current.end_date.nil()) { // Planned end field
        var plannedEnd = new GlideDateTime(current.end_date);
        var now = new GlideDateTime();
        plannedEnd.addSeconds(48 * 3600); // Add 48 hours

        if (now.before(plannedEnd)) {
            gs.addErrorMessage('You cannot close this Change until 48 hours after the Planned End Date/Time.');
            current.state = previous.state.toString(); // Revert state
            current.setAbortAction(true); // Block update
        }
    }
})(current, previous);

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

@Kishore47 

Thank you for marking my response as helpful.

I believe I also shared a working solution.

As per new community feature you can mark multiple responses as correct

💡 If my response helped, please mark it as correct as well so that this helps future readers find the solution faster! 🙏

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

its_SumitNow
Kilo Sage

Hi @Kishore47 

This requirement can't be done with OOB configurations, 

On Change we are having Change model each having it's own workflow, so you only thing is you have to write script preferably business rules

Write Before update business rule on change request

(function executeRule(current, previous) {
    
    // Check if state is changing to Closed
    if (current.state.changesTo('3') || current.state.changesTo('-5')) { // Adjust state values based on your instance
        
        // Verify this is a relevant change type
        var changeType = current.type.toString();
        if (changeType == 'normal' || changeType == 'standard' || changeType == 'emergency') {
            
            // Check if Planned End Date exists
            if (!current.end_date.nil()) {
                
                var plannedEndDate = new GlideDateTime(current.end_date);
                var currentDateTime = new GlideDateTime();
                
                // Add 48 hours to planned end date
                plannedEndDate.addSeconds(48 * 60 * 60); // 48 hours in seconds
                
                // Compare with current date/time
                if (currentDateTime.before(plannedEndDate)) {
                    // Block the closure
                    gs.addErrorMessage('You cannot close this Change until 48 hours after the Planned End Date/Time.');
                    current.setAbortAction(true);
                }
            } else {
                // Optional: Handle cases where Planned End Date is not set
                gs.addErrorMessage('Planned End Date/Time must be set before closing this Change.');
                current.setAbortAction(true);
            }
        }
    }
    
})(current, previous);

Let me Know if any help needed

 

if my response helped,Please mark it helpful & Accept as Solution 🙂

 

Warm Regards

Sumit

Technical Consultant