Populate date field in onChange client script

Elena Pomana
Tera Guru

Hello,

On the contract form have 3 fields. Contract end date should be calculated based on Contract start date + duration months. I created a client script and a script include but somehow I always get "null".

I work in scope, but both client script and script include are created in scope.

e.g.

Contract start date: 2022-01-01

Duration months: 12

Contract end date: should be 2022-12-31

My client script:

find_real_file.png

My script include:

find_real_file.png

My log from script include returns the correct answer:

find_real_file.png

My info message from client script returns null:

find_real_file.png

What am I missing?

 

Thank you,

Elena

 

 

 

1 ACCEPTED SOLUTION

Hi,

Your script include is in Service Module Integration scope so the API name should not start with global.

Can you try creating a fresh script include in your scope and verify this

regards
Ankur

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

View solution in original post

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Hope you are doing good.

Can you share the scripts here and not the screenshots?

Regards
Ankur

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

Hi,

 

Client script:

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

var gaDurrMonths = new GlideAjax('global.AddendumCalculation');
gaDurrMonths.addParam('sysparm_name', 'endDate');
gaDurrMonths.addParam('sysparm_durrMonths', g_form.getValue('u_duration_months'));
gaDurrMonths.addParam('sysparm_cntrSD', g_form.getValue('starts'));
gaDurrMonths.getXML(updateCntrED);
}

function updateCntrED(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    g_form.addInfoMessage('answer is: ' + answer);
if(answer){
    g_form.setValue("ends", answer);
}     
}

Script include:

    endDate: function(){
        var cntrSD = this.getParameter('sysparm_cntrSD');
        var durrMonths = this.getParameter('sysparm_durrMonths');        
        this.endDateCalculus(cntrSD, durrMonths);
    },
    
    endDateCalculus: function(startDate, months){
    var gdt = new GlideDateTime(startDate);
    var gdtMonth = new GlideDateTime(startDate).getMonthUTC();
    var gdtYear = gdt.getYearUTC();

    var monthRemainingToAdd = months % 12; 
    var yearsInMonthsToAdd = (months - monthRemainingToAdd) / 12; 

    var newMonth = gdtMonth + monthRemainingToAdd; 

    var finalMonth = newMonth % 12;

    if(finalMonth == 0){
        finalMonth = newMonth;
        var ultimateYearsToAdd = 0;
    }else{
        var finalyearsInMonthsToAdd = newMonth /12; 
        ultimateYearsToAdd = Math.floor(finalyearsInMonthsToAdd);
    }

    var newYear = gdtYear + yearsInMonthsToAdd + ultimateYearsToAdd;

    gdt.setYearUTC(newYear);
    gdt.setMonthUTC(finalMonth);
    gdt.setDayOfMonthUTC(1);
    gdt.addDaysUTC(-1);

    var endDate = gdt.getValue();
    gs.log("endDate in script include is: " + endDate, 'pom0');    
    return endDate;
    },    

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Check this

1) try to add gs.info() to debug in the script include function; gs.log() will break the code in scoped app

2) Is script include client callable?

3) Try to invoke the Script include with the complete API name at line 6 in client script

4) also use getXMLAnswer() instead of getXML() -> no need to change this if point 1 fixes the issue

Regards
Ankur

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

2) Yes, it is client callable

find_real_file.png

3) + 4) tried that, still getting "null"

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

var gaDurrMonths = new GlideAjax('global.AddendumCalculation');
gaDurrMonths.addParam('sysparm_name', 'endDate');
gaDurrMonths.addParam('sysparm_durrMonths', g_form.getValue('u_duration_months'));
gaDurrMonths.addParam('sysparm_cntrSD', g_form.getValue('starts'));
gaDurrMonths.getXMLAnswer(updateCntrED);
}

function updateCntrED(answer) {
    g_form.addInfoMessage('answer is: ' + answer);
if(answer){
    g_form.setValue("ends", answer);
}     
}