Tuesday, 29 January 2019

Client Script Date/Time Functions

  1. getNowDateTimeDiff

    This ajax function will allow you to calculate the date/time difference between a field on the form and the now date/time. You can specify a return type - "dttype" to get the result in seconds, minutes, hours, or days. See the comment below for the parameter values.
var cdt = g_form.getValue('due_date'); //First Date/Time field
var dttype = 'minute'; //this can be day, hour, minute, second. By default it will return seconds.

var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name','getNowDateTimeDiff');
ajax.addParam('sysparm_fdt', cdt);
ajax.addParam('sysparm_difftype', dttype);
ajax.getXML(doSomething);

function doSomething(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
}

  1. getDateTimeDiff

    This ajax function will allow you to send in two different glide date/time fields and return a calculation of their date/time difference in seconds, minutes, hours, or days.

var cdt = g_form.getValue('due_date'); //First Date/Time field
var sdt = g_form.getValue('expected_start'); //Second Date/Time field
var dttype = 'minute'; //this can be day, hour, minute, second. By default it will return seconds.

var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name','getDateTimeDiff');
ajax.addParam('sysparm_fdt', cdt);
ajax.addParam('sysparm_sdt', sdt);
ajax.addParam('sysparm_difftype', dttype);
ajax.getXML(doSomething);

function doSomething(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
  1. getDateTimeBeforeNow

    This ajax function will take a glide date/time field and return the amount of time till the now date/time. A positive number will represent prior to now, and negative will be how much time after now. This is pretty much a duplicate of the first one "getNowDateTimeDiff" but it gives you the opposite in positive, negative numbers. Positive = before, negative = after.
var cdt = g_form.getValue('due_date'); //first Date/Time field
var dttype = 'minute'; //this can be day, hour, minute, second. By default it will return seconds.

var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name','getDateTimeBeforeNow');
ajax.addParam('sysparm_fdt', cdt);
ajax.addParam('sysparm_difftype', dttype);
ajax.getXML(doSomething);

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

}
  1. getDateTimeBeforeNowBool

    This ajax function will return a true or false if the date/time field is before the now date/time. This made it simpler, rather than having to do more processing on the client side, just evaluate true/false. Makes it simple.
var cdt = g_form.getValue('due_date'); //first Date/Time field

var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name','getDateTimeBeforeNowBool');
ajax.addParam('sysparm_fdt', cdt);
ajax.getXML(doSomething);

function doSomething(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
}

  1. getNowDateTime

    This function returns the date and time of right now.
    var ajax = new GlideAjax('ClientDateTimeUtils');
    ajax.addParam('sysparm_name','getNowDateTime');
    ajax.getXML(doSomething);

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

  1. getNowDate

    This function returns the date of right now.
var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name','getNowDate');
ajax.getXML(doSomething);

