GlideDate Giving"Error while running Client Script "Valid Request Date": ReferenceError: GlideDateTi

edharris
Tera Contributor

I am trying to write a script that pulls a date variable and checks to see if it is 2 weeks from the current date, but I run into this error:

" Error while running Client Script "Valid Request Date": ReferenceError: GlideDateTime is not defined" 

function onSubmit() {
   var requestDate = new GlideDateTime(g_form.getValue('request_date'));
   var currentDate = new GlideDateTime();
   daysDifference = GlideDate.subtract(requestDate, currentDate);

   if (daysDifference < 14) {
       alert('A valid release date is required.');
       return false;
   }
   return true;
}
 
If anyone has any suggestions or solutions, I would greatly appreciate it. 
4 REPLIES 4

Sandeep Rajput
Tera Patron
Tera Patron

@edharris You are trying to use GlideDateTime which is a Server side object and not available via the client script. Instead you can choose to update your implementation as follows.

 

1. Create a Script include as follows.

Screenshot 2023-10-07 at 12.07.13 AM.png

Here is the source code.

var HRUtils = Class.create();
HRUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    compareDates: function() {
		var request_date = this.getParameter('sysparm_request_date');
        var requestDate = new GlideDateTime(request_date);
        var currentDate = new GlideDateTime();
        var daysDifference = GlideDate.subtract(requestDate, currentDate);

        if (daysDifference < 14) {            
            return 'false';
        }
        return 'true';
    },

    type: 'HRUtils'
});

2. Here is how you should modify your client script.

function onSubmit() {
  var ga = new GlideAjax('HRUtils'); // HRUtils is the script include class 
ga.addParam('sysparm_name','compareDates'); // compareDates is the script include method 
ga.addParam('sysparm_request_date',g_form.getValue('request_date')); // Set parameter sysparm_request_date 
ga.getXML(getResponse);  

// the callback function for returning the result from the server-side code
function getResponse(response) {  
   var answer = response.responseXML.documentElement.getAttribute("answer"); 
    if(answer=='false'){
     alert('A valid release date is required.');
       return false;
    }
return true;
}

Hope this helps.

When these two scripts are executed, I get a "There is a JavaScript error in your browser console" when the portal is loaded. Script looks good but if you have any other suggestions that would be great. 

 

@edharris It looks like you are trying to use GlideDateTime in your client script, can you post the snapshot of your client script and script include here.

Anubhav24
Mega Sage
Mega Sage

@edharris ,

As already stated by a colleague , GlideDateTime is server side object, you need to calculate the difference on server side script i.e. script include.

To calculate difference of dates you can convert the complete date in milliseconds

        var date1 = new GlideDateTime(<start date>);
       var nDate1 = date1.getNumericValue();       
        var date2 = new GlideDateTime(<end date>);
       var nDate2 = date2.getNumericValue();       
var ndiff = nDate2-nDate1;
now you have got the difference you can convert this value into hours / seconds / days and implement your logic.