Change content of a field based on the value of another field

I have some fields of the type Selection in a custom form that, depending on the value of another field, must show a certain list of options.
My question is this, should I perform this task in the configuration of the fields or using a Custom Script or in the PY and JS files of the custom form?

1 Like

You can set it in custom script in js.

Please refer to below links,

Thanks for your response @schilgod

I have the following code in Custom Script:

if (cur_frm.doc.item_type == "Raw Material") {
	cur_frm.set_df_property('items', 'options', [
			"raw_material1",
			"raw_material2",
			"raw_material3",
			"raw_material4"
	]);
} else
if (cur_frm.doc.item_type == "Compound") {
	cur_frm.set_df_property('items', 'options', [
			"compound1",
			"compound2",
			"compound3"
	]);
}
cur_frm.refresh_field('items');

This works well when the item_type field has a default option (for example “Compound”), then it only shows the options for that value, and if you change one option for another it has no effect.

When there is no default option and you choose manually one option or another it does not work.

1 Like

When is this code triggered? It should be run when item_type is change in below function.

frappe.ui.form.on("Doctype", "item_type", function (frm, cdt, cdn) {
//your code here
})
1 Like

Oh, that’s right, I forgot that, thank you very much
It works perfectly now.

Really thank you very much @schilgod.

Great :+1: