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.

Adding 1 year to a date field through a client script

CharlesR1
Kilo Guru

Hello,

On our Contract table, I'm creating a client script that needs to take the date value from the 'starts' field, and populate the 'ends' field with the start value plus one year, whenever the true/false field 'u_endless_contract' is ticked. It then needs to remove the 'ends' value when it is unticked. At the same time it updates the Auto-renewal field (u_autoren).

I am using the scripts below, and while the removal of the 'ends' value and the auto-renewal updates are working fine, the 'ends' value is not being populated when it is ticked. Is there anything obvious I am missing?

Client Script:

function onChange(control, oldValue, newValue, isLoading) {  

            if (isLoading || newValue == '') {  

                  return;  

            }  

    //checking if endless is not checked - this section works fine...

  if(g_form.getValue('u_endless_contract') == 'false'){

    g_form.setValue(('ends') , "");

    g_form.setValue(('u_autoren') , "");

}

    else if(g_form.getValue('u_endless_contract') == 'true'){

    g_form.setValue(('u_autoren') , "yes"); // this works fine

                var startTime = g_form.getValue('starts');  

                var ga = new GlideAjax('ContractEndless');  

                ga.addParam('sysparm_name', 'addYears');  

                ga.addParam('sysparm_start', startTime);  

                ga.addParam('sysparm_years', '1');  

                ga.getXML(parseResponse);  

      }  

      function parseResponse(response) {  

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

            g_form.setValue('ends', answer);  

      }  

}

Client Callable Script Include:

var ContractEndless = Class.create();  

ContractEndless.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    addYears: function() {  

          var eDate = this.getParameter('sysparm_start');  

          var addYears = this.getParameter('sysparm_years');  

          var gdt = new GlideDate(eDate);  

          gdt.addYearsLocalTime(addYears);  

             

          return gdt.getDisplayValue();  

    },  

});

Thanks very much for any help offered.

Charles

1 ACCEPTED SOLUTION

CharlesR1
Kilo Guru

Thanks for your help guys - got it working client side only using the following:



function onChange(control, oldValue, newValue, isLoading) {  


            if (isLoading || newValue == '') {  


                  return;  


            }  


    //checking if endless is not checked        



  if(g_form.getValue('u_endless_contract') == 'false'){


    g_form.setValue(('ends') , "");


    g_form.setValue(('u_autoren') , "");


}



    else if(g_form.getValue('u_endless_contract') == 'true'){


    g_form.setValue(('u_autoren') , "yes");



  var year = '1';


      var start = g_form.getValue('starts');


  var startDateObj = new Date(getDateFromFormat(start, g_user_date_format));


  var endValue = "";


      if(start != "" ){


              var y = startDateObj.getFullYear();


              var endYear = parseInt(y)+parseInt(year);


  startDateObj.setFullYear(endYear);


              endValue = formatDate(startDateObj, g_user_date_format);


      }


              g_form.setValue('ends', endValue);


}


}


View solution in original post

3 REPLIES 3

Mike Allen
Mega Sage

Chuck Tomasi
Tera Patron

GlideDate doesn't have an addYears() method. I think you want to use a GlideDateTime() and convert it back to GlideDate if your field is a date only field.



The script include is likely throwing an error and that's why you're not getting back your data - check the logs.


CharlesR1
Kilo Guru

Thanks for your help guys - got it working client side only using the following:



function onChange(control, oldValue, newValue, isLoading) {  


            if (isLoading || newValue == '') {  


                  return;  


            }  


    //checking if endless is not checked        



  if(g_form.getValue('u_endless_contract') == 'false'){


    g_form.setValue(('ends') , "");


    g_form.setValue(('u_autoren') , "");


}



    else if(g_form.getValue('u_endless_contract') == 'true'){


    g_form.setValue(('u_autoren') , "yes");



  var year = '1';


      var start = g_form.getValue('starts');


  var startDateObj = new Date(getDateFromFormat(start, g_user_date_format));


  var endValue = "";


      if(start != "" ){


              var y = startDateObj.getFullYear();


              var endYear = parseInt(y)+parseInt(year);


  startDateObj.setFullYear(endYear);


              endValue = formatDate(startDateObj, g_user_date_format);


      }


              g_form.setValue('ends', endValue);


}


}