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

How to populate a Reference field automatically (no dot-walking, GlideAjax)

Alberto Consonn
ServiceNow Employee
ServiceNow Employee

Hi guys,

I need an help for configuring the Advanced Reference Qualifier in the right way.

In the Time Worker table, I've created a Reference custom field "Account_ref" to the customer_account table.

I would like to fill this field automatically with the account of the selected task, I tried dding with the following Advanced Reference Qualifier...no luck.

javascript:'account='+current.task;

Thanks for your help!!!

find_real_file.png

find_real_file.png

1 ACCEPTED SOLUTION

Dubz
Mega Sage

Hi Alberto,

I replicated your config and figured out the issue, real rookie error for which i accept full responsibility 🙂

The function call in the getXMLAnswer() line is in quotes, it shouldn't be! use this:

//client script
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
	}

	var ga = new GlideAjax('TestScriptInclude');
	ga.addParam('sysparm_name', 'testFunction');
	ga.addParam('sysparm_task', g_form.getValue('task'));
	ga.getXMLAnswer(getAjaxData);

	function getAjaxData(response){
		g_form.setValue('u_account_ref', response);
	}
}

 

EDIT: Just noticed Harsh has already picked this up! 

View solution in original post

36 REPLIES 36

Yeah, you can't dot walk in client scripts. I would advise against using getReference(), it returns the entire record to the client when you only need 1 field from it. Glide Ajax it considered best practice, you call a server side script include and do all the processing there. Happy to help with config but the guide below is really good and it's really important to know how to configure this so i'd recommend having a look. 

https://community.servicenow.com/community?id=community_blog&sys_id=f8ccee25dbd0dbc01dcaf3231f961978

Alberto Consonn
ServiceNow Employee
ServiceNow Employee

Thanks, I'm going to check it in deep, it's a little bit complex at the beginning, so I would like to make the things working as soon as possible first 🙂

May I ask you to support me using getReference()?

Thanks a lot

Alberto

Not sure what's wrong, it looks like the script is written properly. What field are you running this on the change of? It might be worth running it onLoad and also try returning the value of a string field instead.

One important thing to note is that the account field on the task form is itself a reference field. Even if you get your getReference working, it will only be returning a sys_id. If you want to get the name/display value you have to go into the customer_account table and getReference only allows you to go into the referenced table. To go two tables deep you'll need Glide Ajax, something like this:

//client script
var ga = new GlideAjax('ScriptIncludeName');
ga.addParam('sysparm_name', 'functionName');
ga.addParam('sysparm_task', g_form.getValue('task');
ga.getXMLAnswer('getAjaxData');

function getAjaxData(response){
g_form.setValue('u_account_ref', response);
}

//script include
functionName: function(){
var tsk = this.getParameter('sysparm_task');
var gr = new GlideRecord('sn_customerservice_case');
if(gr.get(tsk)){
return gr.getDisplayValue('account');
}
}

Alberto Consonn
ServiceNow Employee
ServiceNow Employee

Thanks David, I'm going to use GlideAjax then, still something not correct, I tried to use your example, please help:

// client script
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
	}
	//Type appropriate comment here, and begin script below
	var ga = new GlideAjax('test');
	ga.addParam('sysparm_name', 'test_function');
	ga.addParam('sysparm_task', g_form.getValue('task'));
	ga.getXMLAnswer('getAjaxData');

	function getAjaxData(response){
		g_form.setValue('u_account_ref', response);
	}
}

//script include
function test_function(){
var tsk = this.getParameter('sysparm_task');
var gr = new GlideRecord('sn_customerservice_case');
if(gr.get(tsk)){
return gr.getDisplayValue('account');
}
}

When you're writing the script include, open a new record and enter the name and it will populate the script field with some syntax, tick the client callable checkbox and it will change that syntax to the ajax prototype. end product should look like this:

var test = Class.create();
test.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	test_function: function(){
		var tsk = this.getParameter('sysparm_task');
		var gr = new GlideRecord('sn_customerservice_case');
		if(gr.get(tsk)){
			return gr.getDisplayValue('account');
		}
	},
	
	type: 'test'
});