I have a excel file with the below info :
User name | Group Name | User id |
---|---|---|
User 1 | Group 1 | 1 |
User 2 | Group 2 | 2 |
User 3 | Group 3 | 3 |
all the listed "users" and "group" are present in the user table and group table, ex(User1, user2, user3 are there in sys_user table and group1, group2 , group3 are present in sys_user_group)
i just want to add the user to the corresponding group mention in the excel.
Can anyone suggest how to automate this in service now by any means because the excel file contains larger no of data and manually it is not possible.
You can use an Import Set and add records to the sys_user_grmember
table.
There are two fields to set on the insert
Update @Joey mentioned the Transform Set that is part of an overall Import Set, take a look at the details for your version. For example, Istanbul https://docs.servicenow.com/bundle/istanbul-servicenow-platform/page/script/server-scripting/task/t_CreateATransformMap.html
Essentially this is used by the Import Set to map the spreadsheet to the fields in ServiceNow.
In your case it's slightly more involved since you'll need to get the sys_id
for the user and the group, while it sounds like you have the usernames and the group names. Using the Script
section of the Transform Map should work in your case.
Something along the code below would be a general idea. However I haven't tested this whatsoever and you'd need to test thoroughly in a development environment to get it working.
Here source.u_user_name
and source.u_group_name
are the fields from your loaded data in the Import Set
(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
// get the user
var usr = new GlideRecord('sys_user');
usr.get('user_name',source.u_user_name.toString());
// get the group
var grp = new GlideRecord('sys_group');
grp.get('name',source.u_group_name.toString());
// insert group membership
var grMember = new GlideRecord('sys_user_grmember');
grMember.initialize();
grMember.user = usr.sys_id;
grMember.group = grp.sys_id;
grMember.insert();
})(source, map, log, target);