function doSomething(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
  1. getNowTime

    This function returns the time of right now.
var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name','getNowTime');
ajax.getXML(doSomething);

function doSomething(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
  1. addDateTimeAmount

    With this ajax function you can add more time to a glide date/time field. You can add seconds, minutes, hours, days, weeks, months, and years. If you want to take time away, use a negative number in the addtime variable.

    Limitation:

    One limitation that I have found so far is in the return date/time format. The added-to-time new date/time is returned in the standard internal date/time format. I am trying to figure out how to return it based on the user defined date/time format. Still a work in progress.

    var cdt = g_form.getValue('due_date'); //Choose the field to add time from
    var addtime = 3; //The amount of time to add
    var addtype = 'day'; //The time type.   Can be second, minute, hour, day, week, month, year.

    var ajax = new GlideAjax('ClientDateTimeUtils');
    ajax.addParam('sysparm_name', 'addDateTimeAmount');
    ajax.addParam('sysparm_fdt', cdt);
    ajax.addParam('sysparm_addtime', addtime);
    ajax.addParam('sysparm_addtype', addtype);
    ajax.getXML(doSomething);


    function doSomething(response){
           var answer = response.responseXML.documentElement.getAttribute("answer");
           //You could then take the new Date/Time answer and set the value of another field.
           // g_form.setValue('expected_start', answer);
           alert(answer);
}
  1. addDateAmount

    This is a function to add time to a Glide Date field. You can add days, weeks, months, or years.
var cdt = g_form.getValue('some_date'); //Choose the field to add time from
var addtime = 3; //The amount of time to add
var addtype = 'day'; //The time type.   Can be day, week, month, year.

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


function doSomething(response){
       var answer = response.responseXML.documentElement.getAttribute("answer");
       alert(answer);
}

  1. addTimeAmount

    This is a function to add time to a Glide Time field. You can add seconds, minutes, hours.

    var cdt = g_form.getValue('some_time_field'); //Choose the field to add time from
    var addtime = 3; //The amount of time to add
    var addtype = 'day'; //The time type.   Can be second, minute, hour.

    var ajax = new GlideAjax('ClientDateTimeUtils');
    ajax.addParam('sysparm_name', 'addTimeAmount');
    ajax.addParam('sysparm_fdt', cdt);
    ajax.addParam('sysparm_addtime', addtime);
    ajax.addParam('sysparm_addtype', addtype);
    ajax.getXML(doSomething);


    function doSomething(response){
           var answer = response.responseXML.documentElement.getAttribute("answer");
           alert(answer);
}





GlideAjax Asynchronous Call

Client Side (Client Script):
function onChange(control, oldValue, newValue, isLoading) {
      if (isLoading || newValue == '') {
              return;
      }
     
      var ga = new GlideAjax('asu_GetLocationData');
      ga.addParam('sysparm_name', 'getCampus');
      ga.addParam('sysparm_buildingid', g_form.getValue("u_building"));
      ga.getXML(updateCampus);
}
     
function updateCampus(response) {
      var answer = response.responseXML.documentElement.getAttribute("answer");
      var clearvalue; // Stays Undefined
      if (answer) {
              var returneddata = answer.evalJSON(true);
              g_form.setValue("campus", returneddata.sys_id, returneddata.name);
      } else {
              g_form.setValue("campus", clearvalue);
      }
}
Server Side (Script Include):
var asu_GetLocationData = Class.create();
asu_GetLocationData.prototype = Object.extendsObject(AbstractAjaxProcessor, {
      getCampus: function () {
              var buildingid = this.getParameter('sysparm_buildingid');
              var loc = new GlideRecord('cmn_location');
              if (loc.get(buildingid)) {
                      var campus = new GlideRecord('cmn_location');
                      if (campus.get(loc.parent)){
                              var json = new JSON();
                              var results = {
                                      "sys_id": campus.getValue("sys_id"),
                                      "name": campus.getValue("name")
                              };
                              return json.encode(results);
                      }
              } else {    
                      return null;
              }            
      }
});


Color coding above indicate as follows:
# Yellow:
This is the name of the class you create. This is usually the same as the name of the Script Include.
# Magenta:
This is the name of the function to use in the script include. You can have a single script include with multiple functions that accept and return different parameters. For example, you could create a single Script Include for getting data related to users and keep adding functions to it as needed.
# Green:
This is a parameter that is passed through the URL of the AJAX call. You can add more than one parameter. usually this is information you will use to make a GlideRecord call in the Script Include.
# Red:
This is the function that asynchronously waits for a response. Any code that you need to wait for a response needs to go in the function referred to in the getXML(). Code that doesn't need to wait goes directly after the getXML() call inside the main Client Script function and won't wait for a response before executing.
# Cyan:
These are the pieces of data you need from the Server. They are added to an object in the Script Include and passed back to the Client Script. You can do anything with these when they are returned. In this example they are used to set a value on the form.

Note: Best Practice says using JSON.stringify() instead json.encode() to do you encoding, and JSON.parse() instead of json.decode() as they is ECMAScript5 compliant and not a SN "version" of the same things. ES5 has been in place since Helsinki. If you're not there yet, here's another good reason to get up to date