I want to set default time value difference to 8 hours when we click on all day check box

Chandu14
Tera Contributor

I want to set default time value diff to 8 hrs when we click on all day check box in Resource Calendar.

Navigation path : My Calendar under Self Service.

Please find the attachement.

4 REPLIES 4

SoniaShridhar13
Giga Guru

Hi @Chandu14  function onLoad() { var ajax = new GlideAjax('MyDateTimeAjax'); ajax.addParam('sysparm_name', 'nowDateTime'); ajax.getXML(function () { var test = ajax.getXMLAnswer(function(answer){ var response = JSON.parse(answer); //alert(response.datetime); g_form.setValue('date_time_deployment', response.datetime); }); }); } var MyDateTimeAjax = Class.create(); MyDateTimeAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, { nowDateTime: function () { var Date = new GlideDate(); var datetime = (Date + ' 08:00:00'); var response = {}; response.datetime = datetime; return JSON.stringify(response); }, type: 'MyDateTimeAjax' });

 

Please use the above script, mark it helpful if it helps...

 

 

Thnaks,

Sonia

Harish Bainsla
Kilo Patron
Kilo Patron

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}

if (control.id == 'all_day') {
var timePicker = g_form.getControl('time_picker');
if (newValue == 'true') {
var currentDate = new Date();
currentDate.setHours(currentDate.getHours() + 8);
var formattedTime = formatTime(currentDate);
timePicker.value = formattedTime;
}
}
}


function formatTime(date) {
var hours = date.getHours().toString().padStart(2, '0');
var minutes = date.getMinutes().toString().padStart(2, '0');
return hours + ':' + minutes;
}

SwarnadeepNandy
Mega Sage

Hello @Chandu14,

 

To set the default time value difference to 8 hours when you click on the all-day checkbox in the Resource Calendar in ServiceNow, you can use a client script on the Resource Allocation form. 

 

One possible client script that could achieve your requirement is:

function onChange(control, oldValue, newValue, isLoading) {
    //Check if the field that changed is the all day checkbox 
    if (control == ‘all_day’) { //Get the values of the start date and end date fields 
        var startDate = g_form.getValue(‘start_date’);
        var endDate = g_form.getValue(‘end_date’); //If the all day checkbox is checked 
        if (newValue == ‘true’) { //Create a new GlideDateTime object for the start date 
            var startGDT = new GlideDateTime(); //Set the value of the start 
            date startGDT.setValue(startDate); //Create a new GlideDateTime object for the end date 
            var endGDT = new GlideDateTime(); //Set the value of the end date 
            endGDT.setValue(endDate); //Calculate the time difference between the start date and end date in milliseconds 
            var timeDiff = endGDT.getNumericValue() - startGDT.getNumericValue(); //If the time difference is less than 8 hours (28800000 milliseconds) 
            if (timeDiff < 28800000) { //Add 8 hours to the end date endGDT.add(28800000); //Set the value of the end date field to the new end date 
                g_form.setValue(‘end_date’, endGDT.getValue());
            }
        }
    }
}

This script will run when any field on the Resource Allocation form changes, and check if the field that changed is the all-day checkbox. If it is, it will get the values of the start date and end date fields and calculate the time difference between them. If the time difference is less than 8 hours, it will add 8 hours to the end date and set the value of the end date field to the new value. You can modify this script according to your needs and preferences.

 

I hope this helps.

 

Kind Regards,

Swarnadeep Nandy

Chandu14
Tera Contributor

I have tried all three methods but one of them are worked, its still showing 24hrs time duration