2

Based on the official wiki page, a User Criteria record may contain a specific set of users, groups, roles, departments ....

My question is, do I have to do manual checking for the user against user criteria, something like:

  1. check if the user exists in user criteria users list
  2. then if the previous test failed, check if the user role exists in user criteria roles list
  3. then if the previous test failed, check for departments .... and so on

Or is there something out of the box in snow that carries out these checks for me? I guess there should be some way but I can't find any thing relevant in the official wiki or docs

Community
  • 1
  • 1
Abdo Adel
  • 1,177
  • 2
  • 17
  • 30

3 Answers3

1

So there is a built-in function that gets ids of all user criteria records available to the current logged in user:

SNC.UserCriteriaLoader.getAllUserCriteria()

the above function returns object of type ArrayList<String>, so you can check if the target user criteria exists in this object through: SNC.UserCriteriaLoader.getAllUserCriteria().contains(user_criteria_sys_id)

This solved my problem, how ever I still don't know if there is some general method that accepts user id as a parameter

Abdo Adel
  • 1,177
  • 2
  • 17
  • 30
  • thanks, I searched so long for this. How did you get that info? I didn't found any documentation on this ... – makim Jun 08 '17 at 13:21
  • hi @makim , I was lucky enough that chuck tomasi saw my question on the official forum and answered it, here is the link https://community.servicenow.com/thread/246758 – Abdo Adel Jun 08 '17 at 19:45
0

I believe what you're looking for is the Match All checkbox, take a look at the documentation at http://wiki.servicenow.com/index.php?title=User_Criteria#Matching_All

If you un-check this box, it checks that a user matches any section of the criteria.

If you check this box, it requires all criteria to be matched.

Example 1 - Match All is un-checked

enter image description here

In this example Match All is un-checked and if a user meets any of the the following criteria they are part of this User Criteria.

User in role: Asset
or
User in company: ACME EMEA
or
User in department: Development

Example 2 - Match All is checked

enter image description here

In this example Match All is checked and a user must meet all of the the following criteria they are part of this User Criteria.

User in role: Asset
and
User in company: ACME EMEA
and
User in department: Development
Kirk
  • 16,182
  • 20
  • 80
  • 112
  • Thanks for your response, but I was asking if there already exists a programmatic way/function to evaluate if some user belong to some user criteria, for example sth like `checkUserCriteria(userId, userCriteriaId)`, this function would check for that checkbox you mentioned and the other fields (roles, deps, groups) and returns me a boolean, I found a one BTW `SNC.UserCriteriaLoader.getAllUserCriteria()` which returns an array of ids of user criterias available to the current logged in user – Abdo Adel Dec 13 '16 at 10:52
  • What are you attempting to do with this, it seems the user criteria only applies to an existing session rather than acting as membership in it. Your only option would be do the manual checking in your code such as in your question. – Kirk Dec 13 '16 at 21:23
  • actually `SNC.UserCriteriaLoader.getAllUserCriteria()` was enough for me but I was aiming into writing a general one that accepts a user id as an input, but looks like there does not exist similar thing supported in snow – Abdo Adel Dec 14 '16 at 11:58
  • 1
    If you came up with something, put an answer in. It may help someone else – Kirk Dec 14 '16 at 22:54
0

Apart from SNC.UserCriteriaLoader.getAllUserCriteria() there is another more generic method:

SNC.UserCriteriaLoader.getUserCriteria(userId)

It seems to return list of matching User Criteria (ArrayList<String>) for the specified user. E.g.

SNC.UserCriteriaLoader.getUserCriteria(gs.getUserID())

and

SNC.UserCriteriaLoader.getAllUserCriteria()

return the same result.

Iurii R
  • 21
  • 4
  • hi lurii, this method wasn't available at the time when I asked the question, I'll test it on Jakarta version and give you feedback, thanks – Abdo Adel Mar 06 '18 at 09:10
  • it doesn't work on Jakarta or Istanbul :( it gives me this error message when I try it from a fix script "JoinQuery invalid field name: user_criteria" – Abdo Adel Mar 06 '18 at 10:54
  • @AbdoAdel I tested it and you were right, it doesn't work if you use it inline. However, it should work fine if you break it into 2 statements: var userId = gs.getUserId() and SNC.UserCriteriaLoader.getUserCriteria(userId). Please let me know if this works for you and I'll update my answer – Iurii R Mar 10 '18 at 09:32
  • Please disregard my previous comment. I keep playing with this method and see that it doesn't produce necessary results and is not stable. I'll write another comment if I figure something. – Iurii R Mar 10 '18 at 09:47