How to add one year to a date field using client script(on change) + script include

prabhjot5
Tera Contributor

How to add one year to a date field using client script(on change) + script include
There are two fields
Start date and End date 

whenever I will select Start date I want to put one year to the End date by using on change client script

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Sample script below using GlideAjax

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 year = 1;
        var gdt = new GlideDateTime(date);
        gdt.addYearsUTC(year);
        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('start_date')); // give here date variable name
    ga.getXMLAnswer(function(answer){
        if(answer != ''){
            g_form.setValue('end_date', answer); // give proper variable name here
        }
    });
    //Type appropriate comment here, and begin script below

}

Regards
Ankur

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

View solution in original post

5 REPLIES 5

Hi @Ankur Bawiskar , could you please share , how can this be done just by using client script. I am also trying similar thing, but this solution mentioned by you here is not working.

 

TIA