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.

GlideAjax returning values for referenced fields

Mark118
Tera Contributor

Hello, 

I am trying to get a GlideAjax query to work based on some reference fields that are on the sys_user table. I have a basic catalog item which has a requested for field and based on that I want to have the company field populate and then expand this out. 

The Company field is a reference field on sys_user

Client Script

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    //Get value of the requested_for
    var requestor = g_form.getValue('requested_for');
    //Debugging
	alert(requestor);
	
	// Call the GlideAjax function	
    var ga = new GlideAjax('commonUtils');
	
	//Name of the function in the GlideAjax script
    ga.addParam('sysparm_name', "getUserInfo");	
	ga.addParam('sysparm_userId', requestor); // Parameter to pass the client side value to Script include
    ga.addParam('sysparm_user', newValue); //Onchange field is reference field to sys_user table
	
	ga.getXMLAnswer(function(answer){
		var response = JSON.parse(answer);
		
alert(answer);
		g_form.setValue('company', response.company); //used for reference field to department table		
		g_form.setValue('email', response.email);
		g_form.setValue('details', response.company + '\n' + response.email + '\n' + response.street );
		
    });
}

 

Script Include

var commonUtils = Class.create();
commonUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
   
	getUserInfo : function(){
		var userInfo = this.getParameter('sysparm_user');
		var userCompany = this.getParameter('sysparm_userId');
		
		var grUser = new GlideRecord('sys_user');
		var grComp = new GlideRecord('core_company');
		grUser.get(userInfo);
		grComp.get(userInfo);
		

		
		var response = {
			"email" : grUser.getValue('email'),
			"company" : grUser.getDisplayValue('company'),	
			
		};
		
		return JSON.stringify(response);
	},
	
	
	type: 'commonUtils'
});

 

I can retrieve the values and output them into a single line text or multiLine text field but am unable to populate any reference field 

find_real_file.png

find_real_file.png

hope someone can help me out 

1 ACCEPTED SOLUTION

Dan H
Tera Guru

Thats because reference fields are expecting to be passed a sys id not display value.

try:

var response = {
			"email" : grUser.getValue('email').toString(),
			"company" : grUser.getValue('company').toString()	
			
		};

 

Now because you're passing back sys ids, your details will look just include sys ids.

so you could also pass back the display names.

var response = {
  email: grUser.getValue('email').toString(),
  company: grUser.getValue('company').toString(),
  emailDV: grUser.getDisplayValue('email').toString(),
  companyDV: grUser.getDisplayValue('company').toString()
};

And then change this line in the client script to:

g_form.setValue('details', response.companyDV + '\n' + response.emailDV + '\n' + response.street );

Hope this helps.

 

Please mark my answer as Correct/Helpful based on impact

Regards,

Dan H

View solution in original post

5 REPLIES 5

Dan H
Tera Guru

Thats because reference fields are expecting to be passed a sys id not display value.

try:

var response = {
			"email" : grUser.getValue('email').toString(),
			"company" : grUser.getValue('company').toString()	
			
		};

 

Now because you're passing back sys ids, your details will look just include sys ids.

so you could also pass back the display names.

var response = {
  email: grUser.getValue('email').toString(),
  company: grUser.getValue('company').toString(),
  emailDV: grUser.getDisplayValue('email').toString(),
  companyDV: grUser.getDisplayValue('company').toString()
};

And then change this line in the client script to:

g_form.setValue('details', response.companyDV + '\n' + response.emailDV + '\n' + response.street );

Hope this helps.

 

Please mark my answer as Correct/Helpful based on impact

Regards,

Dan H

Nikita30
Mega Sage
Hi Mark, Please replace the below code. “company" : grUser.getValue('company'), Let me know if it works Thanks Nikita

Mark118
Tera Contributor

Thanks for the quick replies 

I tried the following, but it seems as though the value is going into the reference field as it's indicated by the preview record 

find_real_file.png

 

however this is blank and when you try and preview the record it comes back with 

find_real_file.png

What is the alert saying for company now when you load the form?

Also its probably not working because you're sending a company sys id existing in table [core_company] to the company field that you said is referencing [sys_user]. Can you change the reference to the company table. That way a record will be found.

 

Hope this helps.

 

Please mark my answer as Correct/Helpful based on impact

Regards,

Dan H