Skip to content

Commit d0c077e

Browse files
Add the members to List of the Groups using GlideRecord (#1300)
* Add the members to List of the Groups using GlideRecord.js this script is to add a single group member to list of grous * Create readme.md this file is for explaining what the code does * Add the members to List of the Groups using GlideRecord Add the members to List of the Groups using GlideRecord
1 parent 5b0f5c8 commit d0c077e

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var rec = new GlideRecord('sys_user_group');
2+
rec.addEncodedQuery(''); //get the list of groups needed.
3+
rec.query();
4+
while (rec.next())
5+
{
6+
var rec1 = new GlideRecord('sys_user_grmember');
7+
rec1.addQuery('group' , rec.sys_id);
8+
rec1.addQuery('user' , '7279f455939e71944c77b6b5fbba1033'); // put the sys_id of "user" here
9+
rec1.query();
10+
if(!rec1.next()) //checking if group member is already existed. if not, we add them.
11+
{
12+
rec1.initialize();
13+
rec1.group = rec.sys_id;
14+
rec1.user = '7279f455939e71944c77b6b5fbba1033'; // put the sys_id of "user 1" here, to insert group member
15+
rec1.insert();
16+
gs.log("User group record inserted");
17+
}
18+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
**Script to Add a User to list of User Groups Where They Are Not Already a Member**
2+
3+
Purpose: we often recive requests add a single group member to multiple groups. that's a manual task. this script makes it very simple.
4+
5+
- **The first GlideRecord('sys_user_group')**: Creates a GlideRecord instance to query the `sys_user_group` table.
6+
7+
- **addEncodedQuery('')**: Add the required list of groups here, by copying filter from list of groups.
8+
9+
- We use while loop to loop through list of groups.
10+
11+
- **The second GlideRecord('sys_user_grmember')**: For each group, creates a new GlideRecord instance to query the `sys_user_grmember` table (user-group memberships).
12+
13+
- **addQuery('group', rec.sys_id)**: Filters the `sys_user_grmember` records by the current group’s `sys_id`.
14+
15+
- **addQuery('user', '7279f455939e71944c77b6b5fbba1033')**: Filters the records for the specific user's `sys_id` ( this is sample sys id. replace with the actual `sys_id` of the user).

0 commit comments

Comments
 (0)