Big shout out to @hrwx for contributing this script. Sincere thanks on behalf of the community. Script dynamically filters doctype contacts for supplier and then for the transporter and return the primary contact default email_id field. It can be applied for other situations where you need to pull a field (in this case email_id) from one doctype (contact / supplier) to another doctype (delivery note) with dynamic filters (based upon a user selection - in this case transporter as a subset of suppliers).
Small explanation for future reference:
The first step is to create a custom field in Delivery Note called transporter_email, with options as Email. Then need to add this custom script for document type Delivery Note. This transporter_email will now be available as an option under email Notification - so delivery note can be sent to transporter upon submit.
Custom Script:
frappe.ui.form.on("Delivery Note", {
transporter: function(frm) {
if (!frm.doc.transporter) {
return;
}
frappe.call({
'method': 'frappe.client.get_list',
'args': {
'doctype': 'Contact',
'filters': [
["Dynamic Link", "parenttype", "=", "Contact"],
["Dynamic Link", "link_doctype", "=", "Supplier"],
["Dynamic Link", "link_name", "=", frm.doc.transporter],
],
'fields':'email_id'
},
'callback': function(r){
if (r && r.message) {
frm.set_value("transporter_email", r.message[0].email_id);
refresh_field("transporter_email");
}
}
});
}
})