Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

The validation failing requesting some help Addition Failing in script

Are Kaveri
Tera Contributor

Hi All,

 

I am working on the below script:

 

UI policy:

 

 

 

function onCondition() {

    var DDSLA = new GlideAjax("incident_Utils");
    DDSLA.addParam("sysparm_name", "ValidateDueDateSLA");
    DDSLA.addParam("sysparm_incidentId", g_form.getUniqueValue());
    DDSLA.getXML(ValidateDuedate);
}

function ValidateDuedate(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    alert("get si value " + answer);
    if (answer == 'true') {
        alert("Less than or equal to 12 / 18 months");
        g_form.clearValue('u_due_date_missed_sla_rationale');
        g_form.setMandatory('u_due_date_missed_sla_rationale', false);
        g_form.setVisible('u_due_date_missed_sla_rationale', false);

    } else {
        alert("test greater than 12 and 18 months");
        g_form.setVisible('u_due_date_missed_sla_rationale', true);
        g_form.setMandatory('u_due_date_missed_sla_rationale', true);

    }
}

 

 

 

 

Script include:

 

 

 

ValidateDueDateSLA: function() {

        var recordId = this.getParameter('sysparm_incidentId);
        var isUpdated = false;

        var gr = new GlideRecord('incident');
        if (gr.get('7b3d3451eb6dd6142008fcbbcad0cd87')) {


            var identifiedStr = gr.getValue('u_identification_date');
            var  BusinessDueStr = gr.getValue('u_business_activities_due_date');
            var Dep = gr.getValue('u_dependency');

            var IdentifiedDate = new GlideDate();
            IdentifiedDate.setValue(identifiedStr);
            gs.addInfoMessage('Converted into Date identified: ' + IdentifiedDate);


            var BusinessDueDate = new GlideDate();
            BusinessDueDate.setValue(BusinessDueStr);
            gs.addInfoMessage('Converted into Date BADD: ' + BusinessDueDate);

            var IdentifiedDatePlus12Months = IdentifiedDate.addMonthsLocalTime(12); //addMonthsLocalTime
            var IdentifiedDatePlus18Months = IdentifiedDate.addMonthsLocalTime(18);
		

            var isBADDWithIn12Months = BusinessDueDate <= IdentifiedDatePlus12Months;
            var isBADDWithIn18Months = BusinessDueDate <=IdentifiedDatePlus18Months;



            gs.addInfoMessage('IdentifiedDatePlus12Months : ' + IdentifiedDate); // Just for debugging in background script, to be removed from Business rules
            gs.addInfoMessage(''IdentifiedDatePlus18Months : ' + BusinessDueDate);
            if (Dep == 'no' && isBADDWithIn12Months) {
                isUpdated = true;
            } else if (Dep == 'yes' && isBADDWithIn18Months) {
                isUpdated = true;
            }
        }
        return isUpdated;
    },

 

 

 

 

I am facing issue in the below scenario:

identification date = 28/10/2024

Business Due date = 28/10/2026

But when adding 18 months in script include using AddMonths(18) to identified date... It was adding more months as below

getting Identification date = 28/04/2027

 

 

I am not understanding what is the issue not able to replicate Please help me....

 

 

 

 

1 ACCEPTED SOLUTION

Runjay Patel
Giga Sage

Hi @Are Kaveri ,

 

I got your issue, silly mistake 

As per your existing code you are adding 12+18=20 month in "IdentifiedDate".

 

replace below code

            var IdentifiedDatePlus12Months = IdentifiedDate.addMonthsLocalTime(12); //addMonthsLocalTime
            var IdentifiedDatePlus18Months = BusinessDueDate.addMonthsLocalTime(18);

 

or if you want to add 12 and 18 month in same date then you have to do below.

     var IdentifiedDatePlus12Months = IdentifiedDate.addMonthsLocalTime(12); //addMonthsLocalTime
 IdentifiedDate.addMonthsLocalTime(-12);
            var IdentifiedDatePlus18Months = BusinessDueDate.addMonthsLocalTime(18);

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

 

 

View solution in original post

2 REPLIES 2

Runjay Patel
Giga Sage

Hi @Are Kaveri ,

 

I got your issue, silly mistake 

As per your existing code you are adding 12+18=20 month in "IdentifiedDate".

 

replace below code

            var IdentifiedDatePlus12Months = IdentifiedDate.addMonthsLocalTime(12); //addMonthsLocalTime
            var IdentifiedDatePlus18Months = BusinessDueDate.addMonthsLocalTime(18);

 

or if you want to add 12 and 18 month in same date then you have to do below.

     var IdentifiedDatePlus12Months = IdentifiedDate.addMonthsLocalTime(12); //addMonthsLocalTime
 IdentifiedDate.addMonthsLocalTime(-12);
            var IdentifiedDatePlus18Months = BusinessDueDate.addMonthsLocalTime(18);

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

 

 

Brad Bowman
Kilo Patron
Kilo Patron

Are the custom fields you added to the incident table the type of date/time or date? What are you seeing in the 4 infoMessages (after correcting the extra apostrophe in the fourth so that it displays)?  Are the results the same if you addMonthsUTC instead?