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

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.getXML('getAjaxData');

function getAjaxData(response) {
  var answer = response.responseXML.documentElement.getAttribute("answer");
  alert(answer); 
g_form.setValue('u_account_ref', response);	
}
}

 

var test = Class.create();
test.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

test_function: function(){
var tsk = this.getParameter('sysparm_task');
gs.log('what is the value for task'+tsk);
var gr = new GlideRecord('sn_customerservice_case');
if(gr.get(tsk)){
gs.log('Value is'+gr.getDisplayValue('account'));
return gr.getDisplayValue('account');
}
},

type: 'test'
});

 

can you try now and let us know what are you getting in logs and alert

I didn't get any alert in the form...this is the log Information:

find_real_file.png

do one thing, store gr.getDisplayValue('account'); in one variable and then return it. 

 

also pass the normal alert inside the ajax function in client script

var test = Class.create();
test.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

test_function: function(){
var res;
var tsk = this.getParameter('sysparm_task');
gs.log('what is the value for task'+tsk);
var gr = new GlideRecord('sn_customerservice_case');
if(gr.get(tsk)){
gs.log('Value is'+gr.getDisplayValue('account'));
res=gr.getDisplayValue('account');
}
gs.log(' function returning '+ res);
return res;
},

type: 'test'
});

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.getXML(getAjaxData);

function getAjaxData(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
g_form.setValue('u_account_ref', response);
}
}

 

i got it .. we were passing the single quote.