The Zurich release has arrived! Interested in new features and functionalities? Click here for more

In catalog form based on start date and time variable ,end date and time variable should be auto dis

Bhanu_thej
Tera Contributor

Hi @Ankur Bawiskar  @Maik Skoddow  @Mohith Devatte @SwarnadeepNandy @Amit Gujarathi  please help me on this scenario.

 

 

In catalog form based on start date time (variable) , end date time (variable) should be auto populated with 12 hours difference in respective to the selected start date time(variable)  

  for eg: selected start date/time is 2:26:47 here end date/time variable should auto populate with 12 hours added  like 14:26:47 end date/time should be greyed out also.

Bhanu_thej_0-1694599299491.png

 

1 ACCEPTED SOLUTION

SANDEEP28
Mega Sage

@Bhanu_thej  You need to create client callable script include and onChange client script to achieve this requirement.

 

Script Include -

 

SANDEEP28_0-1694600831437.png

 

var populateEndDate = Class.create();
populateEndDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	setEndDate:function(){
		var startDate = new GlideDateTime(this.getParameter('sysparm_startDate'));
		if(startDate){ 
			startDate.addSeconds(43200); //convert 12 hours into seconds and then add
			return startDate;
		}
	},

    type: 'populateEndDate'
});

 

Create onChange client script on change of Start date field

 

SANDEEP28_1-1694600930099.png

 

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

    if (oldValue != newValue) {
        var getEndDate = new GlideAjax('global.populateEndDate');
        getEndDate.addParam('sysparm_name', 'setEndDate');
        getEndDate.addParam('sysparm_startDate', g_form.getValue('start_date'));
        getEndDate.getXMLAnswer(function(answer) {
            if (answer != '') {
                g_form.setValue('end_date', answer);

            }
        });
    }


}

 

Replace you start and end date variable name if they are different than mentioned in above script.

 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

View solution in original post

4 REPLIES 4

SANDEEP28
Mega Sage

@Bhanu_thej  You need to create client callable script include and onChange client script to achieve this requirement.

 

Script Include -

 

SANDEEP28_0-1694600831437.png

 

var populateEndDate = Class.create();
populateEndDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	setEndDate:function(){
		var startDate = new GlideDateTime(this.getParameter('sysparm_startDate'));
		if(startDate){ 
			startDate.addSeconds(43200); //convert 12 hours into seconds and then add
			return startDate;
		}
	},

    type: 'populateEndDate'
});

 

Create onChange client script on change of Start date field

 

SANDEEP28_1-1694600930099.png

 

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

    if (oldValue != newValue) {
        var getEndDate = new GlideAjax('global.populateEndDate');
        getEndDate.addParam('sysparm_name', 'setEndDate');
        getEndDate.addParam('sysparm_startDate', g_form.getValue('start_date'));
        getEndDate.getXMLAnswer(function(answer) {
            if (answer != '') {
                g_form.setValue('end_date', answer);

            }
        });
    }


}

 

Replace you start and end date variable name if they are different than mentioned in above script.

 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

Ankur Bawiskar
Tera Patron
Tera Patron

@Bhanu_thej  

just 15-30mins ago I answered similar question and added 1 hour

you can use same script and use onChange on start date/time variable

Sample script

var timein = g_form.getValue('startVariable');
var dateMS = getDateFromFormat(timein, g_user_date_time_format);
dateMS += 12 * 60 * 60 * 1000; // 12 means 12 hour here
var newDT = new Date();
newDT.setTime(dateMS);
g_form.setValue('endVariable', formatDate(newDT, g_user_date_time_format));

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Karthiga S
Kilo Sage

Hi @Bhanu_thej  

 

Write onchange client Script with Start date.

 

Scripting Guide - https://servicenowguru.com/scripting/client-scripts-scripting/client-side-dates-in-servicenow/

 

Please mark it Correct and Hit Like if you find this helpful!

SwarnadeepNandy
Mega Sage

Hello @Bhanu_thej ,

Use Catalog Client script to achieve it.

// Client script for catalog item form
function onChange(control, oldValue, newValue, isLoading) {
  // Get the value of start date time as a GlideDateTime object
  var gdt = g_form.getValue('start_date_time');
  // Add 12 hours to it
  gdt.addSeconds(12 * 60 * 60);
  // Set the value of end date time to the modified GlideDateTime object
  g_form.setValue('end_date_time', gdt);
}

Hope this works.

 

Kind Regards,

Swarnadeep Nandy