The CreatorCon Call for Content is officially open! Get started here.

Using GlideAjax to get records from the reference table of a reference table?

John S
Kilo Contributor

Hey everyone. Still fairly a noob in ServiceNow. Just when I finally think I'm getting the hang of GlideAjax, I run into another problem! Basically, what I'm trying to do is get the in stock status for my catalog items. I now understand how I can get the values to any tables that immediately related to catalog items, but I'm having issue with connecting anything past that. So, I understand it would basically be catalog items to product model, which then connects to consumables. 

Script Include:

function Item_Inventoried( cat_item_sys_id )
{
	var inventory = {
		is_inventoried: false,
		quantity: '0'
	};
	
	var message = 'Inventoried';
	
	var cat_item = new GlideRecord('pc_product_cat_item');
	if( cat_item.get(cat_item_sys_id) ) {
		
		message = ', Item: "' + cat_item.name + '"';
		
		if( cat_item.model ) {
			
			var cons = new GlideRecord('alm_consumable');
			cons.addQuery( 'model', cat_item.model );
			cons.query();
			while( cons.next() ) {
	
				inventory.is_inventoried = true;

				message += ', consumable: "' + cons.display_name + '", state: "' + cons.install_status.getDisplayValue() + '", quantity: ' + cons.quantity;
				
			
				if( cons.install_status == 6) {
					
					inventory.quantity = cons.quantity;
					
					message += ', in stock';
				} else {
					
					message += ', no stock';
				}
			} 
			
		} else {
			
			message += ', NO MODEL for Item';
		}
	} else {
		
		message += ', NO Cat Item found for sys_id: ' + cat_item_sys_id;
	}
	
	return inventory;
}

Client Script:

function onLoad() {
  var getData = new GlideAjax('Inv_Stock');
getData.addParam('sysparm_name', 'Item_Inventoried');
getData.addParam('sysparm_cat_id', g_form.getUniqueValue());
getData.getXML(showRep);

function showRep(response) {
	var answer = response.responseXML.documentElement.getAttribute("inventory");
	//produce an alert for testing.
	var results = JSON.parse(answer);
	alert(results);
}
}

Thank you.

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

I'm not following your example exactly - either our catalog structure is setup differently, or you're missing or mis-using tables in the script include.  In any event, here are some things that may help.

Add some gs.info or gs.addInfoMessage lines at each step in the script include.  This will show you the results of the query / if condition / variable vs. what you are expecting.

Another thing to keep in mind is that you can dot-walk when the field you're going after is a reference field.  To use a different scenario, when a User is selected on a request form and you want to get the user's city that is on the location record.  Pass the sys_id into a script include, then on the server script query for that record on sys_user.  Instead of using the location field on the user record result to query the location table and lookup the city for that location, you can just use something like

var city = usr.location.city; // where usr = GlideRecord

Note that since workflow scripts are server scripts, this also works in workflows - even easier

current.variables.city = current.variables.user.location.city;

View solution in original post

1 REPLY 1

Brad Bowman
Kilo Patron
Kilo Patron

I'm not following your example exactly - either our catalog structure is setup differently, or you're missing or mis-using tables in the script include.  In any event, here are some things that may help.

Add some gs.info or gs.addInfoMessage lines at each step in the script include.  This will show you the results of the query / if condition / variable vs. what you are expecting.

Another thing to keep in mind is that you can dot-walk when the field you're going after is a reference field.  To use a different scenario, when a User is selected on a request form and you want to get the user's city that is on the location record.  Pass the sys_id into a script include, then on the server script query for that record on sys_user.  Instead of using the location field on the user record result to query the location table and lookup the city for that location, you can just use something like

var city = usr.location.city; // where usr = GlideRecord

Note that since workflow scripts are server scripts, this also works in workflows - even easier

current.variables.city = current.variables.user.location.city;