ERPNext Conference 2019* ERPNext.com Blog

Fill a dropdown with cities depend on country select

customization
custom_script
erpnext

#1

I have a dropdown with countries and I need fill another dropdown with the cities of the country selected, but I really have no idea how to make the script, im very new in ERPNext.

This is my exact issue


#2

Can you explain your issues more elaborate.


#3

I just edited my post, thanks for ur attention <3


#4

The resource you’re looking for and an explanation:

  1. You want the user to select from some list of options and then based on that, populate another list with those options. Country and Cities.
  2. An ISO list of countries ships with ERPNext, so you can get that list by assigning it to the options of a select field. IF you want that list to be filtered, or to not include or exclude some country specifically, use a filter query.
  3. You can repeat this pattern, but there isn’t a built-in dataset for cities, states or postal codes in ERPNext. I’d recommend the python package pycountry, which gets you states/provinces/subdivisions but no other address helpers. Maybe that’s enough.
  4. To get those subdivisions of countries in the UI, you need a callback that returns a list and then sets it to the the options in the docfield.

This callback should trigger on country.

 frappe.call({
   method: "dotted.path.to.python.method", // that returns a list of subdivisons when given a country argument
   args: {"country": frm.doc.country // where 'country' is the name of the docfield
 }).done((r) => {
    frm.set_df_property("city", "options", r.message);
 }).fail((f) => {
    console.log(f);
 });

What you’re asking for sounds simple but is actually hard to implement in an accurate way for all countries, which is why it has not been done yet in ERPNext. If you’re doing it for one country, it could be contributed as a regional module.


#5

thanks you!, you help me a lot :heart:


#6

Hola Sara, pudiste resolver el llenado automático?

Podrías detallar un poco cómo lo lograste?

Gracias de antemano.