How to set dependent filter in reference qualifier for state field.

dusmantanayak
Mega Contributor

Hi,

How I can set filter condition to populate State(cmn_location) field value based on the country(core_country)field selection?

dusmantanayak_0-1768311704506.png

Regards,

Dusmanta Nayak

11 REPLIES 11

Dr Atul G- LNG
Tera Patron

Hi @dusmantanayak 

If you are trying this on the variable side, you can make the dependent field state based on the country.

 

https://www.servicenow.com/community/developer-forum/state-should-display-based-on-country-selected-...

 

Reference Qualifier in Catalog item:

DrAtulGLNG_0-1768312081685.png

 

 

Script Include: 

var FilterStates = Class.create();
FilterStates.prototype = {

    getStatesByCountry: function(country) {
        //You check if the country is empty. If it is you don't include it in the reference qualifier and return.
        if (!country) return '';
        var stateList = [];

        var stateGr = new GlideRecord('your_state_table');
        stateGr.addQuery('your_country_field', country);
        stateGr.query();
        while (stateGr.next()) {
            stateList.push(stateGr.getUniqueValue());
        }

        return "sys_idIN" + stateList.join(',');
    },

    type: 'FilterStates'
};
*************************************************************************************************************
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/dratulgrover [ Connect for 1-1 Session]

****************************************************************************************************************

Hi @Dr Atul G- LNG ,

Script include is not working.

Can you please help and suggest that do i need to modify the script include?

Note: Both the fields type is "Lookup Selectbox".

Screenshot:

dusmantanayak_0-1768313614329.png

 

Reference Qualifier of State field:

dusmantanayak_1-1768313756350.png

 

 

Script Include:

var FilterStates = Class.create();
FilterStates.prototype = {

    getStatesByCountry: function(country) {
        //You check if the country is empty. If it is you don't include it in the reference qualifier and return.
        if (!country) return '';
        var stateList = [];

        var stateGr = new GlideRecord('cmn_location');
        stateGr.addQuery('state', country);
        stateGr.query();
        while (stateGr.next()) {
            stateList.push(stateGr.getUniqueValue());
        }

        return "sys_idIN" + stateList.join(',');
    },

    type: 'FilterStates'
};
 
Regards,
Dusmanta
 

@dusmantanayak 

In your reference qualifier check below two probable mistakes :

  1. Word "new" is written twice
  2. location spelling is incorrect. written as "loction". Check the variable configuration.

Accept the solution and mark as helpful if it does, to benefit future readers.
Regards,
Sumanth

 

SIVASANKARIS
Tera Guru

 

Hi @dusmantanayak 

 

  • Right-click the State/Province field and select Configure Dictionary.
  • Go to the Dependent Field tab.

  • Check the Use dependent field box.

  • In the Dependent on field dropdown, select Country (country).

  • Save the record.

If this is useful please mark it as helpful and accept my solution....