How to make a button visible to User with specific role

“Hey everyone, within the interview DocType, there exists a ‘Reschedule’ button. My intention is to make this button accessible only to users who possess the ‘Placement Officer’ role. I have already devised a script that ensures that the button remains hidden for all other users.


Now, what I need to customize in this script is…”

@Mounananda_Reddy you can use call function to get users permission :

frappe.ui.form.on('Interview', {
	refresh(frm) {
	    frappe.call({ 
    method: "frappe.client.get", 
    args: { doctype: "User", name: frappe.session.user, }, 
    callback(r) { 
        if(r.message) {
            let exist=false;
           for (var i=0;i<r.message.roles.length;i++){
               if (r.message.roles[i].role=="your_role"){
                   exist=true
               }
           }
            console.log(exist)
            if (!exist){
                frm.remove_custom_button("Reschedule Interview")
            }
        }
    }    
});}
})

When I access the doctype, the “Interview Reschedule” button will briefly flash and then disappear

frappe.ui.form.on(‘Interview’, {
refresh(frm) {
frappe.call({
method: “frappe.client.get”,
args: { doctype: “User”, name: frappe.session.user },
callback(r) {
if (r.message) {
let isPlacementOfficer = false;
for (var i = 0; i < r.message.roles.length; i++) {
if (r.message.roles[i].role === “Placement Officer”) {
isPlacementOfficer = true;
break;
}
}
console.log(isPlacementOfficer);
if (!isPlacementOfficer) {
frm.remove_custom_button(“Reschedule Interview”);
}
}
}
});
}
})

Hi @Mounananda_Reddy,

Please apply the simple script for it.

frappe.ui.form.on("Interview",{
    refresh: function(frm) {
        setTimeout( () => {
            if (frappe.user.has_role('Placement Officer') == -1) {
                frm.remove_custom_button("Reschedule Interview");
            }
        }, 10);
    }
});

Please set the doctype name and field in the script.

Reference: https://docs.erpnext.com/docs/user/manual/en/restrict-user-based-on-child-record

I hope this helps.

Thank You!

1 Like

Hello @NCP,
The “Interview reschedule” button is not a custom feature it’s a pre-existing built-in button. Therefore, I’m looking for code that works with built-in functionality, not custom implementations.

Hi @Mounananda_Reddy,

I think you check the documentation of default hide button script.

I hope this helps.

Thank You!

Hello @NCP,
thank you for your response.
I would like to explain my concern in detail. How can we make the built-in button named “interview reschedule” visible only to users with the role “Placement Officer” in ERPNext’s “Interview” doctype? The button should not be visible to other roles.

Please again check the script and reload and check it.
I created a script according your scenario.

Thank You!