Fetching Data from Another Doctype Based on Field
Guide
This snippet works on Doctype `Sales Invoice`. It looks into the `Address` doctype and searches for the ID/`name` that is equivalent to the current form (cur_frm.doc.) `shipping_address)name` field, since those should be equal as the `shipping_address_name` field is a link type field. Then it pulls the `zone` field from there.
The second part parses the response data to just pick the desired `zone` field data. Good to have Developer Console running to see the logs.
frappe.ui.form.on('Sales Invoice', 'before_save', function(frm) {
frappe.db.get_value('Address', {'name':cur_frm.doc.shipping_address_name}, 'zone')
.then(response => {
// console.log(response);
// console.log(response['message']['zone']);
frm.doc.zone = response['message']['zone'];
});
});