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.

Assistance with GlideAjax

kristenmkar
Mega Sage

Good evening! 

 

I am trying to push the "User" reference field from the csm_consumer table to the called_by field within Interaction. I created a Script Includes as well as a Client Script but still no luck.  Do you guys see what my problem could be? Thank you! 

 

client script:


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

var user = new GlideAjax('getUserInfo'); // Specify Script Include name
user.addParam('sysparm_name', 'getUser'); // Parameter is the function that we're calling
user.addParam('sysparm_user', g_form.getValue('consumer')); // Parameter is consumer's sys_id
user.getXML(callback);
// getXML sends the server a request to execute the function and parameters associated with this GlideAjax object
// Server processes the request asynchronously and returns the results via the function specified as the callback

// Callback function for returning the result from the Script Include
function callback(response) {
var sysUser = response.responseXML.documentElement.getAttribute("answer");
// Do whatever you want with the response
g_form.setValue('opened_for', sysUser);
}
}

 

Script includes:
var getUserInfo = Class.create();
getUserInfo.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getUser: function() {
var consumer = this.getParameter("sysparm_user");
var grUser = new GlideRecord('csm_consumer');
grUser.get(consumer);

// This is where dot-walking is done
// In this case we return the consumer's sys_id
return grUser.consumer;
},
type: 'getUserInfo'
});

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

There is not a field named 'consumer' on the csm_consumer table, so nothing is returned.  It's always best to return something from a GlideAjax to aid in troubleshooting (add an alert to the client script callback function to check the response/answer), and you should always use the 'get' shortcut syntax within in 'if' statement.  I think the field you want to return from the consumer table is named 'user', so your Script Include should look more like this:

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

    getUser: function() {
		var answer = 'false';
        var consumer = this.getParameter("sysparm_user");
        var grUser = new GlideRecord('csm_consumer');
        if (grUser.get(consumer)) {
	        // This is where dot-walking is done
    	    // In this case we return the User's sys_id
        	answer = grUser.getValue('user');
		}
		return answer;
    },

    type: 'getUserInfo'
});

 

View solution in original post

9 REPLIES 9

Brad Bowman
Kilo Patron
Kilo Patron

There is not a field named 'consumer' on the csm_consumer table, so nothing is returned.  It's always best to return something from a GlideAjax to aid in troubleshooting (add an alert to the client script callback function to check the response/answer), and you should always use the 'get' shortcut syntax within in 'if' statement.  I think the field you want to return from the consumer table is named 'user', so your Script Include should look more like this:

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

    getUser: function() {
		var answer = 'false';
        var consumer = this.getParameter("sysparm_user");
        var grUser = new GlideRecord('csm_consumer');
        if (grUser.get(consumer)) {
	        // This is where dot-walking is done
    	    // In this case we return the User's sys_id
        	answer = grUser.getValue('user');
		}
		return answer;
    },

    type: 'getUserInfo'
});

 

This worked perfectly, thank you for the additional explanation! I realize I was confusing fields in the interaction table vs consumer table - so thank you again! This all makes much more sense (I am a newer developer!).

You are welcome - happy to help!

 

 

Connect with me https://www.linkedin.com/in/brad-bowman-321b1567/

Ankur Bawiskar
Tera Patron
Tera Patron

@kristenmkar 

you can use onChange client script with getReference callback and use the below script

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading) {
		return;
	}

	if(newValue == ''){
		g_form.clearValue('opened_for');
	}
	var ref = g_form.getReference('consumer', callBackMethod);
}

function callBackMethod(ref){
	if(ref.user)
		g_form.setValue('opened_for', ref.user);
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader