GlideAjax returns null

thomaskennedy
Tera Guru

This is not for the service catalog.

 

My Script include (it's client callable). I would ordinarily not include my params in the function signature, but I added them here for troubleshooting.

var DecisionUtility = Class.create();
DecisionUtility.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    initialize: function() {},
	
	// params: modelName, exactMatch, excludeSysId
	// Returns: [{name:"Dell XYZ"}, ...]
	getModelsByName: function(modelName, exactMatch, excludeSysId) {
		gs.info("getModelsByName 1");
		
		if(this["getParameter"]) {
			var modelName = this.getParameter("sysparm_modelname") || modelName;
			var exactMatch = this.getParameter("sysparm_exactmatch") || exactMatch;
			var excludeSysId = this.getParameter("sysparm_excludesysid") || excludeSysId;
		}
		
		gs.info("getModelsByName 2");
		
		var result = [];
		var gr = new GlideRecord("cmdb_model");
		if(excludeSysId) gr.addQuery("sys_id", "!=", excludeSysId);
		if(exactMatch) {
			gr.addQuery("name", "=", modelName);
		} else {
			gr.addQuery("name", "CONTAINS", modelName);
		}
		
		gs.info("getModelsByName 3");
		
		gr.setLimit(3);
		gr.query();
		while(gr.next()) {
			result.push(
				{
					"name": gr.getValue("name")
				}
			);
		}
		
		gs.info("getModelsByName 4");
		
		return JSON.stringify(result);
	},
	
    type: 'DecisionUtility'
});

My onChange Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    if (newValue != "") {
        // also tried including the scope
        //var ga = new GlideAjax('x_acso_spencer_imp.DecisionUtility');
	var ga = new GlideAjax('DecisionUtility');
        ga.addParam('sysparm_name', "getModelsByName");
        ga.addParam('sysparm_modelname', newValue);
        ga.addParam('sysparm_exactmatch', false);
        ga.addParam('sysparm_excludesysid', g_form.getUniqueValue());
        ga.getXML(cb);
    }

    function cb(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        alert(answer);
    }
}

The result is null.

 

However, the method produces the expected outcome when run at the command line:

var util = new x_acso_spencer_imp.DecisionUtility();
var result = util.getModelsByName("Dell");
gs.info(JSON.stringify(result))
x_acso_spencer_imp (DecisionUtility): getModelsByName 1
x_acso_spencer_imp (DecisionUtility): getModelsByName 2
x_acso_spencer_imp (DecisionUtility): getModelsByName 3
x_acso_spencer_imp (DecisionUtility): getModelsByName 4
x_acso_spencer_imp: "[{\"name\":\"Dell Command | Integration Suite for System Center 5\"},{\"name\":\"Dell Command | Power Manager v2\"},{\"name\":\"Dell Command | Update 10 v4 for Windows\"}]"

There is no error in the log. The info statements do not appear in the log when I run it through ajax.

The script include did not include Object.extendsObject.. at first; I added that later when I wanted to add ajax calls.

So, what's the problem here?

3 REPLIES 3

Bert_c1
Kilo Patron

Hi,

 

Maybe use 'ga.getXMLAnser(cb);' in your onChange client script.

 

also, I have a simple example, where my script include has the following:

 

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

	responseStr: function() {
        var userName = this.getParameter("sysparm_user_name");
 
        // Build the payload. You can return additional data if needed. 
        var result = "Hello "+userName;

        return JSON.stringify(result);
    },

    type: 'HelloWorld'
});

My onLoad client script:

 

function onLoad() {
   //Type appropriate comment here, and begin script below
   var ga = new GlideAjax('HelloWorld'); // GetUserInfo is the script include name 
   ga.addParam('sysparm_name','responseStr'); // is the function in the script include that we're calling 
//   ga.addParam('sysparm_user_name','fred.luddy'); // set user to Fred Luddy 
	ga.addParam('sysparm_user_name',g_user.userName); // set user to Fred Luddy 


   /* Call HelloWorld.responseStr() with user set to Fred Luddy and use the callback function ManagerParse() to return the result when ready */
   ga.getXMLAnswer(ManagerParse);  		
}

// callback function for returning the result from the script include
function ManagerParse(response) {  
   alert(response);
}

 

Maybe others here have more to offer. You don't say if any alert is seen on the form.

This is what I get:

thomaskennedy_0-1679952434545.png

In fact the whole param to the callback is null:

function cb(response) {
  alert(response); // null
}

And the log messages do not appear. So it's not invoking anything on the server and I've no idea why not. 

mush
Tera Guru

Thank you!
I've been trying to figure this out and your post really helped me!