Auto fill a field based on another field

Hello,
I have 2 docTypes. NGO (id, name, state, address, mobile), and NGO_STATUS( ngo_id, ngo_name, ngo_state, ngo_status)

In NGO_STATUS, ‘ngo_id’ is a link to NGO
.
When i select a particular ngo in the NGO_STATUS.ngo_id, the ngo_name and ngo_state should be auto filled based on the selected ngo_id.

Is this possible. Pls let me know.

I read about dynamic link, but it seems to be different than what i need.

have a look at:
https://frappe.io/docs/user/en/guides/app-development/overriding-link-query-by-custom-script

You can simply use the add_fetch function for it.

e.g.

https://github.com/frappe/erpnext/blob/develop/erpnext/hr/doctype/leave_application/leave_application.js#L4-L5

1 Like

Thank you very much. This helped configure my need.

Can you also please point me to similar code which does the below

have a look at:

https://frappe.io/docs/user/en/guides/app-development/overriding-link-query-by-custom-script

@sangram

Both my states_list and country fields are “SELECT” type fields. I need to return corresponding states when a country is selected.

//Return states list based on country. I think i need something like this. But i am sure that the way i am returning the state_list is wrong. Can someone point me out to an existing code, or some tutorial. Tried frappe doc, may be i missed it though

cur_frm.cscript.custom_validate = function(doc) {
// clear states_list , which is a SELECT type list
doc.states_list = “”;

// Returns states based on country
switch(doc.country_name) {
    case "India":
        doc.states_list = "Delhi, Assam,AP, TN";
        break;
    case "USA":
         doc.states_list = "Texas, California, MD, MS, WDC";
        break;
    default:
        doc.states_list = "error";
}

}

Please let me know.

you can use, set_df_property to set options

e.g.

frm.set_df_property("state", "options", ["Delhi", "Assam", "AP", "TN"]);
1 Like

Thank you @Sangram . I tried to change the script as per your suggestion. But the states list dont get updated automatically when a country is selected. Should i be using “cur_frm.cscript.custom_validate” or something else. Can you pls point me to some code.

Below is what i have

> cur_frm.cscript.custom_validate = function(doc) {
> 
> // Returns villages based on Mandal
> switch(doc.mandal) {
>    case “vikarabad”:
>        frm.set_df_property(“village”, “options”, [“v1", “v2”, “v3", “v4”]);
>        break;
>    case “marpalle”:
>         frm.set_df_property(“village”, “options”, [“d1”, “d2", “d3”, “d4"]);
>        break;
> }
> }

you have to write on the country trigger, the syntax should be like,

frappe.ui.form.on("DocType Name", {
	country: function(frm) {
		village_map = {
			"vikarabad": ["v1", "v2", "v3", "v4"],
			"marpalle": ["d1", "d2", "d3", "d4"]
		}
		if(frm.doc.country) {
			frm.set_value("state", "")
			frm.set_df_property("village", "options", village_map.get(frm.doc.contry))
		}
	}
})
1 Like

Thank you very much for your help. I was able to get it working. But for some reason, in the above code did not work, it said village_map.get() does not exist. So i made some changes to remove the get function.

frappe.ui.form.on(“Customer”, {
mandal: function(frm) {
if(frm.doc.mandal == “Vikarabad”) {
frm.set_df_property(“village”, “options”, [“d1”, “d2”, “d3”, “d4”]);
}
if(frm.doc.mandal == “Marpalle”) {
frm.set_df_property(“village”, “options”, [“v1”, “v2”, “v3”, “v4”]);
}
}
})

But with this change its working. Thanks for your help.

Good,
Or try this as we are in js .get() will not work.

frm.set_df_property("village", "options", village_map[frm.doc.contry])
2 Likes

Thanks @sangram it worked.