What is Glide User API in ServiceNow | Journey with ServiceNow Scripting APIs| Methods of Glide User
[Music] please subscribe to my channel and click on the bell icon to get the regular updates of my channel and do not forget to like comment and share hello everyone welcome to sas with servicenow in this video you will learn about server side glide user api in servicenow and how can you use its different methods to achieve different functionalities in the platform as you know servicenow provides lot of different apis classes and methods which you can use in scripting in servicenow to achieve different functionalities or access different information of system glide user is one of the api which can be used in server side to access information about current user and its roles glide user api and methods minimize the use of glide record query on sys user table to access the information about logged in user glide user is used with glide system api which is used to access information about the system in order to access system information you use gs and then you use any method and in order to access logged in users information then you use gs.get user this basically gives you the reference to the user object for current logged-in user and glide user api also provides you methods by so that you can access information about the user like first name roles etc let me show you practical demo in my personal developer instance but before i show you the practical demo of glide user there is really one important point i want to inform you that is glide user is not just server side it can also be used in client-side code as well syntax is different methods are different but you can use glide user basically in server side and client-side both so let's go to my personal developer instance so this is my personal developer instance now what we will do we will talk about different methods provided by glide user api so that we can use those methods and achieve different functionalities so if i go to my slide and the first method is get company id now if you have any requirement in which you have to take further action on the basis of company of logged in user then you can use this method to get the society of company record of user and use that society to validate something so overall if you will use get company id method then you will get the logged in users company racket sus id let's say you want to add name of the company of logged in user in short description when new incident is created now this is just a example for demo but in your case you might get different requirements from your customers so in this video i will show you different methods of glide user and how can you access that information and what information you will get when you will you use those methods but if you will get different requirements so you have to see which method basically by using which method you will be able to achieve that particular feature or requirement or if you want to access that information maybe first name first name of logged in user maybe it's last name maybe the full name maybe the society or the company id for which we are just talking about so let's go to my personal developer instance and we are going to basically populate the company name so how exactly we will do that so we will go to incident basically application and here i will directly go to business rule because i want to populate that so what i will do i will go to business rule because i will write a server side code so i will click on new here so the requirement is that whenever new incident is created it should populate in short description it should populate the company name of the company of logged in user so i will just do demo for demo for company and i will just check this box advanced and here i will mention before install or maybe yeah let's let's do before insert is better maybe update as well if that is something we want uh then what we will do here in advanced we will go here now how can i access that user information basically the sys id so as we are talking about this get company id so i will just type here i will just store that value in a variable so that will be cmp id it is just a variable i'm creating maybe i will make it full screen so that you see the whole so i'm just making it big so here now here i will just write where cmp id and then i will do gs so i will start with gs so as i mentioned you that you have to access basically glide system you put dot now we have to get the reference of user object for that what i have to do i have to use get user method you will automatically get here you can see we're getting get user method i will press tab i will automatically access this get user method now we have to get the company id this one get company id how can i access this for that you have to press another dot period once you will do that it will show you all the methods available for user basically for this get user api glide user api so you can see we have first one as company id what it exactly does gets the company id of the current user that means the logged in user so i will just press tab and then i will press semicolon and then i will come here now we have to basically update the short description so in that case i will do current dot and i will put short description equal to now here what i can do current dot shot description plus um i can make it before because i have to add that company uh name but how can i do that because i am able to access now this one is this one is basically giving me just society how would i know the name of the company so for that maybe you can do a glide racket so what we will do we will come here where uh where gr equal to new glide record and here i can put basically the name of the company table so i will go to company i think this one will work so we have this so basically you have this name core company i will come here and i will just mention gr dot basically i can do add query and here we have underscore sys underscore sys id now i know the society what is that the society is basically this one cmp id i will copy this i will put it here not in quotations and that's it and then i will do gr dot gr dot query and then i will do if if i'm getting that basically company then gr dot next and then i will do if if i'm getting any any company that says id then it should update current just short description i will remove this now i want to add that name of the company before whatever short description i am putting myself as a user so in that case here you can put g r dot and you can put here i think it is name yes its name so i will just do name plus maybe i will add one thing just a pipe so that i can make the difference and here i can just do plus again and i can do current so i have current dot shot description and that's it and here i think i have mentioned everything current dot parsing unexpected token okay because i don't have this bracket here and it says okay i can just format it so now you can see i think we are good um in this case i will just do like this i will save this so this business rule is saved and i will just zoom out this screen so now i will create an incident so let's create now before creating let's see if i have a company assigned or maybe you should see what all users we have they have company because if if user will not have company record associated with this with the user's profile then you won't be able to see that so in that case maybe i will see acer i don't have any users you can see here or maybe i will just quickly go to users profile and we will impersonate um any any one user so i will do like this yep i have company i will click ok and yeah we do have company here so let's let's impersonate apple tutor so i will impersonate apple tutor so i have this apple tutor so this user already has this company so now i will go to incident and i will create a new incident so here i will put i think i don't need to put anything i think maybe i can just put mandatory fields i have like color so in color i can put adam bringle and here i can put demo demo for glide user and then i can just save this when i'm doing that absolutely you can see here it is automatically adding the basically name of the company of this user that is the logged in user i have logged in with this user i have impersonated this user and i am able to access it that means it is giving the society so overall if i go back i do this end impersonation and i go to that business rule because that's really important to understand so if i go to business rules and here's my business rule so the important point here is important point is not the glide record i did important point is the method i use for glide user and that is get company id so basically this is returning the society of the company of the logged in user and then it is doing that glide racket with the same society returning the racket and i'm able to access the name and that's the reason it is being populated in the racket that is in the incident racket this is how you can access the information of logged in user but the only those information which basically these direct apis are direct api methods are providing but if you still need some other information because there are a lot of fields on this user table so you can't access all the information with all these methods because you don't have methods for every field you have on this user table it's for major fields which you mo which you basically mostly use during your coding or fulfilling different requirements now let's see another method of glide user so i will go to the slide and the next method is get display name now get display name basically returned the current user's display name that means the actual name of the user so how can i do that so i will just quickly go to my instance now this time what i will do i'm not going to i'm not going to work on different requirements now because because first requirement just to explain you that how can you use it for different requirements if you will get from your customers but overall goal of this session is to just to make you understand that how can you use glide user apis so in that case for second api we have second method we have so what i will do i will just remove this whole code and here i will just show a method whenever i will create an incident or i will update something so for what i will do i will quickly store that information somewhere so this time we want to access display name so what i will do i will just do d name equal to how can i do that gs dot get user so i will just use get user i will do dot and you can see the second one it says gets display name of the current user and i'm system administrator so i will just press tab i will do semicolon here i will put gs.info and here not gs.info maybe gs dot add info message so here i will put this is message for and that's it and here i can just do the name because in this variable i'm storing this name the whole full full name now here we have system administrator but we will impersonate someone maybe apple tutor again but i will do one thing i will just save this business rule it's a very like two line of code because i just want to show you that how exactly you can access that information what i will do now i will quickly impersonate apple tutor and we will create one incident with that so we are going to impersonate apple tutor and i will go to incident and maybe i will maybe i will update something not create just an existing one maybe this one let's update this one so what i will do i will just remove this one and i will update it let's see if you're getting that method yes you can see this is message for apple tutor and it basically adds holes so basically if i come here and go to profile in okay it says record not found it's kind of strange maybe it's because of access that's fine i can still end impersonation and i can still show you that how exactly uh that name is coming so i go to sys underscore user dot list it will take me to the new tab and here you can see name so here is the name and it shows the same display name so wherever in your server side code wherever you want to display the name of the logged in user in any requirement not just for incident in any requirement you can just use get display name method with get user method so let's go to the third one now so next one is get email now if you want to access email address of logged in user how can you do that so let me go to my instance and we will come here we will go to business rules again and i will just come here and i will go to demo for company i think name where we have not changed the name but that's fine because i just want to show you the demo so here i will make it full screen i think i will i will just keep this particular script screen open because i i we have to make further changes so now what i will do i will just make it this comment and here i will create another variable maybe that is uh user u email that means user email equal to gs.getuser and here i'll do i will do dot and i will do basically you can do get email so i can do get email now you basically observed one thing you're not getting that get email method but it is definitely there so that's fine maybe servicenow has not added in their script so basically in script helper so i will just do get email and i will just add this over here and i will save this and what i will do now i will directly access incident with administrator account only because we will see if we are getting any output or not because output will still be the same for every user even for admin as well because we have not put any kind of condition so i will go to open incident i will maybe open the same one and here i will update something maybe one and if i save this you can see i got the email address it says this is message for i think i have just incorrect english but that's fine i think this message is for admin ad example now this is the email address where we have this email address i go here i go to profile or i will just cancel this one i think white is asking me to cancel maybe i will reload this leave and if i will come here maybe i will go to my profile again here and i go to my profile you can see this current logged in user basically which is admin the email address is admin example.com so that's how you can basically get the information of email of the current logged in user for different requirements so let's go to the another method we have for glide user that is get first name now this this particular method you will i think mostly use because you definitely want to know the first name of the logged in user in most of the requirements so how can i do that so i will just go to my instance again and i will basically go to the first name we have system that's the name of this system administrator and here is the business rule we have and what i will do i will just change this so here i will just mention uf name and here i will put i think i don't have first name here but i can just add it so i can just add first name here i can just add parentheses just semicolon and i'm done so i will copy this i will mention here this variable i will save this so this business rule is saved and now i'll go i will go to my incident again and here i will change something and i will save this you can see this is message for system so it is populating basically the first name of the current logged in user regardless of the caller or it is not basically basically copying from the caller it is just running that business rule which we have created here where we are fetching the information of current logged in user and we are fetching the first name with the help of get user now next one we have is get last name so like you can get first name you can also get last name so i will do one thing here i will do last i'm just changing the method and here i will add l for our variable i will save this now this time we should get administrator because that's a last name for administrator so it is saved and then i will i think okay you forgot something you have to use this variable here as well so save this now this time we are good i will go to my incident i will add a one and i will save this that's a basically condition triggering point of business rule i will save this and we will get the message you can see we are getting last name so go for next method now so this is how you can access last name and next one is get id that means the society of the logged in user now we got the society of the company we got uh the first name we got the last name we got the complete name as well but now it might happen that you might get requirement where you have to maybe for acls maybe for some different requirements maybe on client script as well but but but you have to fetch that information but as i told you even for client you have you can use basically this glide user apis but you have some different method syntax is definitely different which you can see in my another video so now we will use get id which will return the society of the current logged in user so i will just go here and what i will do i will maybe i will go to i will search is the system administrator we have so maybe i have this admin and i have the system administrator and if i check the society i will just do copy society and if i paste here so this is the society we have so what i will do now here i will go here in my same business rule here we will mention maybe u id and here i can just do get basically dot and i can do id get id and i can just copy this save i basically put it here and just save this and i'm done i'm able to access current logged in user sys id how i will go here i will save something i will save this incident you can see it is giving me the society now what is the society this is the society you can see here we are 6816 and here we also have 6816 so this is the society of the user the logged in user that is admin user sister system administrator user now we will go for the next method for glide user and that is get my groups now this is also very important because this gives you access to the groups basically the logged in user belongs to so if i'm a user i'm a logged in user and if you want to check that how many groups basically the user same user has been added how many users how many groups that particular logged in user has been added to so in that case you will get all those groups how can we get that just go in my instance and i will go to my business rule and here i will do you groups and i will just do get and we will do my groups get my groups that's it semicolon and i will just copy this paste it here and i will save this and i will update our incident again because we have to get the message get the output i will save this you can see we got the society of one group now how can i know that so let's go here i will show you something so i will open this admin record and we will see the groups associated with this user so you can see we only have one group here we have team development code reviewers now if i will copy this society let's say i will paste it here and i will go to basically the here we have this starting with four seven four c seven four four and if i will come here you can see see okay it says c c b a d i think it's different ideally it should show uh the society of all the groups i think we just have one group here society is different okay i got it because it's a relationship not the society of this group let's go here this is the group this time you will get the right society and you can see we have cfc i think it's it says cfc bad but here we have absolutely we have the same society here we have different why we have different because if you will copy this one this will copy the society of this relationship not the group society but if you are checking here you have the same society and you can see we have cfc b and same here as well that means you are able to get all and that is also an array so how can we check that let's add another group to this admin user so let me add that so that you understand that how exactly it will populate that basically fetch all those information so let me add this to audit administrators so i will click on save so now i have two groups so here if i will go to my incident and i will save this this time i will get two groups the society of two groups i will save this absolutely so basically overall you are getting an array so you can see we are getting in comma and if you want to use these groups you can just maybe glide racket by by separating them by splitting these basically groups and then you can get those information so if you really want to check let's say even for access that if current logged in user is part of this this group in order to provide access that means in order to run the acl in that case you can definitely use get my gross in that case it's it will be really helpful to you without accessing without basically doing the glide record in cis user basically user and group relationship as well you can automatically know the society if you know the society of the group so let's see there's a group and you have you have some access rules where you want that that user should be able to access that particular records only when that user is part of that particular group which you know the society so if you will find so in your logic in your acl you will put first get my groups split those basically uh groups and then check if that society is available in that get my groups output and if you will if that is available then you provide the access else not that's how you can use this get my groups method now the next method we have is get name now as we we got get display name which returns the whole name of the user now get name basically returns the user id of the logged in user the user id field basically the user name field so i will go to my instance and if i come here basically to the user i'm talking about this field user id this user name so if i will come to my business rule and here i will put you maybe u id or name and here i will do get you can see here we have get name and we have get name and you can also read it let me let's let's do it again dot get it says gets the user id or login name of the current user okay so i will just do semicolon i will copy this i will paste it here and i will just save it and i will go to my incident because i have to update it and check the output and check the results i will just click on here and i will save this and you can see i got admin now admin is this user id if you will go here you can see the logged in user user id that user name is admin that's the reason that is populating so if you really want to check the user id the user name of the logged in user you can use get name method then i will go to the next one we have get roles now this is also very important now as we talked about get my groups where you are basically checking if that particular user is part of that group or not but for access you also needs to check roles because roles are definitely for access so you can use this particular method for a logged in user in acl as well even for doing a query so if you really don't want a logged in user to access some rackets you can definitely write acls but you can also do the query you can stop users for querying the rackets for incident change problem or different table records in servicenow system so get roles will give you the what roles that particular user has i think the society let's let's go to here go to the business rule and here i will just add you roles and here i will add dot get roles i think i don't have that available here but i can still put it because it will definitely return me the all basically all roles associated with this user the logged in user and i can just do like this and paste it here save it and i will validate it so i will just change it and i will save this let's see and you can see it is basically not returning the society it is returning the name of the roles basically this user has that means this logged in user has and that is system administrator so you can see our output is definitely not looking good but what you can do i can show you this particular output in a better way how in background script let me go over there so i will just type here background here we have background script and i can just type here gs dot now we will not get helper here but that's fine we'll just do gs.getuser and i will do dot get rows that's it and i will just save the output and in a variable so var a and then i will do gs dot print because i have to show you so i will just do a here that's it and if i will run this you will see the output with all the rules added to the logged in user that is system administrator i will run this and you can see you can see all the roles here all the roles including admin so all the roles which are basically assigned to this user and i can go here you can see we have six roles and if i come here um i think yeah it is showing you the six roles but that's how you can check that what are roles a current logged in user has and you can definitely use it for different functionalities for access or different different requirements you get from your customers the next method of glide user is get user by id and you have to provide a parameter as well now this particular method is really important the reason is because as of now we were talking about the logged in user details we were trying to fetch the information about logged in user but this method is used to fetch the information about another user basically any other user and what is that another user basically you provide that input in this parameter maybe the society of the user or the user id that means the username field let's see how exactly you can access this information so now here what we will do we will uh basically we have to use this get user by id so i will do one thing here i will do user and here i will to dot get user by id now we have to provide a parameter here what parameter as i mentioned it can be basically society of any another user or it can be user id of that user as well so if i go to users now here we have this list of users and you can see we have this apple tutor so what i will do i will basically copy this and i will because it's a string so i will provide that user id now the important point here is once you will put that information now what kind of information you want to access so let's say you want to access apple tutors first name in that case what you will do you will do where user f name you will do user and this is very important point here user dot and you will quickly do get first name that's a that's the api we have that's a method we have getfirstname and i think even if i put gs.getuser i will put getuser.getfirstname i think here we don't have getfirstname that's fine i will just put get first name and if i will copy this and paste it here and what exactly it will do first you are accessing that user app object now for here basically you're accessing the user object then you are basically accessing not the current user you're accessing this user now and once you are done with that now you can use the same methods which you are using with this get user actually it is kind of same but for another user you have to use this and then you can use any method which you are using with get user so now here i am putting user dot get first name and i'm saving this and let's see what kind of output we will get ideally we should get first name of adult apple tutor so i will remove it from here and i will save this absolutely you can see it says apple so here we have output as apple i'm able to access that that means that's how you can access that information for another user as well that it means glide user it is it's not just specifically for a logged in user if you really want to access information of another user same type of information which you're accessing with different methods for different user then you can use this method as well that is get user by id then next method now next method is get user roles now previously we talked about get roles now what is the difference between get user roles and get roles now get roles basically gives you all the roles whether user has got it from group or basically inherited from any particular another role all those roles are basically shown or basically the result will be that all roles are mentioned in that output however this particular method returns only the roles which are explicitly given directly to the user so if i will go to my instance and if i come over here maybe to this user now inherited wherever we have inherited false that means this one is directly assigned to this user says inherited false so let's see let's try to use this get user roles basically we will come here and i will just maybe remove till here because we don't need that anymore and i will just put roles and i will mention here get user roles now this will also be again definitely separated with the comma so here we will just copy this and put it here and i will save this and i will go to my incident i will put one now and i will save this absolutely you can see admin and security admin only two roles i'm getting the reason behind it because these are the two rules which are not which are basically directly added to this user you can see admin false that is it is not inherited and second one is security admin but that's how you can use this get user roles method for glide user next one is has role now this one basically confirms now this is again will be basically used for access to provide access to confirm access for different tables and records when you have to check whether the logged in user or any user has that particular role or not so if you want to provide access to a user if that user has that role or on an acl you already have a field but it i think in some i think in some use cases you might get this kind of requirement so in that case you can use whether that user has that role or not and it says string role where you can provide the society of that particular role so let's go to the instance so here we have admin and what i will do i will just check the society of this admin role so what i will do i will come to my br here and i will just do dot has role and here i have to provide that particular role society and that is i think i can put name as well or i think i can just come here let's put admin and i just to this semicolon and here now what kind of output we will get now this output will be boolean because it shows you yes or no whether it's true or false so i will save here boolean is basically true or false so whether that user has that role or not now system admin has this role admin so i will so ideally we should get true so i will come here and i will edit this and update this record and click on save let's see we are getting true that means this user has admin role and you can go here that means it is just checking if this user has admin role or not that's how you can check for a particular logged in user even for another user as well and i already showed you that how can you access another user so you can check this has role whether the user has that rule or not if you're getting true or false in that case you can definitely put your or write your further logic or whatever action you want to take like while doing scripting let's go for next method the next method we have is member off this is also really important in which you can check whether a particular logged in user is member of a particular group or not you want to get yes or no if you want to check that you have to validate it and on the basis of that validation you want to perform that action so you might get those requirements so let's go to my instance and i will check is member and i will quickly remove this and i will do is member of it says determines if that basically a person is part of that group i will press this and here it says group basically it is also the string so i will basically provide name of the group now maybe the society as well let's try with name first so i will come here and i will just check for any group so we already have this admin uh person i think we already have this uh this particular group maybe we will start with the society so i will copy with the society now this this admin uh admin is already part of this group so i will come here and i will put this society and i will like do like this and here i will put so the output now this one again the same kind of output that is boolean that means true or false because we are doing the validation so i will click on save and then i will just update the racket so that i can run that business rule and i will save this and i will get oh i'm not getting any output let's go over there again this method is true okay i don't know why i'm getting my message over there but that's fine i will come here again and we have this message is for this i think is member um g is that is member of i thinks it seems to be good i will save this and then i will i think it's saved i will come here and now i will update it again and see if i'm getting that message absolutely i'm getting true how would i get false so what i will do i will just maybe access any other group we will add basically other group as a parameter now this system administrator is part of is not part of basically audit managers or maybe billing support let's let's add this billing support so i will just copy the society i will come to my business rule and i will add this business rule society and then i will check it whether this ad system administrator is part of this group or not but as we know the system administrator is only part of two groups audit administrators team development code reviewers so i will come here and what i will do i will update the racket to see the output i will do like this and i will click on save absolutely we got the message is for false so we are getting false because system administrator is not part of the group which we have mentioned here but that's how you can check the basically group membership for current logged in user and even for other user as well so this is how you can use glide user apis and methods to achieve different functionality in service now and that is also in server side but as i mentioned earlier that glide user apis can be used in server side and in client side as well but the syntax is definitely different thanks for watching the video have a great day
https://www.youtube.com/watch?v=0awaAcxM0IU