Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

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

Hi @Voona Rohila 

i tried your code its giving me this date.

 

BR

Ankur Bawiskar
Tera Patron
Tera Patron

@Atchutaram 

you are using Server side code in client side and hence it's not working

so end date should be exact 3 months from start date?

this should work, I assume both are date type variables

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(selected_date.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

Hi @Ankur Bawiskar ,

 

Yes, both are date fields . I tried your code , but its not giving me any date.

 

BR

Hi @Ankur Bawiskar 

I have again tried your code now i am getting this error

 

onChange script error: ReferenceError: selected_date is not defined function () { [native code

@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