fill a date on a form based in other field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2025 02:09 AM
I have two fields: Start Date and Initial Date, both of type Date (not Date/Time).
When the user selects the Group "PANP", I need the Initial Date to be automatically set to 1 month and 7 days after the selected Start Date.
I’ve already tried using a Script Include and a Client Script (onChange), but either it doesn’t work, or it behaves incorrectly — for example, if the Start Date is 12/03/2025, it reads it as December 3rd instead of March 12th.
We use the day/month/year format.
Has anyone done something similar or knows how to make this work properly?
Thanks in advance,
Marisa
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2025 07:50 PM
hi @MARISA INACIO1 ,
I’ve faced this before when dealing with date fields (not date/time) and different system date formats (dd/MM/yyyy vs MM/dd/yyyy). The key is to always use GlideDate or GlideDateTime objects on the server side since client-side date parsing usually messes up the format.
You can try a simple onChange client script that calls a GlideAjax + Script Include:
client script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var group = g_form.getValue('group');
if (group == 'PANP') {
var ga = new GlideAjax('CalcInitialDate');
ga.addParam('sysparm_name', 'getInitialDate');
ga.addParam('sysparm_start', g_form.getValue('start_date'));
ga.getXMLAnswer(function(answer) {
if (answer) {
g_form.setValue('initial_date', answer);
}
});
}
}
script include:
var CalcInitialDate = Class.create();
CalcInitialDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getInitialDate: function() {
var start = this.getParameter('sysparm_start');
var gd = new GlideDate();
gd.setValue(start);
// convert to GlideDateTime for easy add
var gdt = new GlideDateTime(gd);
gdt.addMonthsUTC(1);
gdt.addDaysUTC(7);
return gdt.getLocalDate().getValue(); // returns only date part
}
});
You can make changes as per your requirement. and make sure that your script include is client callable.
