How to get a reference field value from a field on a service portal form

mandie
Kilo Contributor

Hi all, 

i am trying to get the value "employee_number" from a user field on the form on our self service portal. I will explain better: 

I have a field which asks "who are you logging this on behalf of" and I want to get the employee number of the person who this is being logged on behalf of and then manipulate further questions based on this. I.e., if the employee number shows the person is a student, ask student question and if staff, ask staff questions. 

I have managed to use a glideajax to get the value of the current person logged in to manipulate the questions if the incident is being logged on behalf of the person logging it themselves. Now all I need is to be able to get the employee_number value of the person who it is being logged on behalf of IF the current user is not the one who needs the job done. 

I have tried using a g_form.getReference(field_name, "employee_number"), but this does not work. I read somewhere the getReference() function does not work on the service portal? 

Can anyone help? 

Thanks!!

Amanda 

4 REPLIES 4

palmen
Tera Guru

I'm using an onChange Catalog Client Script.
When the value changes for the field, you can get this value by using newValue in the Client Script.

Pass this value in a glide ajax call to a script include where you make the lookup what the employee number this user has.
Return this value and build the logic based on this.

Inactive_Us2276
Kilo Contributor

Hi. Amanda.

Please see this link.

getReferenceAdvanced Client Script Function, a GlideAjax Alternative

let me know if it worked for you !!

mandie
Kilo Contributor

Hi all, thank you for your suggestions. I tried the getReferenceAdvanced function but it completely broke the whole script.

With the glide Ajax, this might sound like a silly question, but how do you do a glideRecord query using a field on the form? what is the object? I know with checking that the current user was a staff/student, I did a glideRecord query with the object "current". What do I use if I am picking up values from a field in the form? 

Thanks in advance,

Amanda 

I'm using a catalog client script onChange, so when the variable changes the script will trigger and you can get the new value by using newValue

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	//AJAX call to lookup Requested for information
	var ga = new GlideAjax('demoUtils'); //Name of Script Include
	ga.addParam('sysparm_name','requestedForValues'); //Function in Script Include
	ga.addParam('sysparm_requestedFor', newValue); //Parameter to pass to function
	ga.getXML(setData);
}

 

This is what I use in the Script Include

requestedForValues: function() {
	var rf = this.getParameter('sysparm_requestedFor');
	//Get the User Record for Requested For
	var gr = new GlideRecord('sys_user');
	gr.get(rf);

Now you have the user record and can return whatever info from the user you need back to the client script and set those values.

More information about this can be found in the API
https://developer.servicenow.com/app.do#!/api_doc?v=jakarta&id=c_GlideAjaxAPI