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

About User Criteria's Advanced Script

Kurumi
Tera Contributor

In the Advanced of user criteria, I want to set the condition that a specific word is included in the department to which the user belongs. Please tell me about the code written in the script.

1 ACCEPTED SOLUTION

Mohit Kaushik
Mega Sage
Mega Sage

Hi Kurumi,

You can use the below code to make it work.

var loggedInUser = new GlideRecord('sys_user');
loggedInUser.get(gs.getUserID());
if(loggedInUser.department.getDisplayValue().includes('specific word you want to check')){
    answer = true;
}

 

Please mark this as correct and helpful if it resolved the query or lead you in right direction.

Thanks,
Mohit Kaushik
Community Rising Star 2022

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)

View solution in original post

4 REPLIES 4

ersureshbe
Giga Sage
Giga Sage

Hi,

can you  try the below script:

var dept;
var user = new GlideRecord('sys_user');
//user.get(gs.getUserID());
//gs.log('ID:'+gs.getUserID());
user.addQuery('sys_id',gs.getUserID());
user.query();
if(user.next()){
    dept = user.department;
    gs.log('Dept:'+dept.getDisplayValue());
    if(user.department.getDisplayValue().indexOf("Finance")>-1){ // Add the departname contains
         gs.log('Success');
         return true;
    }else{
        return false
    }
}
 
Regards,
Suresh.

 

Regards,
Suresh.

Mohit Kaushik
Mega Sage
Mega Sage

Hi Kurumi,

You can use the below code to make it work.

var loggedInUser = new GlideRecord('sys_user');
loggedInUser.get(gs.getUserID());
if(loggedInUser.department.getDisplayValue().includes('specific word you want to check')){
    answer = true;
}

 

Please mark this as correct and helpful if it resolved the query or lead you in right direction.

Thanks,
Mohit Kaushik
Community Rising Star 2022

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)

I encountered an issue I feel like is important to be aware of, and it is related to the script you wrote here.
Using gs.getUserID() checks the session for the logged in user. If for some reason the logged in user has to run a check against another user, the criteria will still check the logged in user. 
Docs page:
https://docs.servicenow.com/en-US/bundle/utah-servicenow-platform/page/product/service-catalog-manag...

Explicitly states "

  • Do not use gs.getUser() or other session APIs since they cause conflict when used in diagnostic tools. Use the pre-defined user_id variable available in the script to get the user id of the user being used to evaluate the script.

"

Hi @Joshua Draper,

Thanks for noticing and bringing that out. I always appreciate if someone finds mistake in my code and that can be corrected. I agree with you this code can be better by using the user_id variable.

 

 

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)