- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-27-2018 07:06 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-27-2018 09:28 PM
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):
But, I think it does what you are looking for.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-27-2018 07:14 AM
I took one of mine and altered it for your fields (as far as I can tell). See if this works.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var getloc = g_form.getReference('sw_use', function(gl) {
g_form.setValue('sftw_model', gl.model);
});
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-27-2018 07:42 AM
Good Morning Shane!
Unfortunately that did not work for me.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-27-2018 07:17 AM
On your script line 8
it's need to be sModel.model

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-27-2018 07:18 AM