Skip to main content

Set up a bunch of test users

 via fix script


Users – 10 in total

NAME

USER ID

test USER1

test_user1

test USER2

test_user2

test USER3

test_user3

test USER4

test_user4

test USER5

test_user5

test USER6

test_user6

test USER7

test_user7

test USER8

test_user8

test USER9

test_user9

test USER10

test_user10

Groups – 2 in total

 

TestGrp1

TestGrp2

 

Group memberships

All above users added to each group

 

Group roles – 2 in total

itil

agent_workspace_user

 

Stand alone user roles – 1 in total

 

Catalog (added to all above users)

 

Last login default

 

'2020-01-01';

Last login time default

'2020-01-01 15:47:31';

 

Fix script name “set up test users"

To be run once only

Script code:

 

var lastLogin = '2020-01-01';

var lastLoginTime = '2020-01-01 15:47:31';

setUpUsers(lastLogin, lastLoginTime);

setUpGroups();

AddRolesToGroups();

AddStandaloneRolesToUsers();

AddUsersToGroups();

 

function setUpUsers(lastLogin, lastLoginTime) {

    setUpUser('test', 'TEST1', lastLogin, lastLoginTime);

    setUpUser('test', 'TEST2', lastLogin, lastLoginTime);

    setUpUser('test', 'TEST3', lastLogin, lastLoginTime);

    setUpUser('test', 'TEST4', lastLogin, lastLoginTime);

    setUpUser('test', 'TEST5', lastLogin, lastLoginTime);

    setUpUser('test', 'TEST6', lastLogin, lastLoginTime);

    setUpUser('test', 'TEST7', lastLogin, lastLoginTime);

    setUpUser('test', 'TEST8', lastLogin, lastLoginTime);

    setUpUser('test', 'TEST9', lastLogin, lastLoginTime);

    setUpUser('test', 'TEST10', lastLogin, lastLoginTime);

}

 

function setUpUser(firstName, lastName, lastLogin, lastLoginTime) {

    var grUser = new GlideRecord('sys_user');

    grUser.newRecord();

    grUser.first_name = firstName;

    grUser.last_name = lastName;

    grUser.last_login_time = lastLoginTime;

    grUser.last_login = lastLogin;

//grUser.password.setDisplayValue('xxxxx');

    

var combinedNames = firstName + '_' + lastName;

    grUser.user_name = combinedNames.toLowerCase();

    grUser.insert();

}

 

function setUpGroups() {

    setUpGroup('TestGrp1');

    setUpGroup('TestGrp2');

}

 

function setUpGroup(groupName) {

    var grGroup = new GlideRecord('sys_user_group');

    grGroup.newRecord();

    grGroup.name = groupName;

    grGroup.insert();

}

 

function AddRolesToGroups() {

    AddRoleToGroup('TestGrp1', 'itil');

    AddRoleToGroup('TestGrp1', 'agent_workspace_user');

    AddRoleToGroup('TestGrp2', 'itil');

    AddRoleToGroup('TestGrp2', 'agent_workspace_user');

}

 

function AddRoleToGroup(groupName, groupRole) {

    var grGrpRole = new GlideRecord('sys_group_has_role');

    grGrpRole.newRecord();

    grGrpRole.setDisplayValue('group', groupName);

    grGrpRole.setDisplayValue('role', groupRole);

    grGrpRole.insert();

}

 

function AddStandaloneRolesToUsers() {

    var grUsers = new GlideRecord('sys_user');

    var sQuery = 'last_nameTEST';

    grUsers.addQuery(sQuery);

    grUsers.query();

    while (grUsers.next()) {

        AddStandaloneRoleToUser(grUsers.sys_id, 'catalog');

        AddStandaloneRoleToUser(grUsers.sys_id, 'catalog');

    }

}

 

function AddStandaloneRoleToUser(userSysID, roleName) {

    var grUserRole = new GlideRecord('sys_user_has_role');

    grUserRole.newRecord();

    grUserRole.user = userSysID;

    grUserRole.setDisplayValue('role', roleName);

    grUserRole.insert();

}

 

function AddUsersToGroups() {

    var grUsers = new GlideRecord('sys_user');

    var sQuery = 'last_nameLIKETEST';

    grUsers.addQuery(sQuery);

    grUsers.query();

    while (grUsers.next()) {

        AddUserToGroup(grUsers.sys_id, 'TestGrp1');

        AddUserToGroup(grUsers.sys_id, 'TestGrp2');

    }

}

 

function AddUserToGroup(userSysID, groupName) {

    var grGrpRole = new GlideRecord('sys_user_grmember');

    grGrpRole.newRecord();

    grGrpRole.user = userSysID;

    grGrpRole.setDisplayValue('group', groupName);

    grGrpRole.insert();

}


Comments

Popular posts from this blog

URL link in addInfoMessage

var ga=new GlideAjax('gld_HR_ajax'); ga.addParam('sysparm_name', 'checkEmployeeNumber_hrProfile'); ga.addParam('sysparm_hrprofilenumber', g_form.getValue('number')); ga.addParam('sysparm_employeenumber', newValue); ga.getXMLAnswer(function(answer) { if (answer!='undefined' && answer!=''){ var navURL="<a style='text-decoration:underline;color:blue' href=hr_profile.do?sysparm_query=number=" + answer + ">" + answer + "</a><img width='3' src='images/s.gif'/>"; var sMsg='The employee number entered already exists on another HR Profile ' + navURL; //alert(sMsg); g_form.showErrorBox('employee_number', 'error - please check'); g_form.addInfoMessage(sMsg); } });

GlideRecord setValue

setValue(String name, Object value) Sets the specified field to the specified value. Normally a script would do a direct assignment, for example,  gr.category = value . However, if in a script the element name is a variable, then  gr.setValue(elementName, value)  can be used. When setting a value, ensure the data type of the field matches the data type of the value you enter. This method cannot be used on journal fields. If the value parameter is null, the record is not updated, and an error is not thrown https://developer.servicenow.com/app.do#!/api_doc?v=madrid&id=r_GlideRecord-setValue_String_Object