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

GopikaP
Mega Sage

Hi @Luke James , I tried the same lines of code with just table change in my PDI,it works fine. The value is auto populating as expected. 

Are you sure the variable name that you are trying to populate is 'sold_product'

Also are there 'active' true records in the table - 'sn_install_base_sold_product'

Also, here - which one is getting printed - if block or else block?

console.log(':inbox_tray: GlideAjax response for sold_product: ', answer);
if (answer) {
g_form.setValue('sold_product', answer);
console.log(':white_heavy_check_mark: Auto-filled sold_product with: ' + answer);
} else {
console.log(':cross_mark: No single product found – leaving sold_product blank.');
}

 

Hi @GopikaP , 

 

Thanks for your response. I checked the logs and i'm getting this response 

🔄 Checking for single matching sold_product...
VM112322:10 📥 GlideAjax response for sold_product: null
VM112322:15 No single product found – leaving sold_product blank.

 

Screenshot 2025-04-25 at 11.18.45.png

Just to show you as well, there is only one sold product available in this list for the account and contact that is populated. But it is not automatically populating, any ideas? 

Ankur Bawiskar
Tera Patron
Tera Patron

@Luke James 

Update as this in default value of that variable and it will work and no client script, script include required

there is no active field on that table "sn_install_base_sold_product"

I believe you want State=Active

AnkurBawiskar_0-1745574636375.png

 

javascript: var recordSysId = '';
var gr = new GlideRecord('sn_install_base_sold_product');
gr.addEncodedQuery('state=active');
gr.query();
var count = gr.getRowCount();
if (count == 1 && gr.next()) {
    recordSysId = gr.getUniqueValue();
}
recordSysId;

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

Hi @Ankur Bawiskar , 

 

Thanks very much for your response. I did try this, but it did not work. Did you mean paste your script into the default value of the field? 

 

Kind Regards, 

 

Luke