I wantto override warehouse based on customer on event ‘validate’ on saving. The message does appear means the function does work…but item warehouse is not updated. Why? Anybody know? Thanks
// fetch warehouse based on customer
frappe.ui.form.on("Delivery Note", {
validate: function(frm, cdt, cdn) {
// calculate incentives for each person
var total_comm = 0;
$.each(frm.doc.items, function(i, d) {
frappe.call({
method: "frappe.client.get_value",
args: {
doctype: "Customer",
fieldname: "warehouse",
filters: { name: frm.doc.customer},
},
callback: function(r) {
if (r.message.warehouse){
msgprint(r.message.warehouse);
// d = frappe.get_doc(cdt, cdn);
d.warehouse = r.message.warehouse;
refresh_field("items");
}
else{
validated = false;
msgprint("Warehouse not found for this customer!");
}
}
});
});
@nabinhait if i traced it…it did update the warehouse value, but after completing the function it went back to default value…Can you help…perhaps javascript behavior ??
@rmehta triggering item_code change and the result is the same… msgprint(d.warehouse) did show the expected warehouse value but it go back to previous default value… What may cause this?
frappe.ui.form.on("Sales Invoice Item", "item_code", function(frm, cdt, cdn) {
var d = locals[cdt][cdn];
frappe.call({
method: "frappe.client.get_value",
args: {
doctype: "Customer",
fieldname: "warehouse",
filters: { name: frm.doc.customer },
},
callback: function(r) {
if (r.message.warehouse){
frappe.model.set_value(cdt, cdn, "warehouse", r.message.warehouse);
//msgprint(d.warehouse);
}
else {
frappe.throw("Warehouse not found for Customer : " + frm.doc.customer);
}
}
});
});
this is because asynchronous behavior of javascript … try another field trigger e.g. custom field trigger on change of item_code…or just use validate event to change the warehouse
frappe.ui.form.on("Delivery Note Item", {
custom_field: function(frm, cdt, cdn){
//msgprint("TEST");
var item = frappe.model.get_doc(cdt, cdn);
if(item.item_code){
frappe.model.set_value(cdt,cdn,"warehouse","ALMACEN-006 - DE");//not working 1
msgprint("Item selected: "+item.item_code);//show this ok
//item.warehouse = "ALMACEN-006 - DE";//not working 2
}
}
});
It is solved using qty (item quantity), it works fine.
frappe.ui.form.on("Delivery Note Item", {
qty: function(frm, cdt, cdn){
//msgprint("TEST");
var item = frappe.model.get_doc(cdt, cdn);
if(item.item_code){
frappe.model.set_value(cdt,cdn,"warehouse","ALMACEN-006 - DE");//not working 1
msgprint("Item selected: "+item.item_code);//show this ok
//item.warehouse = "ALMACEN-006 - DE";//not working 2
}
}
});
However if the user does not change the quantity of the item the script will not be executed, how can apply the validate event to change the warehouse?, I don’t know how to do that, it is in the delivery_note.py?
Or how to set qty default “0” instead of the 1 that always loads, I try setting “0” to the default value that this doc child has, but not zero load is always one. I do not know where it is assigned from.