How to autopopulate choice if there is single value in reference field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2024 11:52 PM
Hi All,
I have created two fields,
Account- Reference
Platform- Reference
Based on Account Platform is giving user to select the record.
But if there is a single record in the Platform then it should be autopopulate.
I tried the below client script on change of Account
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var custReq = g_form.getElement('u_platform');
alert(custReq);
if (custReq.options.length == 1) {
g_form.setValue('u_platform', custReq.options[0].value);
}
}
But it is throwing error "onChange script error: TypeError: Cannot read properties of undefined (reading 'length') function () { [native code] } "
Please help in this.
Thanks,
Sam
- Labels:
-
Change Management

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2024 11:53 PM
Hi there,
Check question first:
Is this in the Core UI or on a Portal? If portal, there is the Auto-populate function.
Let me know, based on that I can answer your question.
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2024 11:55 PM
Since you did also mention that u_platform is a reference, do look at this article that I wrote 4 years ago.
- 2020-10-19 - Article - setValue() reference fields/variables, use the value AND displayValue parameter
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2024 12:01 AM
Hi @Mark Roethof ,
Thanks for your reply.
Both fields are in one custom table. And i am writing client script for Core UI.
Thanks,
Sam
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2024 11:57 PM
Hi @Samiksha2,
please check below script :
Create a Script Include:
var CheckPlatformRecords = Class.create();
CheckPlatformRecords.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getPlatformRecordsCount: function(accountId) {
if (!accountId) {
return 0;
}
var platformGR = new GlideRecord('u_platform_table'); // Replace with your Platform table name
platformGR.addQuery('u_account', accountId); // Replace with the correct reference field name
platformGR.query();
var count = 0;
var platformId = '';
while (platformGR.next()) {
count++;
if (count == 1) {
platformId = platformGR.sys_id.toString();
}
}
return count == 1 ? platformId : count;
}
});
Create a Client Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var accountId = newValue;
var ga = new GlideAjax('CheckPlatformRecords');
ga.addParam('sysparm_name', 'getPlatformRecordsCount');
ga.addParam('accountId', accountId);
ga.getXMLAnswer(function(response) {
var result = response.responseXML.documentElement.getAttribute('answer');
if (result && !isNaN(result)) {
// Multiple or zero records found, do nothing
return;
} else if (result) {
// Only one record found, set it
g_form.setValue('u_platform', result);
}
});
}
Thank you, please make helpful if you accept the solution.