How to grant access(group/role) to user automatically for the users who exists in other custom table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-08-2023 10:32 PM
Users table = x
custom table = y
Requirement is like query or code should run daily and checks if user exists in custom table then provide access (one group with read only access for custom app) automatically to those users who dont have it already.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-08-2023 11:05 PM
Hi @Priya Bonam ,
You can write a before Update BR with trigger as your_field changes with below code , just replace sys_user with your custom table and add another line of code to provide the role which you want:
var grSysUser = new GlideRecord('sys_user');
grSysUser.addEncodedQuery("user_name="+current.your_fieldname_here);
grSysUser.query();
if (!grSysUser.next()) {
current.setAbortAction(true);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-08-2023 11:23 PM
Hi @Priya Bonam
Did you try anything and got stuck?
You can achieve this with flow designer schedule as trigger or with a scheduled job.
Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2023 03:40 AM
Stuck to grant access with script .
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2023 04:29 AM
Hi @Priya Bonam ,
To achieve this, you can follow the steps outlined below:
Create a scheduled job or script in ServiceNow that runs daily.
write a script in scheduled job to query the custom table (Table Y) and check if each user exists in the Users table (Table X) and if they do not already have access to the custom app.
- try to use below reference script :
// Step 2: Define the Script Logic
// Query the custom table (Table Y) for users
var customUsers = new GlideRecord('table_y');
customUsers.query();
// Loop through the custom users and check for access
while (customUsers.next()) {
var userId = customUsers.getValue('user_id');
// Check if the user exists in the Users table (Table X)
var user = new GlideRecord('sys_user');
user.addQuery('user_id', userId);
user.query();
// Grant access if the user exists and doesn't have it already
if (user.next() && !user.isMemberOf('group_read_only_access')) {
// Assign the user to the predefined group with read-only access to the custom app
user.addToGroup('group_read_only_access');
}
}
If you want to use own script , then create a array to push all the users belong to that custom table lets say Y . And then create a read ACL and cal your script include function and then if user is part of array then set answer to true to grant access .
Regards,
Riya Verma