- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-03-2022 01:48 AM
I am trying to add a role to a specific group of users. These users all need to be active, and part of a specific company. The script is not erroring, but it's also not adding the role. What am I doing wrong? Another similar script executed earlier on the Contacts table worked well.
var gr = new GlideRecord("sys_user");
gr.addQuery('active', true);
gr.addQuery('company', 'CONTAINS', 'companyname'); //we have multiple companies, but all contain a specific word.
gr.query();
while(gr.next()) {
var role = new GlideRecord('sys_user_has_role');
role.addQuery('user',gr.sys_id);
role.addQuery('role', '7fcaa702933002009c8579b4f47ffbde');
role.query();
if(!role.next())
{
role.initialize();
role.user = gr.sys_id;
role.role = "7fcaa702933002009c8579b4f47ffbde";
role.insert();
}
}
Thank you in advance for the help!
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-03-2022 02:03 AM
I made some corrections. Please check now.
var gr = new GlideRecord("sys_user");
gr.addQuery('active', 'true');
gr.addQuery('company', 'LIKE', 'companyname'); //we have multiple companies, but all contain a specific word.
gr.query();
while(gr.next()) {
var role = new GlideRecord('sys_user_has_role');
role.addQuery('user',gr.sys_id);
role.addQuery('role', '7fcaa702933002009c8579b4f47ffbde');
role.query();
if(!role.next())
{
role.initialize();
role.user = gr.sys_id;
role.role = "7fcaa702933002009c8579b4f47ffbde";
role.insert();
}
}
Feel free to mark correct and helpful.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-03-2022 02:03 AM
I made some corrections. Please check now.
var gr = new GlideRecord("sys_user");
gr.addQuery('active', 'true');
gr.addQuery('company', 'LIKE', 'companyname'); //we have multiple companies, but all contain a specific word.
gr.query();
while(gr.next()) {
var role = new GlideRecord('sys_user_has_role');
role.addQuery('user',gr.sys_id);
role.addQuery('role', '7fcaa702933002009c8579b4f47ffbde');
role.query();
if(!role.next())
{
role.initialize();
role.user = gr.sys_id;
role.role = "7fcaa702933002009c8579b4f47ffbde";
role.insert();
}
}
Feel free to mark correct and helpful.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-03-2022 02:15 AM
Hi,
Use below script its working for me
var CopmanyName = "ACME"; // you can add here diffrent names
var encodedQuery ='companyLIKE'+CopmanyName+'^active=true';
var gr = new GlideRecord('sys_user');
gr.addEncodedQuery(encodedQuery);
gr.query();
while(gr.next()) // you can test it by if first
{
gs.print("user is :" +gr.name);
var role = new GlideRecord('sys_user_has_role');
role.addQuery('user',gr.sys_id);
role.addQuery('role', '1ec244e67732101044703036971061ef');
role.query();
if(!role.next())
{
role.initialize();
role.user = gr.sys_id;
role.role = '1ec244e67732101044703036971061ef';
role.insert();
gs.print("Role inserted");
}
}
Result: (adding role to 1 user )
Kindly mark correct and helpful if applicable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-03-2022 02:21 AM
Hi Rahul, thanks for the reply. I found the answer already. I am trying to add role "snc_internal" and only now found out that it's not needed as it will be added automatically whenever someone logs on.
Will mark this as resolved, thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-03-2022 02:11 AM
Hi,
Just update your line gr.addEncoddedQuery(company.nameLIKEcompanyname);
role.addQuery('user', gr.sys_id()); with --> role.addQuery('user', gr.getUniqueValue());
role.user = gr.sys_id with ---> role.user = gr.getUniqueValue();
Thanks,
Sagar Pagar