The CreatorCon Call for Content is officially open! Get started here.

Normal chnage need to abort during the black schedule time, please help in the requirement.

Arjun Kumar Le1
Tera Contributor



I have created a before insert business rule on change table, please correct this, black out schedule screen shot in below.

ArjunKumarLe1_2-1692191960810.png

 

ArjunKumarLe1_1-1692191824396.png

(function executeRule(current, previous /*null when async*/) {
    // Get the start and end dates of the new change request
gs.addInfoMessage('1');
    var changeStartDate = current.start_date.getGlideObject().getDateTimeValue();
gs.addInfoMessage('1');
    var changeEndDate = current.end_date.getGlideObject().getDateTimeValue();
gs.addInfoMessage('1');
 
    // Query for the specified blackout schedule
    var blackoutQuery = new GlideRecord('cmn_schedule_span');
gs.addInfoMessage('11');
    blackoutQuery.addQuery('schedule', 'a67892cd4720311073441288c26d43af');
gs.addInfoMessage('111');
    blackoutQuery.query();
gs.addInfoMessage('1111');
 
    if (blackoutQuery.next()) {
gs.addInfoMessage('11111');
        // Get the start and end dates of the blackout schedule
        var blackoutStartDate = blackoutQuery.start_date_time.getGlideObject().getDateTimeValue();
gs.addInfoMessage('111111');
        var blackoutEndDate = blackoutQuery.end_date_time.getGlideObject().getDateTimeValue();
gs.addInfoMessage('1111111');
 
        // Check if the change request's dates overlap with the blackout schedule
        if (changeStartDate < blackoutEndDate && changeEndDate > blackoutStartDate) {
gs.addInfoMessage('11111111');
            gs.addErrorMessage('Change request creation is not allowed during the specified blackout schedule.');
gs.addInfoMessage('8');
            current.setAbortAction(true);
gs.addInfoMessage('9');
        }
    }
})(current, previous);

 

 

4 REPLIES 4

Bert_c1
Kilo Patron

Hi Arjun,

 

Why not use OOB functionality for this?  Please see:

 

https://docs.servicenow.com/en-US/bundle/utah-it-service-management/page/product/change-management/t...

 

Activate the related plugins if not present.

ArjunKumarLe1_0-1692223538370.png

as mentioned in the screen shoot i have created back out schedule, but it not aborting the creation , for that i have written a script

Hi try the following changes to your script. I've removed some 'gs.addInfoMessage' and updated some with useful information

 

(function executeRule(current, previous /*null when async*/) {
    // Get the start and end dates of the new change request
    var changeStartDate = current.start_date.getGlideObject().getDateTimeValue();
    var changeEndDate = current.end_date.getGlideObject().getDateTimeValue();
	gs.addInfoMessage('Start: ' + changeStartDate + ', End: ' + changeEndDate);
 
    // Query for the specified blackout schedule
    var blackoutQuery = new GlideRecord('cmn_schedule_span');
    blackoutQuery.addQuery('schedule', 'a67892cd4720311073441288c26d43af');
    blackoutQuery.query();
	gs.addInfoMessage('Found: ' + blackoutQuery.getRowCount() + ' records');
 
    if (blackoutQuery.next()) {
        // Get the start and end dates of the blackout schedule
        var blackoutStartDate = blackoutQuery.start_date_time.getGlideObject().getDateTimeValue();
        var blackoutEndDate = blackoutQuery.end_date_time.getGlideObject().getDateTimeValue();
		gs.addInfoMessage('Blackout start: ' + blackoutStartDate + ', End: ' + blackoutEndDate);
 
        // Check if the change request's dates overlap with the blackout schedule
        if (changeStartDate < blackoutEndDate && changeEndDate > blackoutStartDate) {
            gs.addErrorMessage('Change request creation is not allowed during the specified blackout schedule.');
            current.setAbortAction(true);
        }
    }
})(current, previous);

that should help you debug your logic.

Testing your setting of the vairables you used show each are 'undefined'. Try the following:

 

(function executeRule(current, previous /*null when async*/) {
    // Get the start and end dates of the new change request
//    var changeStartDate = current.start_date.getGlideObject().getDateTimeValue();
//    var changeEndDate = current.end_date.getGlideObject().getDateTimeValue();
    var changeStartDate = current.start_date;
    var changeEndDate = current.end_date;
	gs.addInfoMessage('Start: ' + changeStartDate + ', End: ' + changeEndDate);
 
    // Query for the specified blackout schedule
    var blackoutQuery = new GlideRecord('cmn_schedule_span');
    blackoutQuery.addQuery('schedule', 'a67892cd4720311073441288c26d43af');
    blackoutQuery.query();
	gs.addInfoMessage('Found: ' + blackoutQuery.getRowCount() + ' records');
 
    if (blackoutQuery.next()) {
        // Get the start and end dates of the blackout schedule
//        var blackoutStartDate = blackoutQuery.start_date_time.getGlideObject().getDateTimeValue();
//        var blackoutEndDate = blackoutQuery.end_date_time.getGlideObject().getDateTimeValue();
        var blackoutStartDate = blackoutQuery.start_date_time;
        var blackoutEndDate = blackoutQuery.end_date_time;
		gs.addInfoMessage('Blackout start: ' + blackoutStartDate + ', End: ' + blackoutEndDate);
 
        // Check if the change request's dates overlap with the blackout schedule
        if (changeStartDate < blackoutEndDate && changeEndDate > blackoutStartDate) {
            gs.addErrorMessage('Change request creation is not allowed during the specified blackout schedule.');
            current.setAbortAction(true);
        }
    }
})(current, previous);

And even the corrections are not needed, you can just use 'current.start_date', 'current.end_date', 'blackoutQuery.start_date_time', and 'blackoutQuery.end_date_time' in the "if" statement.  You also need logic for either current.start_date or current.end_date falls withing the Blackout schedule.