Autopopulation of field three months prior on form

Atchutaram
Tera Contributor

Hi Everyone,

 

I am working on population of date field in one variable based on selection of other date variable.

I have start date variable that is date format filed, and other filed is end date.

When i select a date in start date the end date field should be populated with date 3 months prior.

I tried this using client script butting getting an error. Here is my code

 

var stdate = g_form.getValue('start_date');
   if (stdate) {
    var duedt = new GlideDateTime(stdate);
    duedt.addMonthsUTC(3);
    g_form.setValue('end_date', duedt.getDate());

 

   }
 
Can i use GlideDateTimein client script?
 
BR

 

 

1 ACCEPTED SOLUTION

@Atchutaram 

updated now

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

    if (newValue === '')
        g_form.clearValue('end_date');

    if (newValue != '') {
        var startDt = new Date(getDateFromFormat(newValue, g_user_date_format));
        startDt.setDate(startDt.getDate() + 90);
        g_form.setValue('end_date', startDt.toISOString().slice(0, 10));
    }
}

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

9 REPLIES 9

Voona Rohila
Kilo Patron
Kilo Patron

Hi @Atchutaram 

 

Sample code: 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var selected_date = new Date(getDateFromFormat(newValue, g_user_date_time_format));
    selected_date.setDate(selected_date.getDate() + 90);//add 90 days to the selected date.
    var selected_dateStr = formatDate(selected_date, g_user_date_time_format);
    g_form.setValue('u_expected_end', selected_dateStr); // change field name as required.
}

 


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

Hi @Voona Rohila

 

Does this work on change of my start date,  if yes where are we getting that variable?

 

BR

Hi @Voona Rohila 

 

I have tried the code but it not working its giving me some random date not 3 months prior date.

Try this: 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var selected_date = new Date(getDateFromFormat(newValue, g_user_date_time_format));
   
    selected_date.setDate(selected_date.getDate() - 90); //add 90 days prior to the selected date.
	g_form.addInfoMessage(selected_date);
    var selected_dateStr = formatDate(selected_date, g_user_date_time_format);
    g_form.setValue('u_expected_end', selected_dateStr);
}

 

I just tried in my pdi and its setting 90 days prior to the expected start value in expected end field.

VoonaRohila_0-1749460008775.png

 


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP