Auto populate a reference field from a look up select box field.

othomas1
Kilo Guru

Hello community,

I have entered an issue where i am trying to populate a reference field with info from a lookup select box. There reference field variable is sftw_model and the lookup select box variable is sw_use. I read where if i changed the lookup value on the lookup select box to sys id that it would work, but im still not having any luck. Any tips would be helpful.

find_real_file.png

find_real_file.png

find_real_file.png

1 ACCEPTED SOLUTION

Jim Coyne
Kilo Patron

So if I'm understanding this properly, you are selecting a License from a Lookup Select Box and then you want to auto-populate a Reference field that points back to the Product Model table, correct?

First thing - you should use a GlideAjax call whenever your end goal is populating a Reference field.  This is because you will want to use "setValue" with the display value as the third parameter, avoiding a trip back to the server for the display value.

Second - "getReference" will not work on a Lookup Select Box!  It works on "Reference" fields.

Here's how you would go about it. The Client Script would look something like this:

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	
	var ga = new GlideAjax("UCustomAjaxUtils");
	ga.addParam ("sysparm_name", "getLicenseModelInfo");
	ga.addParam("sysparm_license_id", newValue);
	ga.getXMLAnswer(populateModel);

	function populateModel(answer){
		var response = JSON.parse(answer);  //convert the returned JSON string to an object
		console.log(response);  //just so you can see the data that is returned in the browser's console, probably want to comment it out after
		g_form.setValue("sftw_model", response.modelId, response.modelName);  //call setValue with both the sys_id and display value
	}
}

 

And the Script Include would look like this:

Name: UCustomAjaxUtils
Client callable: checked
Script:

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

	getLicenseModelInfo: function() {
		var dataToReturn = {};
		dataToReturn.modelId = "";
		dataToReturn.modelName = "";
		var sysId = this.getParameter("sysparm_license_id");
		if (sysId) {
			var gr = new GlideRecord("alm_license");
			if (gr.get(sysId)) {
				dataToReturn.modelId = gr.getValue("model");
				dataToReturn.modelName = gr.model.getDisplayValue();
			}
		}
		return JSON.stringify(dataToReturn);		
	},


	type: "UCustomAjaxUtils"
});

It's a little odd because both fields end up with the same display (because you had "model" as the Lookup label field):

find_real_file.png

But, I think it does what you are looking for.

View solution in original post

20 REPLIES 20

Hello Mike,

 

I changed the code to reflect the changes, but still no luck.

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var sModel = g_form.getReference('sw_use', callBack); // doAlert is our callback function

function callback(sModel) { //reference is passed into callback as first argument

g_form.setValue('sftw_model',sModel.model);

Community Alums
Not applicable
It should be callBack not callback ( or the other way around)

try below

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	
	var sModel = g_form.getReference('sw_use', callBack); // doAlert is our callback function
	
	function callBack(sModel) {
		g_form.setValue('sftw_model', sModel.model);
	}
}

I used the code below, didnt work for me. thanks for your help though:

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var sModel = g_form.getReference('sw_use', callBack); // doAlert is our callback function

function callBack(sModel) { //reference is passed into callback as first argument

g_form.setValue('sftw_model', sModel.model);

}
}

right before g_form.setValue('sftw_model', sModel.model); add 

alert(sModel.model);