Filter second field options based on first field selection

Hi

I have created 3 doctypes, 1. Country doctype, 2. City doctype 3. doctype3

Country doctype will have all the country names, City doctype will have all the cities with its respective Country link from Country doctype.

In 3rd Doctype i have created two fields with links to Country and City doctypes.

so what i want is if i select country, the city field should show only cities which are related to a specific country.

right now city field showing all cities from all countries.

Should we have to write custom script? or is there any option to select in 3rd doctype?

You would have to write a custom script using frm.set_query.

Refer documentation:
https://frappeframework.com/docs/user/en/api/form#frmset_query

frappe.ui.form.on("Doctype3", {
	linkfield: function(frm) {
		frm.set_query("linkfield", function() {
			return {
				filters: [
					["country_doctype","country", "in", [frm.doc.country_doctype_field]]
				]
			}
		});
	}
});

linkfield is the fieldname of the city field
country_doctype_field is the name of the field linking to Country Doctype
country is the country link fieldname in the City Doctype

Try this and let me know.

Hi @Vesper_Solutions

I have a similar situation but instead of countries and cities, I have car brands and models. I have tried your solution but it does not seem to be working. I have three simple doctypes and have put the following code in client scripts:

frappe.ui.form.on(‘Cars’, {
model: function(frm) {
frm.set_query(‘model’, function() {
return {
filters: [
[‘car_makers’,‘brand’, ‘in’, [frm.doc.brand]]
]
};
});
}
});

Could you suggest where it is I am going wrong? Many thanks

https://frappeframework.com/docs/v13/user/en/api/form#frmset_query

I resolved it. For anyone looking for a similar solution, I used the below code:

frappe.ui.form.on("Cars", {
	onload: function(frm) {
		frm.set_query("model", function() {
			return {
				filters: [
					["Car Models","brand", "in", [frm.doc.brand]]
				]
			};
		});
	}
});

FYI, this doesn’t work on quick entries only on the full form