We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

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

@Yashsvi Don't forget to apply best practices and with that the second parameter on reference fields/variables!

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

@Mark Roethof

yes, will remember this from now on.👍 

Thank you.

Hi @Yashsvi ,

I tried it is not working.

 

Thanks,
Sam