Servicenow

HARSHA GOWDA R
Tera Contributor

Can anyone help me with this code , Below i attached 2 files .So if i select the date for example from 02-01-2024(effective date) to 12-01-2024(effective date) so in the end point its triggering with same year but with diff date and month , can anyone look at the script and files and guide where i went wrong?
Onchange Script

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

    var ga = new GlideAjax('iamServiceAccountOnboardingUtils'); //scriptinclude name
 ga.addParam('sysparm_name', 'calculateEndDate'); //function name
 ga.addParam('sysparm_effective_date', newValue);
 ga.getXML(answerCallback); // Define the callback function
 }

 function answerCallback(response) {
     var answer = response.responseXML.documentElement.getAttribute("answer");

     if (answer) {
         // Set the calculated end date in the end date field
         g_form.setValue('end_date', answer);
     }
 }

Script Include
var iamServiceAccountOnboardingUtils = Class.create();
iamServiceAccountOnboardingUtils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    calculateEndDate: function() {
        var effectiveDate = this.getParameter('sysparm_effective_date');
        var endDate = new GlideDateTime(effectiveDate);
        endDate.getDate().getByFormat("dd-MM-yyyy");
        endDate.addYearsLocalTime(1);
        return endDate.getValue();
    },

 type: 'iamServiceAccountOnboardingUtils'
   
});
10 REPLIES 10

Oya Orhan
Giga Guru

Hi @HARSHA GOWDA R 

When you send date-time value from client side, server side code change the value because of date format type difference. So you need to send date as millisecond.

 

Update your client code as below:

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

    var ga = new GlideAjax('DateTimeUtils'); //scriptinclude name
    ga.addParam('sysparm_name', 'calculateEndDate'); //function name
    var date_number = getDateFromFormat(newValue + ' 00:00:00', g_user_date_time_format);
    ga.addParam('sysparm_effective_date', date_number);
    ga.getXML(answerCallback); // Define the callback function
}

function answerCallback(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");

    if (answer) {
        // Set the calculated end date in the end date field
        g_form.setValue('end_date', answer);
    }
 
update your SI as below:
    calculateEndDate: function() {
        var effectiveDate = this.getParameter('sysparm_effective_date');
        var end_date = new GlideDateTime();
        end_date.setNumericValue(effectiveDate);
        end_date.addYears(1);
        return end_date.getDate();
    }
}
 
If my post helped you, please click the accept solution button and hit the thumbs up! Thank you!
Regards,
Oya

Hi @Oya Orhan 
Code is working fine but the end date is coming one day prior to effective date , here i have attached copy of that plz review and give me the update what changes i have to make

Could you please copy paste your codes here? It works good on my PDI.

Hi @Oya Orhan it should work on portal as well

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

    var ga = new GlideAjax('iamServiceAccountOnboardingUtils'); //scriptinclude name
    ga.addParam('sysparm_name', 'calculateEndDate'); //function name
    var date_number = getDateFromFormat(newValue + ' 00:00:00', g_user_date_time_format);
    ga.addParam('sysparm_effective_date', date_number);
    ga.getXML(answerCallback); // Define the callback function
}

function answerCallback(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");

    if (answer) {
        // Set the calculated end date in the end date field
        g_form.setValue('end_date', answer);
    }
}

var iamServiceAccountOnboardingUtils = Class.create();
iamServiceAccountOnboardingUtils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    calculateEndDate: function() {
        var effectiveDate = this.getParameter('sysparm_effective_date');
        var end_date = new GlideDateTime();
        end_date.setNumericValue(effectiveDate);
        end_date.addMonthsLocalTime(12);
   
        return end_date.getDate();

    },

    type: 'iamServiceAccountOnboardingUtils'

});