Auto Populate field on record producer when it has one record referenced

Luke James
Tera Contributor

Hello All, 

 

I am working on a requirement where on a record producer the requirement is to auto populate a field where there is only one record referenced within it. I have tried using a catalog script for this, but it is not working. 

 

Anyone have a script or a method where they have this working? Or knows how to do it? 

 

Here is my current code. 

Catalog client script

function onLoad() {
console.log('🔄 Checking for single matching sold_product...');

var ga = new GlideAjax('GetSingleReferenceOption');
ga.addParam('sysparm_name', 'GetSingleReferenceOption');
ga.addParam('sysparm_ref_field', 'sold_product'); // this param is optional for now
ga.getXMLAnswer(function(answer) {
console.log('📥 GlideAjax response for sold_product: ', answer);
if (answer) {
g_form.setValue('sold_product', answer);
console.log(' Auto-filled sold_product with: ' + answer);
} else {
console.log(' No single product found – leaving sold_product blank.');
}
});
}
 
Script include
var GetSingleReferenceOption = Class.create();
GetSingleReferenceOption.prototype = Object.extendsObject(AbstractAjaxProcessor, {

GetSingleReferenceOption: function() {
var gr = new GlideRecord('sn_install_base_sold_product');
gr.addQuery('active', true); // You can adjust this condition if needed
gr.query();

var count = gr.getRowCount();
if (count === 1 && gr.next()) {
return gr.getUniqueValue(); // sys_id
}

return ''; // More than one or none
}

});
 
Thanks in advance for your help. 
1 ACCEPTED SOLUTION

@Luke James 

are you testing with admin or non-admin?

if non-admin then ensure user has read access on table "sn_install_base_sold_product"

In my instance there are lot of records and I tested with setLimit(1) and it worked.

Output: It gave me 1 record for me correctly

AnkurBawiskar_5-1745581806982.png

 

 

AnkurBawiskar_4-1745581760839.png

 

 

AnkurBawiskar_1-1745581283966.png

 

AnkurBawiskar_3-1745581692714.png

 

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

8 REPLIES 8

@Luke James 

Correct in the default value field present on that variable configuration

I hope your variable is reference type and referring "sn_install_base_sold_product" table

AnkurBawiskar_0-1745577314136.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar 

 

Thanks again for your response. I have tried this, but it does not work unfortunately. The variable is referencing the "sn_install_base_sold_product" table as well. 

 

Did you try this yourself was it working? 

 

Kind Regards, 

 

Luke

@Luke James 

are you testing with admin or non-admin?

if non-admin then ensure user has read access on table "sn_install_base_sold_product"

In my instance there are lot of records and I tested with setLimit(1) and it worked.

Output: It gave me 1 record for me correctly

AnkurBawiskar_5-1745581806982.png

 

 

AnkurBawiskar_4-1745581760839.png

 

 

AnkurBawiskar_1-1745581283966.png

 

AnkurBawiskar_3-1745581692714.png

 

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Luke James 

I believe I have shared a working solution with output and also guided you to debug further.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader