State should display based on country selected in Record producer form

VikramM1
Tera Contributor

we have two variables in record producer one is country reference to country table and state referencing to state table. In country table we have list of countries and in state table we have country field referring to country table and state field having states based on country. In record producer once we select country, state should show in list based on country name. How to achieve this in service now

1 ACCEPTED SOLUTION

@VikramM1 Try the following, just change the table names according to yours.

 

Reference Qualifier in Catalog item:

kkrushkov_0-1711622401179.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'
};

I hope this answers your question! 🙂

If it does, kindly mark it as answered.

 

 

View solution in original post

12 REPLIES 12

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @VikramM1 

 

Did you try with Auto populate feature in catalog item.

 

AGLearnNGrow_0-1711620132926.png

 

 

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

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/atul_grover_lng [ Connect for 1-1 Session]

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

Dependent question taken same country field in state field also

 

kkrushkov
Mega Sage

Hello, @VikramM1 

If you want to filter states based on selected country name, I believe you should use the advanced reference qualifier.

Yes can you please provide code to add in record producer advanced reference qualifier.