How to hide specific attachment to all the users except admin's and AssignedTo.Manager

Sironi
Kilo Sage

Hi All, Please help me on below issue

I have 10-attachments on Incident records, there how to hide specific attachment which has file name 'INC000001' out of 10 attachments

For All the users - 9 attachments should be available except one attachment which has 'INC000001' as a file name (hide).
For admin's and AssignedTo.Manager should be available 10-attachments.


but below script creates infinite loop, so could you pleas suggest some solution.

 

 var grA = new GlideRecord("sys_attachment");
    grA.addEncodedQuery("table_name=incident");
    grA.query();
    if (grA.next()) {
                current.addQuery("file_name", 'CONTAINS', 'INC000001);
    }

 

 

4 REPLIES 4

Community Alums
Not applicable

Hi

There are multiple ways you can achieve this. Using OnLoad client script, After Display business rule or through an ACL as well.

 

Please check ths https://www.servicenow.com/community/developer-forum/how-to-hide-attachments-based-on-user-role/m-p/...

 

As per other blogs, it's generally recommended to handle sensitive operations like controlling access to attachments using the server side

 

(function executeRule(current, previous /*null when async*/ ) {
    // Check if the current user is an admin or the manager of the assigned to
    var isAdminOrManager = gs.hasRole('admin') || 
                           (current.assigned_to.manager == gs.getUserID());

    // If the user is an admin or manager, don't hide any attachments
    if (isAdminOrManager) {
        return;
    }

    // Query for the specific attachment
    var grA = new GlideRecord("sys_attachment");
    grA.addQuery("table_name", "incident");
    grA.addQuery("table_sys_id", current.sys_id);
    grA.addQuery("file_name", "CONTAINS", "INC000001");
    grA.query();

    // If the specific attachment is found, hide it
    if (grA.next()) {
        grA.setValue('visible', false);
        grA.update();
    }

})(current, previous);

Change the addQuery conditions as per your requirement.

Please mark this helpful if it works. 

Hi Sravani,

Thanks for sharing your inputs and thoughts.
I gone through this link : https://www.servicenow.com/community/developer-forum/how-to-hide-attachments-based-on-user-role/m-p/...
Which is actually hiding  attachment icon itself as per role. but in my case I want to hide specific attachment itself only should not impact on other attachments..

Please let me know is it Update Business Rule  ?  if yes how come it will help us every time when we impersonate with users and test it.

var grA = new GlideRecord("sys_attachment");
grA.addQuery("table_name", "incident");
grA.addQuery("table_sys_id", current.sys_id);
grA.addQuery("file_name", "CONTAINS", "INC000001");
grA.query();

// If the specific attachment is found, hide it
if (grA.next()) {
grA.setValue('visible', false); // with name VISIBLE there is no such system field in ATTACHMENT table
grA.update();
}


Sironi
Kilo Sage

Hi Can some one please suggest some solution

sushilraina9396
Tera Contributor

have you tried setting up encryption context.