Adding Options to custom filed based on item_code selected in the items child table

I have a doc “Purchase Order” - parent

“Purchase Order Items” - child table with fieldname as “items”

In child table “items” - 2 filelds “item_code” and “custom_mpn” where custom_mpn is a select field

I want to dynamically add options to “custom_mpn” based on the “item_code”

I tried with code in client script when

frappe.ui.form.on(“Purchase Order Item”,{item_code:function(frm,cdt,cdn) {

var row = locals[cdt][cdn];
if(row[“item_code”] == “a”)
{
frappe.utils.filter_dict(cur_frm.fields_dict[“items”].grid.docfields,
{“fieldname”: “custom_mpn”})[0].options = [" “,“a”,“b”]
cur_frm.refresh_field(“custom_mpn”);
}
else
{
frappe.utils.filter_dict(cur_frm.fields_dict[“items”].grid.docfields,
{“fieldname”: “custom_mpn”})[0].options = [” ",“c”,“d”]
cur_frm.refresh_field(“custom_mpn”);
}

}
});

It is not adding any options to the field

Hi @Navya_shree,

Please apply it.

frappe.ui.form.on("Purchase Order Item", {
    item_code: function(frm, cdt, cdn) {
        var row = locals[cdt][cdn];
        if (row.item_code === "a") {
            frm.fields_dict.items.grid.update_docfield_property("custom_mpn","options",["A","B"]);
        } else {
            frm.fields_dict.items.grid.update_docfield_property("custom_mpn","options",["C","D"]);
        }
        frm.refresh_field("items");
    }
});

Output:

Reference:

Thank You!

@NCP Thank you very much.