Need find user's with role (itil) via script

Pastupe
Mega Guru

Hello,
I cannot find here how to get list of user's from database which have ITIL role.

I have seen element g_user.hasRole , but this work only on Client side.
I need find way how to build script with GlideRecord to be able query sys_user table via Script Background

I have my script below.
Can someone experienced in script's help please.
I tried several examples with gs...... but nothing work and I cannot find how to include query in C variable that user have itil role.
I tried add query in below script as------- c.hasRole('itil'); -------c.addQuery('sys_user','hasRole','itil');

I very appreciate whichever help and advice
Thank you
Petr



var c = new GlideRecord('sys_user');
c.addQuery('first_name','CONTAINS','Pe');
c.orderBy('fist_name');
c.query();
while (c.next()) {
gs.print(c.email)
}

6 REPLIES 6

michael_baker
Tera Guru

Petr,

To accomplish this you will need to query the 'sys_user_has_role' table. I have provided a sample code below:



var grUserRoles = new GlideRecord('sys_user_has_role');
grUserRoles.addQuery('role', 'f07c4745f50b4100217b7c462590361d'); // itil
grUserRoles.addQuery('user.first_name','CONTAINS','Pe');
grUserRoles.orderBy('user.first_name');
grUserRoles.query();
while(grUserRoles.next()) {
gs.print(grUserRoles.user.email)
}

Hope this helps!


Hi Michael!

yes, it work great. I tried in the same way you proposed and query sys_user_has_role table.
Thank you for that.

I have received via private message from mukesh.kumar@eworks.in also another proposal i have already tested and it also work.
If you want try , please use my original script and add query ----- c.addQuery('roles=itil');

full script is

var gr = new GlideRecord("sys_user");
gr.addQuery('first_name','CONTAINS','Pe');
gr.orderBy('fist_name');
gr.addQuery('roles=itil');
gr.query();
while(gr.next()) {
gs.print(gr.email);
}

Mukes, thank you too also for this proposal.

Petr


ravi1_tandon
Kilo Guru

you don't even need to write the script. Just create a report on sys_user table and add the following filter. You can also use in sys_user list view as well

Filter to be used:
sys_id is javascript:getRoledUsers('IN','itil').

Regards

Ravi Tandon


Hello,
thank you for your feedback.
Reason why I need it via script is, that I need to schedule job, which create for me every Month Incident assigned to me with attachment and in attachment will be list of user's which have ITIL role and were not log in the tool over 3 month's.

Main Target was automatically create incident with CSV file. Now i have the part of script how get user role's via script and it work for me fine.

Just to mention, I'm new in javascript (6 month's experience only) and try to learn as much as I can.

Also thank you for your contribution.
Petr