Call Script Include from Client Script - Null value returned

Rshear
Kilo Expert

Ok, so here again...After a few weeks of not getting to involved with SN dev it seems my minds gone blank.

Im currently writing a client script which calls a script include which will pass back a total value of time fields found on associated records. Every time I run it (open up the record as the CS is on load) it shows Null on the alert Ive set..Im sure im going mad!

Client script (on load):

function onLoad() {

  var ga = new GlideAjax('ProjectTimeCardTotals');

ga.addParam('sysparm_project',   g_form.getValue('u_project_no'));

ga.getXML(TotalProjectTimeCParse);

  alert("this is total " +total);

function TotalProjectTimeCParse(response) {

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

alert (answer);

}

}

Script include:

ProjectTimeCardTotals = function() {

var total = 0;

var project = this.getParameter('sysparm_project');

var gr = new GlideRecord('u_daily_time_card');  

gr.addQuery('u_project_no', project);  

gr.query();  

while (gr.next()) {  

  var duration = gr.u_time.dateNumericValue();

  total += duration;

}

  gs.log("this is the total " +total);

    return total;

  };

The log (highlighted in red) shows the value I want so I know its there

The alert highlighted in blue is what i need to show whats highlighted in red also..but for some reason returns null

Any assistance much appreciated.

1 ACCEPTED SOLUTION

The SN Nerd
Giga Sage
Giga Sage

Issues:


  • Your class ProjectTimeCardTotals Script include need to extends "global.AbstractAjax Processor"
  • You need to call a function within the Script include in the client script by sysparm_name in your client script


Try this



Client Script


function onLoad() {


  var ga = new GlideAjax('ProjectTimeCardTotals');


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


ga.addParam('sysparm_project',   g_form.getValue('u_project_no'));


ga.getXML(TotalProjectTimeCParse);


  alert("this is total " +total);


function TotalProjectTimeCParse(response) {


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


alert (answer);


}


}




Script Include


var ProjectTimeCardTotals = Class.create();


ProjectTimeCardTotals.prototype = Object.extendsObject(AbstractAjaxProcessor,




getTotal: function() {


  var total = 0;


  var project = this.getParameter('sysparm_project');


   


  var gr = new GlideRecord('u_daily_time_card');


  gr.addQuery('u_project_no', project);


  gr.query();


   


  while (gr.next()) {


    var duration = gr.u_time.dateNumericValue();


    total += duration;


  }


    gs.log("this is the total " +total);


      return total;


},


type: 'ProjectTimeCardTotals'


});



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

View solution in original post

6 REPLIES 6

Mark Stanger
Giga Sage

Might try converting it to a string before returning...



return total.toString();


Artemis15
Kilo Guru

Hi,



Could you please check that your log is getting printed. Is script include "client callable" checkbox is checked.


B'coz for script include to be client callable it should extend "global.AbstractAjax Processor".



Use this thread for help : GlideAjax - Clientscript display null as server response



Hope this helps.



--


Cheers,


AR


Thanks for the reply gents. .


The script Inc is checked as client call able and is outputting the right value to the log so I knw that bits working.


I will try making it a string before sending to client script however not sure that will work as I ran a quick test earlier...


If I put return "this is a tet" I still get null in the clent script alert.


The SN Nerd
Giga Sage
Giga Sage

Issues:


  • Your class ProjectTimeCardTotals Script include need to extends "global.AbstractAjax Processor"
  • You need to call a function within the Script include in the client script by sysparm_name in your client script


Try this



Client Script


function onLoad() {


  var ga = new GlideAjax('ProjectTimeCardTotals');


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


ga.addParam('sysparm_project',   g_form.getValue('u_project_no'));


ga.getXML(TotalProjectTimeCParse);


  alert("this is total " +total);


function TotalProjectTimeCParse(response) {


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


alert (answer);


}


}




Script Include


var ProjectTimeCardTotals = Class.create();


ProjectTimeCardTotals.prototype = Object.extendsObject(AbstractAjaxProcessor,




getTotal: function() {


  var total = 0;


  var project = this.getParameter('sysparm_project');


   


  var gr = new GlideRecord('u_daily_time_card');


  gr.addQuery('u_project_no', project);


  gr.query();


   


  while (gr.next()) {


    var duration = gr.u_time.dateNumericValue();


    total += duration;


  }


    gs.log("this is the total " +total);


      return total;


},


type: 'ProjectTimeCardTotals'


});



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022