Restrict UOM Link Field Values In Sales Invoice Item

Hi,

I want to restrict the values in the UOM (Unit of Measure) Link Field in the Sales Invoice Item of the Sales Invoice. Instead of showing all the values from the UOM documents, I only want to display the UOMs that are defined in the Item doctype’s UOM field.

For example, let’s say I have an item called “ABC” with its default UOM set as “Nos” (meaning “Number of Items”). Additionally, the product is also sold in “Boxes”. I have recorded the conversion factor between “Box” and “Nos” in the UOM table within the Item doctype.

Now, when I create a Sales Invoice with the item “ABC” in the items table, I only want the UOM field to show the values “Box” and “Nos”, which are the available UOMs for that particular item.

please help me with achieving this requirement? Thank you!

3 Likes

Check this: Overriding Link Query By Custom Script

1 Like

To customize the filtering of the “uom” field in the child table “items” within the Sales Invoice form, you can incorporate the following code snippet into the client script of the Sales Invoice:

frappe.ui.form.on("Sales Invoice", {
    refresh: function(frm) {
        frm.fields_dict.items.grid.get_field("uom").get_query = function(doc, cdt, cdn) {
        //items is the name of the child table, uom is the field you want to apply filter
            const row = locals[cdt][cdn];
            if (row.item_code) {
                console.log(row.item_code)
                //code for fetching uoms from item
                var filters = { 'name': ['in', ['NOS', 'BOX']] };
                return {
                    filters: filters
                };
            }
        };
    }
});

Please note that you should replace [[‘NOS’] ,[‘BOX’]] with the appropriate uoms that you have retrieved from the corresponding item in that row.

3 Likes

I’m glad to share that I found a solution to restrict the UOM (Unit of Measure) options in the Sales Invoice Item form. Thanks to @rmehta and @jabir.elat for your valuable inputs.

To implement the solution, I added a custom client script to the Sales Invoice doctype. Here’s the code I used:

frappe.ui.form.on('Sales Invoice', {
    onload: function(frm) {
        // Set the get_query function for the 'uom' field on form load
        frm.fields_dict.items.grid.get_field('uom').get_query = function(doc, cdt, cdn) {
            // Get the current row
            let row = locals[cdt][cdn];

            // Check if the row has uom_list data
            if (uom_lists[cdn]) {
                return { filters: { 'name': ['in', uom_lists[cdn]] } };
            } else {
                // If uom_list data is not available, show all UOMs
                return { filters: { 'name': ['!=', ''] } };
            }
        };
    }
});

let uom_lists = {};

frappe.ui.form.on('Sales Invoice Item', {
    item_code: function(frm, cdt, cdn) {
        let row = locals[cdt][cdn];
        frappe.db.get_doc('Item', row.item_code)
            .then(docs => {
                let uom_list = [];
                docs.uoms.forEach(uom => {
                    uom_list.push(uom.uom);
                });
                uom_lists[cdn] = uom_list;

                // Trigger a refresh of the 'uom' field to apply the updated get_query function
                frm.fields_dict.items.grid.get_field('uom').refresh();
            });
    },
});

This solution successfully restricts the UOM options in the Sales Invoice Item form

2 Likes