How to restrict user access to records on a table based on current user same as field on table

kdelbridge
Tera Expert

I need to limit the records that a user can see when they access records on one of my tables.

I have a table that users with several different roles has access to.

There is one user in particular ( my loan officer user) that I ONLY want them to be able to see the records where they are the loan officer.

 

I tried this this business rule so far, and it isn't working. When I impersonate a user with the this role , they can still access records where they are not the loan officer.

 

Here is my code I am using. Be gentle as I am still learning my scripting.  will copy in text and attach an image

 

(function executeRule(current, previous /*null when async*/) {
// Get the current user
    var currentUser = gs.getUser();

    // Check if the current user has the "Loan Officer" role
    if (currentUser.hasRole('x_1073015_loan_p_0.Loan Officer')) {
        // Check if the current user matches the name in the Loan Officer field
        if (current.loan_officer == currentUser.getName()) {
            // Allow access to the record for Loan Officers
            gs.log("User with Loan Officer role and matching name has access to the record.");
        } else {
            // Prevent access to the record for Loan Officers if name doesn't match
            gs.log("User with Loan Officer role does not match the Loan Officer name, access denied.");
            current.setAbortAction(true);
        }
    } else {
        // Allow access to all records for users without the "Loan Officer" role
        gs.log("User without Loan Officer role has access to the record.");
    }

})(current, previous);
kdelbridge_0-1710531253170.png

Thanks in advance!

2 REPLIES 2

Chandra18
Mega Sage

Hi @kdelbridge 

Create a read acl. Use  answer = true for access & answer = false for abort.

 

Thank You!

James Chun
Kilo Patron

Hi @kdelbridge,

 

Have you considered using data filtration instead?

https://docs.servicenow.com/bundle/washingtondc-platform-security/page/administer/security/task/crea...

 

It shouldn't require you to write any code.

 

But if you do insist on using the BR, can you confirm that you are using the 'Query' BR to block access?

 

Thanks