How to get the Title of logged in User in Business rule

Rakesh11
Tera Contributor

Working on User Criteria, need to show the catalog item to the user whose title is in some specific values.
We have create a property to hold those title values.

Now we are trying to check whether user is having the title or not. Here in script it is always returning true.Below is the script:

 

Rakesh11_0-1697554916718.png

 

3 REPLIES 3

RaghavSh
Kilo Patron

gs.getUser().title or gs.getUser.getTitle() will not work.

 

To get title user:

var usr = new GlideRecord('sys_user');

usr.get(gs.getUserID());

var Usertitle = usr.title.toString();

 

You can now use Usertitle for your other operations like uppercase and indexOf


Please mark the answer correct/helpful accordingly.


Raghav
MVP 2023

@Rakesh11 did this work for you?


Raghav
MVP 2023

Vishal Birajdar
Giga Sage

Hi @Rakesh11 

 

In user criteria script you should use "user_id" & not gs.getUser()

 

 

/** Scripted User Criteria is not cached, and evaluated everytime, so performance is dependent on the script.
* Populate `answer` with true/false or evaluate to true/false
* The script is evaluated in the scope the user criteria is defined
* Don't use `current` in the script or populate the variable
* Don't use `gs.getUser()` or  `gs.getUserID()`, 
* instead use `user_id` which contains the user sys_id against whom the evaluation is happening.
*/

/*1. Decalre variable to store user title */
var userTitle;
/*2. Glide record on user table */
var grUser = new GlideRecord('sys_user');
grUser.addQuery('sys_id',user_id);   //use user_id variable  to get logged-in user sys_id
grUser.query();
if(grUser.next()){
    userTitle = grUser.getValue('title');  //we will get the title of logged in user
}

/*3. Use your logic below to check the user title we got from above with system property */

 

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates