script

AnanyaT
Tera Contributor

(function run() {
// Step 1: Greet the user and ask for their email.
var email = prompt("Hello! I can help you assign roles. Please provide the email ID of the user.");

// Step 2: Check if the email is valid.
if (!email) {
return "You did not provide an email address. Please provide a valid email ID.";
}

// Step 3: Ask the user for the role they need to assign.
var role = prompt("What role would you like to assign to the user?");

// Step 4: Check if the role is valid.
if (!role) {
return "You did not provide a role. Please provide a valid role name.";
}

// Step 5: Search for the user by email in the sys_user table.
var userGr = new GlideRecord('sys_user');
userGr.addQuery('email', email);
userGr.query();

// Step 6: Check if the user exists.
if (!userGr.next()) {
return "Sorry, I couldn't find a user with that email address. Please check the email and try again.";
}

// Step 7: Check if the role exists in sys_user_role_list.
var roleGr = new GlideRecord('sys_user_role');
roleGr.addQuery('name', role);
roleGr.query();

if (!roleGr.next()) {
return "Sorry, I couldn't find the role '" + role + "'. Please provide a valid role.";
}

// Step 8: Assign the role to the user.
var userRoleGr = new GlideRecord('sys_user_roles');
userRoleGr.initialize();
userRoleGr.user = userGr.sys_id; // Assign the user sys_id
userRoleGr.role = roleGr.sys_id; // Assign the role sys_id
userRoleGr.insert(); // Insert the record into the sys_user_roles table

// Step 9: Return success message to the user.
return "The role '" + role + "' has been successfully assigned to " + userGr.name + " with email " + email + ".";
})();



But it is giving this message as visible in the screenshot. So what will be the correct script code.

It is for is designed to assist in assigning roles to users. It greets the user, asks for their email ID and the role they need to provide, extracts the role from the sys_user_role_list, updates the user's role in the sys_user table, and informs the user once the process is completed. The intended audience for this agent is ServiceNow users who need to assign roles to other users.

 

3 REPLIES 3

Shivalika
Mega Sage

Hello @AnanyaT 

 

The table in which you can assign role to user is "sys_user_has_role" table and not "sys_user_roles" 

 

Correct this and let me know the updates. 

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY

 

AnanyaT_2-1744090187810.png

 

giving this error

 

 

 
 

 

 

Ankur Bawiskar
Tera Patron
Tera Patron

@AnanyaT 

As shared by @Shivalika please correct the table name and it should work

this line

var userRoleGr = new GlideRecord('sys_user_has_role');

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader