Override Warehouse Based On Customer

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!");
                  }                             
             }
     });      
 });

The code seems ok. Try to use set_value function to set warehouse value.

frappe.model.set_value(cdt, cdn, "warehouse", r.message.warehouse);   
msgprint(d.warehouse);

It did show correct warehouse value (means function working fine)…but the field is not updated with new value…Whats wrong?

@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 ??

@jof2jc you can’t call an ajax in validate. Add this server-side.

@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); 
                  }                                        
             }
     });
      
});

If I trace the log, warehouse was updated but got override again by background function. It seems custom script is behind priority.

If I use other field trigger after item_code change then it worked fine.

How to get this to work with item_code change event?

This is a tough one.

Put your frappe.call like this

frappe.after_ajax(function() { 
  frappe.call({ ...

  ...
});

This will make sure that the event gets fired after any pending requests.

Hello @rmehta and everyone,

I have the same problem, the warehouse does not update some background function cancel the script. Here is my code in delivery_note.js

frappe.after_ajax(function() { 
      frappe.ui.form.on("Delivery Note Item", {
    	item_code: 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
    		}        	    		
    	}
      });
});

For this example I am using a fixed name for the warehouse, but never assign it.
I used the function:

frappe.after_ajax(function() { 
      frappe.call({ ...

      ...
});

Is something wrong or what file that handles the background function should I modify?

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
    		}        	    		
    	}
});
1 Like

Thanks @jof2jc,

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.

Thanks for your help.

frappe.ui.form.on("Delivery Note", "validate", function(frm, cdt, cdn) {
    	$.each(frm.doc.items, function (i, r) 
		{
			frappe.model.set_value(r.doctype, r.name, "warehouse", "warehouse name");
		})
});
1 Like

It works!, Solved.

Thank you @jof2jc, you saved my life. :+1: