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

Community Alums
Not applicable
Try to return in your callback the field you need from the ref record and as it will be held in your variable, use g_form.setValue(). For test only - dont use callback so the entire object is there. And just set the field to it. If it works do the above

Hello Joro,

 

I tried the below code, with no luck.

 

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

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

g_form.setValue();

}
}

Community Alums
Not applicable

Try it this way for a test: var sModel = g_form.getReference('sw_use'); g_form.setValue('swft_model', sModel);  happens?

 

Check the fields as i am writing from my cell 🙂

Community Alums
Not applicable
Check the field names pls....posting from my cell 🙂

Thanks for all of your help, i tried the below code with no luck unfortunately.

 

var sModel = g_form.getReference('sw_use'); // doAlert is our callback function

g_form.setValue('sftw_model', sModel);