Need help getting data from location table to Catalog item

thomast63
Mega Contributor

We are looking for some help with the Service Catalog and routing Catalog Tasks to groups.

I have Fields that were put on the Location table and trying to pull them into the Catalog Items.

We have been told we can only dotwalk 1 level on getting these Fields

 

“Since these 2 fields exist in the underlying table and not in user, using g_form.getReference you cannot access them.

The temporary and quick fix is to create 2 additional fields in user table and store this data for all users in those fields as well. Then you can access those fields like any other fields using getReference.”

 

I can pull them in with javascript but if user ID is changed then the update script will not work.   Wondering if you have any ideas for how to handle this situation?

23 REPLIES 23

Mark Roethof
Tera Patron
Tera Patron

Hi there,

You can use GlideAjax for this. See this article I published recently, contains detailed examples and explanation.
Client Side Scripting: Go for GlideAjax (with getXMLAnswer)!

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

john_duchock
Kilo Guru

I avoid using getReference as much as possible as it puts processing stress on the client.  You can achieve this by creating a script include / client script to get all the data you need.

An example of how we pull lots of user data with a script include (including custom fields)

Script Include:  GetUserDetailsAjax

Client Callable:  TRUE

var GetUserDetailsAjax = Class.create();
GetUserDetailsAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	getInfo: function(){

		var obj={};
		obj.department='';
		obj.email='';
		obj.phone='';
		obj.manager = '';
		obj.title = '';
		obj.u_store = '';
		obj.location = '';
		obj.company = '';
		obj.employee_number = '';
		var id=this.getParameter('sysparm_user_id');
		var gr= new GlideRecord('sys_user');
		if(gr.get(id)){

			obj.department = gr.department.name.toString();
			obj.phone = gr.phone.toString();
			obj.location = gr.location.name.toString();
			obj.company = gr.company.name.toString();
			obj.email=gr.email.toString();
			obj.manager = gr.manager.name.toString();
			obj.title = gr.title.toString();
			obj.u_store = gr.u_store.toString();
			obj.employee_number = gr.employee_number.toString();

		}

		return JSON.stringify(obj);

	},

	type: 'GetUserDetailsAjax'
});

 

This script include pulls several values from SYS_USER (and more can be added if needed).  I then use my client script to only pull the values i need at the time.  This client script only pulls a few of the values:

 

function onChange(control, oldValue, newValue, isLoading) {

	if (newValue == '') {

		return;

	}

	var id = g_form.getValue('requested_by');
	var ga = new GlideAjax('GetUserDetailsAjax');
	ga.addParam('sysparm_name','getInfo');
	ga.addParam('sysparm_user_id',id);
	ga.getXML(CallBack);

	function CallBack(response)
	{
		var answer = response.responseXML.documentElement.getAttribute("answer");
		var user=JSON.parse(answer);


		//g_form.setValue('email',user.email);
		g_form.setValue('requested_by_email',user.email);
		g_form.setValue('requested_by_phone',user.phone);
		g_form.setValue('requested_by_manager',user.manager);
		g_form.setValue('u_store',user.u_store);
		g_form.setValue('requested_by_department',user.department);


		g_form.setReadOnly('requested_by_email',true);


		// 		var ajax = new GlideAjax('MyDateTimeAjax'); 
		// ajax.addParam('sysparm_name', 'nowDateTime'); 
		// ajax.getXML(function () 
		// { 
		// g_form.setValue('requested_date', ajax.getAnswer()); 
		// }); 



	}

}

 

Now, having said all that, you could also use a REST API to call data, but that is not always optimal (and can be complicated). 

 

Hope this helps !

how would I use this to pull from Location table?

john_duchock
Kilo Guru

sorry, maybe i wasn't clear. I will have to make some assumptions as the information you provided did not have some needed clarification:


ASSUMPTIONS:

I will assume you are trying to pull "Catalog Default", "Phone Default" and "PC Catalog" from CMN_LOCATION by your screenshot.

I will also assume that on the Locations table, the following fields exist:

"Phone Default" = cmn_location.phone
"Catalog Default" = cmn_location.u_catalog_default
"PC Catalog" = cmn_location.u_pc_catalog

find_real_file.png

I will further assume that your catalog item has the following variables:

Location: Reference cmn_location table
Phone: Single Line Text
PC Catalog: Single Line Text
Default Catalog: Single Line Text

find_real_file.png

 

(Your Specific CODE):

Create a SCRIPT INCLUDE (with "Client Callable" checkbox set to TRUE).  Put this in the script box

var GetLocationDetailsAjax = Class.create();
GetLocationDetailsAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	getInfo: function(){

		var obj={};
		obj.phone='';
		obj.u_catalog_default='';
		obj.u_pc_catalog='';
		var id=this.getParameter('sysparm_location');
		var gr= new GlideRecord('cmn_location');
		if(gr.get(id)){

			obj.phone = gr.phone.toString();
			obj.u_catalog_default = gr.u_catalog_default.toString();
			obj.u_pc_catalog = gr.u_pc_catalog.toString();
			
		}

		return JSON.stringify(obj);

	},

	type: 'GetLocationDetailsAjax'
});

 find_real_file.png

 

Next, create a client script on your catalog item:

  • UI Type:  ALL
  • TYPE:  onChnage
  • Variable Name:  Location
  • Script:
function onChange(control, oldValue, newValue, isLoading) {

	if (newValue == '') {

		return;

	}

	var id = g_form.getValue('location'); 
	var ga = new GlideAjax('GetLocationDetailsAjax');
	ga.addParam('sysparm_name','getInfo');
	ga.addParam('sysparm_location',id);
	ga.getXML(CallBack);

	function CallBack(response)
	{
		var answer = response.responseXML.documentElement.getAttribute("answer");
		var location=JSON.parse(answer);
		
		g_form.setValue('phone',location.phone);
		g_form.setValue('pc_catalog',location.u_pc_catalog);
		g_form.setValue('default_catalog',location.u_catalog_default);
		
		g_form.setReadOnly('phone',true);
		g_form.setReadOnly('pc_catalog',true);
		g_form.setReadOnly('default_catalog',true);

	}

}

 

The result is that when a user selects a location, the values update.  Please note that i included readOnly lines as typically, you want to keep users from editing pre-populated fields

find_real_file.png