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

Paul,



Thanks for the help on this..I did read another thread referring to declaring the function although didn't show the answer. This makes complete sence to me and is now returning the answer I need (once id put .toSting) on.



A question though as I am relatively new to using script includes called by CS's. Whats the purpose of putting:


type: 'ProjectTimeCardTotals'


What does that part of the code do?


This is automatically generated into the Script field when you create a new Script Include.


I think it is there purely for being able to determine what 'Class' an object is.



For example:


var arr = new ArrayUtil();


gs.print(arr.type);


gs.print(typeof arr);



Output


ArrayUtil


object



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