Filtering Sales Order Item per company

I have added a custom field called company on Item doctype that links an item to a particular company. I want, while making a Sale Order, to only be able to select items linked to the company set in the SO company field.
I think it can be done through a client-side custom script, but I have tried several options on the forum with no success.

My current code looks like this:

frappe.ui.form.on("Sales Order", "refresh", function (frm, cdt, cdn) {
  frm.set_query("item_code", "items", function () {
    return { 
      query: "erpnext.controllers.queries.item_query",
      filters: {'company': frm.doc.company}
    }
  });
});

Any help will be highly appreciated (I am using V7)

I am not sure , try this code and see if this works or not , if you are using v7 , delete the first row of the child table and check whether this code works for the second row ,

frappe.ui.form.on("Sales Order", {
item: function(frm) {
frappe.call({
"method": "frappe.client.get_value",
"args": {
"doctype": "Item",
"filters": {"company": cur_frm.doc.item},
"fieldname": ["company"]
}, 
callback: function(r) { 
cur_frm.set_query("company", "items", function(doc, cdt, cdn){
return {
"filters": {
"company": ["=",r.message.company],
}
}
});
}
});

}
});

Thanks

No, it still displays all items

Did you try deleting the first row and start filtering in the second row?

@RWEMA_Aimable , Try with this code -

frappe.ui.form.on("Sales Order", "refresh", function (frm, cdt, cdn) {
    frm.set_query("item_code", "items", function () {
         return{
		filters: {
			"company": frm.doc.company
		}
         }
    });
}); 

Thanks, Priya

2 Likes

@srinivasragav Yes did that but still the same

Try this ,

frappe.ui.form.on("Sales Order", {
item: function(frm) {
frappe.call({
"method": "frappe.client.get_value",
"args": {
"doctype": "Item",
"filters": {"company": cur_frm.doc.item},
"fieldname": ["company"]
}, 
callback: function(r) { 
cur_frm.set_query("company", "items", function(doc, cdt, cdn){
return {
"filters": {
"frm.doc.company": ["=",r.message.company],
}
}
});
}
});

}
});

@priya_s Great that works Thanks