gs.getUser in background scripts

Khanna Ji
Tera Guru

Hi All,

How can I execute below script in background script for one of the user?

gs.getUser().isMemberOf('abc')

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@ServiceNow User 

Like this

Pass the user name or user sys_id

var username = 'abel.tuter';

var isMember = gs.getUser().getUserByID(username).isMemberOf('abc');

gs.info(isMember);

Regards
Ankur

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

View solution in original post

6 REPLIES 6

DirkRedeker
Mega Sage

Hi

the "gs.getUser()" function is intended to act on the CURRENT user. That is the user logged in and calling this function. see:

https://docs.servicenow.com/bundle/paris-application-development/page/script/server-scripting/task/t...

If you want to check the membership with "isMemberOf()" of any particular user for a given group, you should use the option, that Ankur proposed.

If you want to know, where you can find the assignments of users to Groups, you can have a look to the database table [sys_user_grmember], which you can show by typing "sys_user_grmember.list" to your Application Navigator in the Classic UI and pressing <ENTER>.

Effectively, the "isMemberOf()" API ready that table data.

Note, that the "gs.getUser().getUserByID(username)" function call does not work in Scoped Apps, it only works in Global Scope.

 

Let me know if that answers your question and mark my answer as correct and helpful.

BR

Dirk

 

 

Thank you Dirk, for the response. Marked your answer as helpful.