Client Scripts, Script Includes & Scoped Applications

Sue Frost
Giga Guru

I've been working with this discussion which was very helpful:   Client Script Date/Time Functions .

Here is my dilemma.

I created "ClientDateTimeUtils" as a global script include - it has uses far beyond a single scoped application.

I have a scoped application from which I'd like to call the script include. The end result will be to reset the Expiry Date if the Effective Date changes.

The Expiry Date should be 90 days after the Effective Date.

I've been looking at it so long, I don't really see it anymore. I've been poking around the community but I can't get it to work. I'm only see the first

alert in the Client Script that returns "newValue". It appears that the Script Include isn't being called at all and I don't know why.

My client script, running on change of the Effective Date field:

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

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

  return;

  }

  //Type appropriate comment here, and begin script below

  var cdt = newValue; //Choose the field to add time from                           <-- was wrong, I have corrected this line as per my reply below

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

  var addtype = 'day'; //The time type.   Can be day, week, month, year.

  var ajax = new GlideAjax('global.ClientDateTimeUtils');

  //var ajax = new GlideAjax('x_enig_quote_extrm.ClientDateTimeUtils');

  ajax.addParam('sysparm_name', 'addDateAmount');

  ajax.addParam('sysparm_fdt', cdt);

  ajax.addParam('sysparm_addtime', addtime);

  ajax.addParam('sysparm_addtype', addtype);

  ajax.getXML(doSomething);

  alert(newValue);

  function doSomething(response){

  alert('into doSomething');

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

  alert(answer);

  }

}

My global, client callable Script Include:

var ClientDateTimeUtils = Class.create();

ClientDateTimeUtils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

      ... <snip> ...

//Takes a glide date field and adds time to it.

//params = sysparm_fdt (the first date/time field), sysparm_addtype (type of time to add - day, week, month, year),sysparm_addtime (amount of time to add based on the type).

addDateAmount: function(){

var firstDT = this.getParameter('sysparm_fdt'); //First Date Field

var addTYPE = this.getParameter('sysparm_addtype'); //What to add - day (addDays()), week (addWeeks()), month (addMonths()), year (addYears())

var addTIME = this.getParameter('sysparm_addtime'); //How much time to add

var day = GlideDate();

day.setValue(firstDT);

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;

1 ACCEPTED SOLUTION

Thanks for the update Sue. Replace script include with below updated code.


var ClientDateTimeUtils = Class.create();


ClientDateTimeUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  addDateAmount: function(){



  var firstDT = this.getParameter('sysparm_fdt'); //First Date Field


  var addTYPE = this.getParameter('sysparm_addtype'); //What to add - day (addDays()), week (addWeeks()), month (addMonths()), year (addYears())


  var addTIME = this.getParameter('sysparm_addtime'); //How much time to add


  var day = GlideDate();


  day.setValue(firstDT);



  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;


  },



  _privateFunction: function() { // this function is not client callable



}




});



View solution in original post

15 REPLIES 15

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Sue,



Replace this global.ClientDateTimeUtils with API name of the script include and make sure client callable checkbox is set to true.


Thanks for the reply. Let me repeat, so I'm sure what you're recommedning.



Replace this global.ClientDateTimeUtils with API name of the script include and make sure client callable checkbox is set to true.



This must apply to the Script Include - it is set as client callable. The API name of the script include is "global.ClientDateTimeUtils".



------



Also replace script include


        var ClientDateTimeUtils = Class.create();


        ClientDateTimeUtils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {


with


        var ClientDateTimeUtils = Class.create();


        ClientDateTimeUtils.prototype.prototype = {


  initialize: function() {


  },



Also in the script include, replace the text as indicated.



-----



I've done both those things but there's no change to the end result.


Can you please share a screenshot of script include. Can you please confirm if script include "Accessible from" is set to all application scope?


Of course ...



5-3-2017 3-15-22 PM.png



The "Accessible From" field WAS incorrectly set. I've changed it to "all application scopes" (as showing above). PROGRESS!


Now I'm getting the second prompt from my original post ( alert('into doSomething'); ) but the last alert that returns an answer is still coming back as "null".