Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

if Planned start date is less than 24ours or outside of 8 pm and 6am make Reason for emer Mandatory

Kishor O
Tera Sage

In one of the catalog items I have a Planned start date and Reason for emergency change field .

 

Reason for emergency change should be visible and Mandatory only if Planned start date is less  than 24ours from now or outside of 8 pm and 6 am.

KishorO_0-1721201061415.png

 

 

How can I achieve this any help will be appreiated.

 

Thanks

1 REPLY 1

Sid_Takali
Kilo Patron

Hi @Kishor O Try below onChange Catalog Client Script and modify code accordingly

function onChange(control, oldValue, newValue, isLoading) {

    var plannedStartDate = g_form.getValue('planned_start_date'); 
    if (plannedStartDate) {
        var plannedStartDateObj = new GlideDateTime(plannedStartDate);
        var currentDateObj = new GlideDateTime();
        
        var differenceInHours = gs.dateDiff(plannedStartDateObj, currentDateObj, true); 
        
        if (differenceInHours < 24 || isOutsideNightHours(plannedStartDateObj)) {
            g_form.setMandatory('reason_for_emergency_change', true); 
            g_form.setVisible('reason_for_emergency_change', true); 
        } else {
            g_form.setMandatory('reason_for_emergency_change', false); 
            g_form.setVisible('reason_for_emergency_change', false); 
        }
    }
}

function isOutsideNightHours(dateObj) {
    var hour = dateObj.getHourOfDay();
    return (hour < 6 || hour >= 20); 
}