- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 10-30-2021 07:14 AM
Dear community members,
I surely like the simulator concept, but in the HAM simulator you have to manually add a bunch of users and for such repetitive tasks I like to learn how to do it in a script.
I created a little script where you just replace a placeholder with your first name. It is based on other community entries of Mark Roetthoff and Tushkar Walvekar and I like to thank them for sharing. It created after replacing the placeholder all desired users correctly. So if you want to save you a little time mark it helpful, please. I ran it as a background script.
I also attached the source with the complete code. You might want to rename it from .txt extension back to .js. so that the editor of your choice recognizes it as a javascript and it contains all users needed while the code example below just shows it for the first one. Then replace the placeholder <your first name> with your first name copy it to background scripts and execute it.
I am aware this is not a high class example ... in the next step i would like to create a json payload from the needed data and create a reusable function to implement the payload into user and has_role tables.
Hope you like the idea of sharing.
PS: if you wonder what a Simulator is, you can follow this link to a 6 minute intro video from ServiceNow.
/*
ADMIN
User ID: admin.<your first name>
First Name: Admin
Last name: <your first name>
Email: <user id>@example.com
Role(s): admin
*/
//Create User
var grUser = new GlideRecord('sys_user');
grUser.initialize();
grUser.setValue('user_name', 'admin.<your first name>');
grUser.setValue('first_name', 'Admin');
grUser.setValue('last_name', '<your first name>');
grUser.setValue('email', 'admin.<your first name>@example.com');
grUser.setValue('preferred_language', 'en');
grUser.setValue('time_zone', 'Europe/Amsterdam');
grUser.setValue('date_format', 'dd-MM-yyyy');
grUser.setValue('sys_created_on', '1983-05-01 12:30:00');
grUser.setNewGuidValue('admin.<your first name>');
grUser.autoSysFields();
grUser.insert();
//Declare roles in array
//var desiredRoles = ["role1","role2","role3"];
var desiredRoles = ["admin"];
//Assign the roles
var grRole = new GlideRecord('sys_user_has_role');
for(var i =0; i<desiredRoles.length; i++){
grRole.initialize();
grRole.user = grUser.sys_id;
grRole.setDisplayValue('role',desiredRoles[i]);
grRole.insert();
}
// removed duplicate code, download attachment to get the complete
// user implementation code for the simulator as in the first task