Sales Invoice - Update Warehouse in Sales Invoice Item based on Customer

Hi,

With reference of the following links,

I am still unable to show “warehouse” in Sales Invoice Items. Custom FIeld “warehouse” created in Customer DocType.

Basically, I want the Warehouse in SI Items to be updated based on the Customer selection.

My code:

frappe.ui.form.on("Sales Invoice Item", "item_code", function(frm, cdt, cdn) {
      
frappe.after_ajax(function() { 
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); 
                  }                                        
             }
     });
});
      
});

try to use trigger on item_name change instead of item_code

Hi!

Nope. Not working either.

Try this:

frappe.ui.form.on("Sales Invoice Item", "item_code", function(frm, cdt, cdn) {
	frappe.model.get_value("Customer", cur_frm.doc.customer, "warehouse", function(r) { 			
		var d = locals[cdt][cdn];
		d["warehouse"] = r.warehouse;		
	});
});

Hi

Thanks for the reply. Still unable to get it to work.

From what I understand after looking through the forums, we need to put the custom script in the master instead of the child table. It won’t trigger if we put this inside the child table.

Hi @mulyadi-agtechsg ! it should be like this

cur_frm.cscript.{field_under_child_table} = function (doc, cdt, cdn) {
var d = locals[cdt][cdn];
/do something here/
}

ex.

If I have child table A and has field sample_field then it will be like this

cur_frm.cscript.sample_field = function (doc, cdt, cdn) {
var d = locals[cdt][cdn];
/do something here/
}

Take note: This script should be under the Parent Doctype, in your case it should be in Sales Invoice

1 Like

Hi @johnskywalker

OK, at least it triggered when using item_code field.
However, it does not get all the details into the item details.

Findings so far.

  1. Using item_code to trigger, it triggers but all item details will not be retrieved
  2. Using item_name to trigger, does not trigger

Try to trigger when user change qty

please help me . how can i use this code ?