logo

NJP

Script to Add 5 Users to 5 Different Groups | Background Scripts | Server-Side Scripts

New article articles in ServiceNow Community · Oct 28, 2024 · article

Hello Everyone,

Use Case Scenarios for requirement fulfillment in our day-to-day development activities.

Server-Side Scripts Used for Demo Purpose -

var users = ['7d9a8d6ac3b85a10440478edd401318c', 'ddaa05eac3b85a10440478edd40131da', 'aaaac9aec3b85a10440478edd40131c4', '37aa8daec3b85a10440478edd4013182', '01ba0daec3b85a10440478edd4013143']; // You can add any number of user arrays in my case I have used 5, you can pass dynamic array list here

var groups = ['aaba01eec3b85a10440478edd4013141', '0dcacdaec3b85a10440478edd4013131', '56ca8daac3b85a10440478edd401314a', '23cacdaec3b85a10440478edd4013101', 'b1da01eec3b85a10440478edd4013148']; // You can add any number of Group arrays in my case I have used 5, you can pass dynamic array list here

for (var i = 0; i less than groups.length; i++) {

var groupSysId = groups[i];

for (var j = 0; j less than users.length; j++) {

var userSysId = users[j];

var grMember = new GlideRecord('sys_user_grmember');

grMember.initialize();

grMember.group = groupSysId;

grMember.user = userSysId;

grMember.insert();

gs.info('Added User ' + userSysId + 'to group ' + groupSysId);

}

}

For the Demo please have a look at the following video -

Best Regards,

Prashant Kumar (LearnIT)

YouTube Channel LearnIT: https://www.youtube.com/@learnitwithprashant

Blog LearnIT: https://medium.com/@LearnITbyPrashant

Prashant Kumar LinkedIn: https://www.linkedin.com/in/learnitbyprashant/

ServiceNow Community Prashant Kumar - https://www.servicenow.com/community/user/viewprofilepage/user-id/19635

View original source

https://www.servicenow.com/community/developer-blog/script-to-add-5-users-to-5-different-groups-background-scripts/ba-p/3084879