Error when setting item in Script Include

CarlJSchmidt
Tera Expert

Hi there, 

 

when working with a scoped Script Include, I face the issue, that I cannot write into the document of the request.
My Script Include is defined similar to this:

 

 

var TestAjax = Class.create();
TestAjax.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    initialize: function() {
    },
	testFunction: function() {
		var gr = new GlideRecord('sys_user');
		gr.query();

		while (gr.next()) {
			var item = this.newItem();     // <--- `item` is undefined

			item.setAttribute('sys_id', gr.getValue('sys_id'));
			item.setAttribute('name', gr.getValue('name'));
		}
	},
    type: 'TestAjax'
});

 

 

 

When testing this Script Include via a Fix Script, I receive the following error:

 

 

Evaluator: com.glide.script.RhinoEcmaError: The undefined value has no properties.
   script : Line(10) column(0)

 

 

Line 10 is the line where I first try to set an attribute for the item element, indicating that item is undefined.

 

Thanks to @Alikutty A who describes the implementation of this.newItem(), I also tried to explicitly redefine this method in the scoped Script Include. But unfortunately, this also did not solve the issue.

 

Is there anything to take special car of when writing Script Includes for scoped applications other than using global.AbstractAjaxProcessor instead of AbstractAjaxProcessor?

 

Any help is much appreciated! Thank you!

1 ACCEPTED SOLUTION

Hi @CarlJSchmidt 

Looking like the Client Callable checkbox is missing in your script include. Make sure your to enable it.

Screenshot 2024-03-22 at 10.58.32.png

 

Cheers,

Tai Vu

View solution in original post

6 REPLIES 6

Neeraj_27
Tera Guru

Hi @CarlJSchmidt,

It is obvious to through an error as newItem() function is not defined in the scirpt include.  Try creating the function in script include with newItem(), define the action which you want to perform. If you are looking for creating the xml entries/node use the below code

newItem: function(name) {
        if (!name)
            name = "item";

        var item = this.getDocument().createElement(name);
        this.getRootElement().appendChild(item);
        return item;
    },

Hi @Neeraj_27,

 

thank you for your reply!
I was assuming, that the newItem() method is inherited from the 

AbstractAjaxProcessor. But I also tried your solution with the following result:

 

var TestAjax = Class.create();
TestAjax.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    initialize: function() {
    },
	testFunction: function() {
		var gr = new GlideRecord('sys_user');
		gr.query();

		while (gr.next()) {
			var item = this.newItem();     // <--- `item` is undefined

			item.setAttribute('sys_id', gr.getValue('sys_id'));
			item.setAttribute('name', gr.getValue('name'));
		}
	},
	newItem: function(name) {
		if (!name) {
			name = 'item';
		}
		var doc = this.getDocument();     // <--- `doc` is undefined
		var item = doc.createElement(name);
		this.getRootElement().appendChild(item);
		return item;
	},
    type: 'TestAjax'
});

 

 

Tai Vu
Kilo Patron
Kilo Patron

Hi @CarlJSchmidt 

You can try to return a JSON data type that's converted to a string instead of XML.

Like below

var TestAjax = Class.create();
TestAjax.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

	testFunction: function() {
		var users = [];
		var gr = new GlideRecord('sys_user');
		gr.query();
		while (gr.next()) {
			var user = {};
			user.sys_id = gr.getUniqueValue();
			user.name = gr.getValue('name');
			users.push(user);
		}
		return JSON.stringify(users);
	},

    type: 'TestAjax'
});

 

function onLoad() {
    var ga = new GlideAjax('TestAjax');
    ga.addParam('sysparm_name', 'testFunction');
    ga.getXMLAnswer(function(answer){
		var users = JSON.parse(answer);
		for (var i in users){
			alert(users[i].name);
		}
	});

}

 

Cheers,

Tai Vu

Hi @Tai Vu,

 

I appreciate your reply, thanks a lot. I tried your solution with a slight modification for the Client Script of the UI Page calling  TestAjax:

 

 

// Called when the form loads.
addLoadEvent(function() {
	var ajax = new GlideAjax('TestAjax');
	// Specify the function of the Script Include to call.
	ajax.addParam('sysparm_name', 'testFunction');
	ajax.getXMLAnswer(function (answer) {
		alert(answer);      // <--- `answer` is null
		var users = JSON.parse(answer);
		for (var i in users) {
			jslog(users[i].name);
		}
	});
})

 

 

 

However, when I test the Script Include via the Fix Script server side, I got all the users back. So the Script Include is working.

 

Assuming that the two methods below are essentially similar, the Script Include just does not seem to have any document element to which can attach its output.

 

 

ajax.getXMLAnswer(function(answer) {
    // Process `answer`.
}

ajax.getXML(function(response) {
    var answer = response.responseXML.documentElement.getAttribute('answer');
    // Process `answer`.
}

 

 

 

This would also fit with the observation I posted above when I try to execute this.getDocument() in the Script Include directly (see reply to @Neeraj_27 post).