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.

how to prefill date in service catalog

TMKAM
Tera Contributor

Hi, 

i have create 3 variable and what i would like to achieve as per below scenario.

I would like to achieve that when i enter variable 1 start date and select the variable 2 - 3 day or 5 day it will automatically calculate and insert to variable 3 end date

variable 1 - start date

variable 2 - select box with multiple selection

variable 3 end date

 

scenario 1

variable 1 - i select date is 2025-03-05

variable 2 - i select 5 day

variable 3 - should automatically insert 2025-03-10

 

scenario 2

variable 1 - i select date is 2025-04-05

variable 2 - i select 10 day

variable 3 - should automatically insert 2025-04-15

 

im not sure what scripts and how to create a script to achieve above scenario.

 

thank you

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@TMKAM 

Sample Scripts below

Script Include: It should be client callable

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

calculateDate: function(){

var date = this.getParameter('sysparm_date');
var days = this.getParameter('sysparm_days');
var gdt = new GlideDateTime(date);
gdt.addDaysUTC(parseInt(days));
return gdt.getDate();
},

type: 'checkRecords'
});

onChange client script:

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

var ga = new GlideAjax('checkRecords');
ga.addParam('sysparm_name', "calculateDate");
ga.addParam('sysparm_date', g_form.getValue('variable1')); // give here date variable name
ga.addParam('sysparm_days', g_form.getValue('variable2')); // give here days variable name
ga.getXMLAnswer(function(answer){
if(answer != ''){
g_form.setValue('variable3', answer); // give proper variable name here
}
});
//Type appropriate comment here, and begin script below

}

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

View solution in original post

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@TMKAM 

ensure the choice value for that select box has value like 1 or 2 or 5

check this

Adding Days to a date via Script 

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

Ankur Bawiskar
Tera Patron
Tera Patron

@TMKAM 

Sample Scripts below

Script Include: It should be client callable

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

calculateDate: function(){

var date = this.getParameter('sysparm_date');
var days = this.getParameter('sysparm_days');
var gdt = new GlideDateTime(date);
gdt.addDaysUTC(parseInt(days));
return gdt.getDate();
},

type: 'checkRecords'
});

onChange client script:

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

var ga = new GlideAjax('checkRecords');
ga.addParam('sysparm_name', "calculateDate");
ga.addParam('sysparm_date', g_form.getValue('variable1')); // give here date variable name
ga.addParam('sysparm_days', g_form.getValue('variable2')); // give here days variable name
ga.getXMLAnswer(function(answer){
if(answer != ''){
g_form.setValue('variable3', answer); // give proper variable name here
}
});
//Type appropriate comment here, and begin script below

}

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