Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Why does my script include return "empty"?

JCavin
Kilo Contributor

Here's the situation: I get an XML object back. The problem is that it only ever has the elements and attributes that were added during the request. I can't get an answer/value back from the server.

I have a UI Action calling a Script Include. The Script Include is in the Global app and the client-side UI Script is in Audit.

Here's how the Script Include is setup:

find_real_file.png

Here's the Script Include code:

var prettydiffSN = Class.create();
prettydiffSN.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	initialize: function (){
		var result = this.newItem("result");
		result.setAttribute("message", "The great payload");
	},
    type: 'prettydiffSN'
});

 

Here's the client-side code (in a UI Action, with the Client box checked):

function runPrettyDiffPrev() {
	jslog('prev Version ui action');
	
	var ajax = new GlideAjax('global.prettydiffSN');
	ajax.addParam('sysparm_name', "initialize");
	ajax.getXML(getDiffCode);
	
}


function getDiffCode(response) {
	var doc = response.responseXML.getElementsByTagName("result");
	var message = doc[0].getAttribute("message");
	jslog(message);
	
}

 

Here's the result:

<?xml version="1.0" encoding="UTF-8"?><xml sysparm_max="15" sysparm_name="initialize" sysparm_processor="global.prettydiffSN"/>

 

Actually, this version throws an error "Cannot read property 'getAttribute' of undefined". But, I can't find where it deviates from the documentation:

https://docs.servicenow.com/bundle/kingston-application-development/page/script/ajax/topic/p_AJAX.ht...

 

1 ACCEPTED SOLUTION

Akshat Rastogi
Giga Contributor

Hi,

Instead of calling initialize function call say XYZ, for example:

Script include:

var prettydiffSN = Class.create();
prettydiffSN.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    XYZ: function (){
        var result = this.newItem("result");
        result.setAttribute("message", "The great payload");
    },
    type: 'prettydiffSN'
});

 

Client script:

function onLoad() {

    var ajax = new GlideAjax('global.prettydiffSN');
    ajax.addParam('sysparm_name', "XYZ");
    ajax.getXML(getDiffCode);
    

function getDiffCode(response) {
    var doc = response.responseXML.getElementsByTagName("result");
    var message = doc[0].getAttribute("message");
    jslog(message);
    alert(message);
    
}
}

 

View solution in original post

3 REPLIES 3

Rogers Cadenhe1
Giga Guru

When you want to call a script include function asynchronously from a client script or other client-side code, the function needs to return a value:

initialize: function() {
  var result = this.newItem("result");
  result.setAttribute("message", "The great payload");
  return result;
},



In the client code the value returned can be retrieved by calling the responseXml.documentElement.getAttribute("answer") method:


var ajax = new GlideAjax('global.prettydiffSN');
ajax.addParam('sysparm_name', "initialize");
ajax.getXML(getDiffCode);

function getDiffCode(response) {
var doc = response.responseXML.documentElement.getAttribute("answer");
}

It turns out, your approach is the simplest (most proper?) way to do this. However, I was shadowing the "initialize" function. After I fixed that, the tag/attribute, "result"/"message", were present along with the returned value you suggested.

Akshat Rastogi
Giga Contributor

Hi,

Instead of calling initialize function call say XYZ, for example:

Script include:

var prettydiffSN = Class.create();
prettydiffSN.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    XYZ: function (){
        var result = this.newItem("result");
        result.setAttribute("message", "The great payload");
    },
    type: 'prettydiffSN'
});

 

Client script:

function onLoad() {

    var ajax = new GlideAjax('global.prettydiffSN');
    ajax.addParam('sysparm_name', "XYZ");
    ajax.getXML(getDiffCode);
    

function getDiffCode(response) {
    var doc = response.responseXML.getElementsByTagName("result");
    var message = doc[0].getAttribute("message");
    jslog(message);
    alert(message);
    
}
}