logo

NJP

Learn MORE JavaScript on the ServiceNow Platform: Lesson 11 Exercise - GlideUser

Import · Dec 12, 2023 · video

Hey. Welcome back. It's been a couple of lessons
since we had an exercise, so let's go ahead and do this really quick
exercise. I want you to try to create a script
that checks that the current user has a specific role of admin
and log a message accordingly. That's it. Really simple. Hit pause now, and we'll see you in 5
seconds or however long it takes you to. I'll see you in a little bit and we're back. Okay. It should be pretty straightforward. I created a user object and I called it currentIser and I added gs.getUser()
because you've got to make sure you always do that. And then I just use the hasRole()
method to check if they had admin. So if current user has role of admin, print the statement
and if they don't, print this other statement saying they don't have it. So let's go ahead and take that code
and I'm going to jump into service now it's into my background
script and press run and current user is an admin
because I am logged in as admin. So before we end this exercise,
one thing you need to know though is that if you are logged in as admin, you are
considered to always have all roles and so it will always return true
if you're running this for admin. So if you impersonate
another user that doesn't have admin, then it will properly do a check
if you put in a different role. So just be aware that if you are logged in as an admin you may always get true. Okay. Cool. Bye!

View original source

https://www.youtube.com/watch?v=MWllMbDjSV8