Set values according to option checked

I’m trying to customize the ERPNext, but I’m experiencing some problems, I would like your help.
Suppose we have three fields, namely: Country, State and City.

I wanted to do the following: when I select a country, States that country will appear in the list and when I choose the state, will appear Cities that State.

I have not the slightest idea how to do this, thank you for your help. If you are missing some information, just let me know.

Maintain three masters City, State and Country.

Note


State should have link field for Country
City should have link field for State

Then add link fields for Country State and City on form and filter state and city by writting get_query trigger on link fields.

eg:

cur_frm.fields_dict['state'].get_query = function(doc, dt, dn) {
    return {
        filters:{"country": doc.country}
    }
}
2 Likes

Many thanks for the reply !

Unfortunately, I could not do, that would be the complete script?
I will explain what I did:
1 - I created 4 DocType: “Country”, “State”, “City” and “Test”;
2 - In the DocType “Test”, I created three fields (using link to their respective DocType): “Country”, “State” and “City”;
3 - Added his script for the DocType “Test”.

I apologize, unfortunately I have not much knowledge in this area but I find it interesting and I’m learning slowly …

I found an example of the way I wanted to do:
https://jsfiddle.net/gleisson03/15xuh1cu/1/

Design master as follow.

  1. State doctype should have country as link field.
  2. City doctype should have state as link field.
  3. Then on test form put all three fields, Country , State and City.
  4. On test.js add script
cur_frm.fields_dict['state'].get_query = function(doc, dt, dn) {
    return {
        filters:{"country": doc.country}
    }
}

cur_frm.fields_dict['city'].get_query = function(doc, dt, dn) {
    return {
        filters:{"country": doc.state}
    }
}

This script will filters States as per selected Country and Cities as per selected State.

Hope this will helps.

1 Like

Working perfectly, just had to make a change in the last line filter (country to state):

cur_frm.fields_dict[‘state’].get_query = function(doc, dt, dn) {
       return {
               filters:{“country”: doc.country}
       }
}

cur_frm.fields_dict[‘city’].get_query = function(doc, dt, dn) {
       return {
               filters:{“state”: doc.state}
       }
}

Thank you for the support and patience in teaching me!

1 Like