The CreatorCon Call for Content is officially open! Get started here.

how to add specific role to all users with itil role

Alon Grod
Tera Expert

Hi,

im trying to add to all users with 'itil' role a specific role (template_editor_group) but its not working. what am I doing wrong?

WhatsApp Image 2023-08-31 at 13.09.54.jpeg

WhatsApp Image 2023-08-31 at 13.08.26.jpeg

WhatsApp Image 2023-08-31 at 13.08.18.jpeg

1 ACCEPTED SOLUTION

Mohith Devatte
Tera Sage
Tera Sage

Hello @Alon Grod ,

please try with below script 

var gr = new GlideRecord('sys_user_has_role');
gr.addEncodedQuery('role=282bf1fac6112285017366cb5f867469');
gr.query();
while(gr.next()){

    var role = 'f581eaad0a0a0bc500fbf9055d015838';
    var  spRole = new GlideRecord('sys_user_has_role');
    spRole.initialize();
    spRole.user = gr.getValue('user');
    spRole.role = role;
    spRole.insert();
}

 

Hope this helps 

Mark the answer correct if this helps you 

Thanks

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@Alon Grod 

Did you try running the script for limited users?

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

Mohan raj
Mega Sage

Hi @Alon Grod,

 

Try below code its working for me 

Mohanraj_0-1693477371587.png

var gr = new GlideRecord('sys_user_has_role');
gr.addEncodedQuery('role!=f581eaad0a0a0bc500fbf9055d015838^ORrole=282bf1fac6112285017366cb5f867469');
//gr.setLimit(5);
gr.query();
while(gr.next()){
    var role = 'f581eaad0a0a0bc500fbf9055d015838';
    var gr1 = new GlideRecord('sys_user_has_role');
    gr1.initialize();
    gr1.user = gr.getValue('user');
    gr1.role = role;
    gr1.insert();
}

 

If my response helps you to resolve the issue close the question by Accepting solution and hit thumb icon. From Correct answers others will get benefited in future.

Regards,

T Mohan.

Mohith Devatte
Tera Sage
Tera Sage

Hello @Alon Grod ,

please try with below script 

var gr = new GlideRecord('sys_user_has_role');
gr.addEncodedQuery('role=282bf1fac6112285017366cb5f867469');
gr.query();
while(gr.next()){

    var role = 'f581eaad0a0a0bc500fbf9055d015838';
    var  spRole = new GlideRecord('sys_user_has_role');
    spRole.initialize();
    spRole.user = gr.getValue('user');
    spRole.role = role;
    spRole.insert();
}

 

Hope this helps 

Mark the answer correct if this helps you 

Thanks