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.

GlideDate() is working in client Script, however the requirements should be in GlideDateTime (). Is there anyone who can assist me with this?

Ashoka Panchal
Tera Contributor

var recPriority = g_form.getValue('u_rec_rating');
var cdt = new Date();  // When I use GlideDateTime() it's showing error.
var addtime;
var addtype = 'day';
if (recPriority == '2') {

addtime = 7; //The amount of time to add
}
if (recPriority == '3' || recPriority == '4') {

addtime = 90; //The amount of time to add

}

var ajax = new GlideAjax('global.ClientDateTimeUtils');
ajax.addParam('sysparm_name', 'addDateAmount');
ajax.addParam('sysparm_fdt', cdt);
ajax.addParam('sysparm_addtime', addtime);
ajax.addParam('sysparm_addtype', addtype);
ajax.getXML(doSomething); //Need callback function

function doSomething(response) {
//Sets field
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('due_date', answer);

Current Result:  find_real_file.png

Expected : DateTime

15 REPLIES 15

addDateTimeAmount: function(){
var firstDT = this.getParameter('sysparm_fdt'); //First Date-Time Field
var addTYPE = this.getParameter('sysparm_addtype'); //What to add - second (addSeconds()), minute (need to add conversion), hour (need to add conversion), day (addDays()), week (addWeeks()), month (addMonths()), year (addYears())
var addTIME = this.getParameter('sysparm_addtime'); //How much time to add
var day = GlideDateTime(firstDT);

if(addTYPE == 'second'){day.addSeconds(addTIME);}
else if (addTYPE == 'minute'){day.addSeconds(addTIME*60);}
else if (addTYPE == 'hour'){day.addSeconds(addTIME*(60*60));}
else if (addTYPE == 'day'){day.addDays(addTIME);}
else if (addTYPE == 'week'){day.addWeeks(addTIME);}
else if (addTYPE == 'month'){day.addMonths(addTIME);}
else if (addTYPE == 'year'){day.addYears(addTIME);}
else {day.addDays(addTIME);}

//return "First Date: " + firstDT + " -Time to Add: " + addTIME + " -Add Type: " + addTYPE + " -Added Time: " + day;
return day;
},

 

I've used this one, it is not working

 

Hi Ashoka,

                       Check below code i have tested in background script, it's working fine for me, you can remove testing code lines and try in your script include

    var firstDT = this.getParameter('sysparm_fdt'); //Ensure this is datetime variable  
    var addTYPE = this.getParameter('sysparm_addtype'); // it should be string 
    var addTIME = this.getParameter('sysparm_addtime'); // it should be integer value
        
		
      var day = GlideDateTime(firstDT);
		
		// for testing purpose in backgorund script
		//var addTYPE ='year';   // for test
		//var addTIME = 2;       // for test     
		//var day = GlideDateTime(); // for test adding values in todays date
       
        var days = addTIME;
        var week = days * 7;
        var month = days * 30;
        var year = days * 365;

        if (addTYPE == 'day') {
            day.addDays(days);
        } else if (addTYPE == 'week') {
            day.addDays(week);
        } else if (addTYPE == 'month') {
            day.addDays(month);
        } else if (addTYPE == 'year') {
            day.addDays(year);
        } else {
            day.addDays(days);
        }

     //    gs.print('Check-5 Type is :' + addTYPE + " Add Time : "+ addTIME + " Date after adding addTime :"  + day);

return day.getDisplayValue();

 

Result :

find_real_file.png

Hi Have you tried above script ?

Yes, I tried. It's throwing an error.  Can you please check it in onchange client script? 

 

find_real_file.png

Hi

           where ever you have used GlideDateTime make sure syntax should be like this

 var day = new GlideDateTime();

      and make sure it is Server-side code, don't use it in Client script. 

Check these 2 things and let me know