How to autopopulate choice if there is single value in reference field

Samiksha2
Mega Sage

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

 

7 REPLIES 7

Mark Roethof
Tera Patron
Tera Patron

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

LinkedIn

Mark Roethof
Tera Patron
Tera Patron

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

LinkedIn

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

Yashsvi
Kilo Sage

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.