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

This also makes sense, thank you!

@kristenmkar

Glad to help.

As per new community feature you can mark multiple responses as correct.

If my response helped please mark it correct as well so that it benefits future readers.

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

Runjay Patel
Giga Sage

Hi @kristenmkar ,

 

One issue in your script include, you are returning sys_id of consumer record but you need sys_id of user record which associated with consumer record. So the small change which you need to make in your script include that is return grUser.user;

 

RunjayPatel_0-1735188267643.png

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

 

Thank you so much for the explanation! Super helpful!

Hi @kristenmkar ,

 

Thanks for marking my solution helpful, now community enable multi acceptance feature so you accept multiple.