Reference fields dependency

ShivamS44129005
Tera Contributor

How to set one dependent field on another. Like we have 'class' field and another is 'sub-class' but subclass field should be dependent on the class field. These two fields are reference type and also we have tried the option of the dependency from the field dictionary but it is not working.

6 REPLIES 6

Runjay Patel
Giga Sage

Hi @ShivamS44129005 ,

 

In reference qualifier of sub-class dictionary put below code.

javascript: "u_class=" + current.u_class;

 

Assuming u_class field is present on sub-class reference table.

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

Rajesh Chopade1
Mega Sage

Hi @ShivamS44129005 

Create a Client Script (onChange) for the class field to dynamically filter the available sub-class options.

function onChange(control, oldValue, newValue, isLoading) {
    // Exit if the form is loading
    if (isLoading) return;

    // Clear the current options for the `sub-class` field
    g_form.clearOptions('sub_class');
    
    // If a `class` value is selected, filter `sub-class` options based on the class
    if (newValue) {
        var gr = new GlideRecord('your_class_to_subclass_table'); // A table that links 'class' to 'sub-class'
        gr.addQuery('class', newValue); // Query based on the selected class
        gr.query();
        
        // Loop through the records and add options to the `sub-class` field
        while (gr.next()) {
            g_form.addOption('sub_class', gr.sub_class, gr.sub_class.name); // Add the subclass option
        }
    }
}

I hope my answer helps you to resolve your issue, if yes please mark my answer helpful and correct.

thank you

Rajesh

Ankur Bawiskar
Tera Patron
Tera Patron

@ShivamS44129005 

please share some details

1) both the field refer to which table

2) what's your exact requirement to filter

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