How to get only some records from the linked doctype based on a field value of the linked doctype

I made a doctype ‘Asset Data’ and it has a field ‘Asset Code’ which is linked to the another doctype ‘Item’
Asset Data(Doctype)
– Asset Code → Item (’ Asset Code’ field linked to ‘Item’ doctype)
Item(Doctype)
– Item Group
so when filling the field ‘Asset Code’ all the records of ‘Item’ doctype are getting displayed and I want to display only those records of ‘Item’ doctype for whom ‘Item Group’ == ‘IT’
so how can i do it?

Use frm.set_query.

Refer:
https://frappeframework.com/docs/user/en/guides/app-development/overriding-link-query-by-custom-script
Eg:

frappe.ui.form.on("Bank Reconciliation", "onload", function(frm) {
    frm.set_query("bank_account", function() {
        return {
            "filters": {
                "account_type": "Bank",
                "group_or_ledger": "Ledger"
            }
        };
    });
});

1 Like

thanks @Fadil_Siddique
I was able to solve the issue
here is the code

frappe.ui.form.on ("Asset data", {
        onload : function(frm) {
                frm.set_query("asset_code", function() {
                return {
                    "filters": {
                        "item_group": frm.doc.asset_type
                    }
                };
            });
        }
});

above code will filter the records of Item doctype for whom item_group is equal to value of asset_type field

1 Like