ServiceNow CAD – GlideForm & GlideUser Part 2
- GlideUser Introduction
Hello and welcome back. In this video, we’ll take a look at the Glide user API. So the ServiceNow docs state the Glide User API provides access to information about the current user and current user roles. Using the Glide user API avoids the need to use the slower Glide record queries to get user information. So as the developer, we’d use the Glide user API on the client side to retrieve user information. We can reference Glide user by g underscore user. And it’s a relatively small and simple API. We’ll see here in just a moment that there’s really only a handful of methods and properties on this API. All right, now let’s take a look at the Show me the code slide.
So in this example, we would like to check if the current user has the ITIL rule. So on line one, we can see that we create a new variable, has ITIL, and we assign it the value returned to us from the has role method. In this case, it accepts a string argument which is simply the name of the role. In this case, ITIL and it will return a boolean value either true or false if the current user has the given role. So on line two, we’re saying if has ITIL is false, we want to alert the message you do not have sufficient privileges. All right, now let’s take a look at the Glide User API.
Overview. So again, we can access the Glide user API by using g underscore user anywhere on the client side and some of the properties that this API contains our first name, last name, user ID and Username or the current user. And as far as methods, we have Has Role, has roll exactly, has Roll from List and a few others. All right, now let’s quickly go through these one by one. So first name, last name, User ID and Username are simply properties on the G user object.
So they just hold the value of the current user’s information. In this case their first name, their last name, their user ID and their username. All right, then we have the Get client data method which will return to us a value that was set using the Put client data method. We also have the Get full name method which will return to us the current user’s full name. So their first and last name. And then we have some roles methods. So we have Has Role, which accepts one argument, simply the name of the role and returns to us a boolean value. True or false? If the user has that role or not, then we have Has Role exactly, which is similar to has role, but it tests whether the user has the explicit role or if it was inherited, it would return false.
So oftentimes roles can be inherited from other roles, while the has role exactly method would check if the user has been explicitly assigned a specific role. Next we have has roll from list. So we can actually provide a list of roles to this method, which is just a comma separated string. And finally, we have the has roles method, which simply tests whether or not the current user has any roles at all and will return a boolean value of whether or not they have any roles. All right, so let’s take a deeper look at the has roll exactly method. So again, it takes the name of the role as its argument and returns true only if the user has the specified role, even if they’re an admin.
So in this case, because I am an admin, in my personal instance, I automatically receive the approval admin role. So the has role on line one returns true. So on line two, when we use the has role exactly, it’s testing whether I’ve been explicitly assigned this role or if I’ve inherited it from another role. So in this case, because I haven’t been explicitly assigned this role, it actually returns false here. So this shows the difference in using Has Roll and has roll exactly. All right.
And finally we’ll take a deeper look at the Glide user properties. So the Glide user API is a little different from the rest because it actually has documented API properties, because they are simply properties on the object. We don’t need any getter methods to access them. We can access them by just simply using g user dot and then the name of the property. So for example, we could log the user’s first and last name by doing g user first name plus a space plus G user last name. And online too, we log the user ID using G user user ID. Line three, the username g user username, line four, the departmentg user department ID. And finally, on line five, their roles using G user roles.
- Demo: GlideUser
Hello and welcome back. In this video, we’ll jump into service now and take a deeper look at the Glide user API. So, as I stated in the Glide form API video, one of the cool things about client side APIs is we can actually log them in the browser’s web console. So if we’ve bring up your browser’s console, we can do a console dir and pass in the g user API. And here we get a formatted list of the object so all of its properties and methods. We can see that we have an all roles array, which is just an array of the roles that the current user has.
We have a client data object as well as some properties department ID, first name, full name, last name, and some additional methods and properties. So if we open up the proto object, we can see here the methods associated with this API. So get full name, get roles, get username, has role, has roll, exactly, et cetera. All right, so to start with, we’ll take a look at the first name, last name and user ID properties.
We’ll do a simple alert and we’ll just say hello g user first name plus a space plus g user last name plus the string your user ID plus g user user ID. So if we copy and paste this in the JavaScript executor again, we can access this on a Mac using Control Shift j and paste that in and run the code, we can see that the alert box says hello. Mark Miller your user ID is and then the SysID of my user. All right, so instead of accessing two properties, the first name and last name, we can use a shortcut by using the git full name method. So here we’ll just do hello and then g user git full name, we’ll copy and paste that and we can see here that it says hello.
Mark Miller all right, now let’s take a look at the has roles method. So we’ll just do a simple alert do you have any roles? And then log g user has roles. Again, this method just returns a boolean value if the current user has any roles or not. And we see true here because I do in fact have roles. All right, now let’s take a look at the has roll method. So for this we’ll do an alert do you have the ITIL role? And we’ll do g user has roll and pass in the string value of the role name, which is just ITIL copy and paste that. And we can see here that it says true because I do in fact have the ITIL role. And finally we’ll take a look at the username property. So we’ll just do an alert your username is and then g user username. So we can see here that my username is mark Miller within the system.
- Section Recap
Hello and welcome to the last video in section six. So where can we use the glide form and glide user API? Well, it should be no surprise that these are in fact clientside APIs and that we cannot access them on the server side. All right, so as developer will use the Glide Form API to access information or manipulate a form in service. Now, so if we’re on the incident form or problem form and we need to manipulate the data somehow, we would use this G form API.
If we would need to access information about the current user, we would use the Glide user API. Again, both Glide form and Glide user are client side APIs. And if we try to access them on the server side, we would get an undefined error. If you’d like to access the ServiceNow JavaScript executor on the client side, use Control plus Shift plus J and it will pop up dialog box within your browser.