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

Shane J
Tera Guru

Maybe your issue at this point is the Variable you're trying to populate.  Why don't you add an alert to show what value you're getting back (the one using to setValue), or add a basic String variable and populate that instead of the one you are trying to fill.

Sorry for the delay Shane, are you suggesting i change the variable from reference to a text/string field?

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.

Thanks Jim, i spent a lot of hours trying to figure this one out, i really appreciate your assistance.

You are welcome.