Background script to add role to users with certain queries not working

Marissa N_
Kilo Expert

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!

1 ACCEPTED SOLUTION

RAHUL YADAV9
Mega Guru

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.

View solution in original post

7 REPLIES 7

RAHUL YADAV9
Mega Guru

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.

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 )

find_real_file.png

Kindly mark correct and helpful if applicable

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!

Sagar Pagar
Tera Patron

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

The world works with ServiceNow