Limit Customer & Items Search By Company

When a new sales invoice is created and the company is set, is there a way to limit the search results for the customer as well as the item to those that belong to that company? Out of box, I know both Customer and Item doctypes don’t have a company field, but I have added those as custom fields. By having these search results filtered by company it reduces noise as well as lessens chance of error by the user. Has anyone gone down this route?

Hi @fiveoaks,

It’s possible, you have to create a custom field like company in the customer and item doctype. then set the Overriding Link Query By Custom Script. When the user selects the customer then only company-related customer and item will appears.

I hope this helps.

Thank You!

That worked perfectly. Here is the custom script I created for others to reference if they like:

frappe.ui.form.on( "Sales Invoice", "refresh", function( frm ){
    frm.set_query( "customer", function(){
        return {
            "filters": [
                [ "Customer", "custom_company", "=", frm.doc.company ]
            ]
        }
    });

	frm.set_query( "item_code", "items", function(){
        return {
            "filters": [
                [ "Item", "is_sales_item", "=", "1" ],
                [ "Item", "has_variants", "=", "0" ],
                [ "Item", "custom_company", "=", frm.doc.company ]
            ]
        }
	});
});

One question though. In the example of the link you provided, they are using “onload”. I couldn’t get that to work but once I changed to “refresh” it did. Does “onload” only work if the application is restarted? I did do a clear cache to no avail.