Hid fields in child table per user role

Dear All
I want to hide some fields in the child table per user role
Thank you

@msiam use permission levels
give the field a permission level for example 2 . then open permission manager for the role and set level 2 permission .

I need JS Code

@msiam no you don’t . just use permission levels

Try Once @msiam

if(frappe.user.has_role('Manager')){
  const gridRows = frm.fields_dict["your_child_table_name"].grid.grid_rows;

  gridRows.forEach((row) => {
    row.docfields.forEach((field) => {
      const fieldName = field.fieldname;
      
     if (["field_x", "field_y"].includes(fieldName)) {
          field.hidden = 1;
        } else {
          field.hidden = 0;
        }
    });
  });
}

not working
my code ::

frappe.ui.form.on(‘Delivery Note’, {
refresh: function(frm) {
if (frappe.user.has_role(‘Showroom User’)) {
const gridRows = frm.fields_dict[‘items’].grid.grid_rows;
gridRows.forEach((row) => {
row.doc.items.forEach((item) => {
if ([“base_rate”, “base_amount”, “rate”, “amount”, “discount_and_margin”].includes(item.fieldname)) {
item.df.hidden = 1;
} else {
item.df.hidden = 0;
}
});
});
frm.fields_dict[‘items’].grid.refresh();
}
}
});

frappe.ui.form.on(‘Delivery Note’, {
    refresh: function(frm) {
        if (frappe.user.has_role(‘Showroom User’)) {
            const gridRows = frm.fields_dict[‘items’].grid.grid_rows;
            gridRows.forEach((row) => {
                row.docfields.forEach((field) => {
                    const fieldName = field.fieldname;
                    if ([“base_rate”, “base_amount”, “rate”, “amount”, “discount_and_margin”].includes(item.fieldname)) {
                        item.df.hidden = 1;
                    } else {
                        item.df.hidden = 0;
                    }
                });
            });
            frm.fields_dict[‘items’].grid.refresh();
        }
    }
});

Try this @msiam

frappe.ui.form.on(‘Delivery Note’, {
refresh: function(frm) {
if (!frappe.user.has_role(‘System Manager’)) {
const gridRows = frm.fields_dict[‘items’].grid.grid_rows;
gridRows.forEach((row) => {
row.docfields.forEach((field) => {
const fieldName = field.fieldname;
if ([“base_rate”, “base_amount”, “rate”, “amount”, “base_price_list_rate”, “discount_and_margin”,“price_list_rate”].includes(item.fieldname)) {
item.df.hidden = 1;
} else {
item.df.hidden = 0;
}
});
});
frm.fields_dict[‘items’].grid.refresh();
}
}
});

it’s affecting only “rate”, “amount”
I want to add more fields

@msiam you can keep adding in list of fields in given code

it’s affecting only “rate”, “amount” only
I want to add more fields like
base_rate”, “base_amount”, …etc

Just apply field level permissions (change perm levels) to the child table in the customize form.
Provide proper access to those perm levels in the role permission manager and you should be good.

my tested version is as below

const hidden = frappe.user.has_role("Agriculture Manager")? 1:0;
["rate","amount"].forEach((field) => {
    frappe.meta.docfield_map['Delivery Note Item'][field].hidden = hidden;
})
frappe.ui.form.on("Delivery Note", {
});

before the grid created/rendered, there is getdoctype ajax call to fetch the parent doctype’s meta data which includes the child table(doctype) meta also, the meta data is kept in frappe.meta.docfield_map, when creating/rendering the child table(grid), the local meta data is used, we need to manipulate(change) the local meta data before grid is created.