logo

NJP

ServiceNow GlideRecord Mark Read-Only | ServiceNow GlideRecord Tips and Tricks | ServiceNow

Import · Jul 12, 2022 · video

[Music] hi all and welcome back so in today's session we'll be looking into two undocumented function but i find it really helpful so this functional with respect to making glide recorded only yes you are hearing me correctly making client recorded only so for example consider a scenario that you have created certain functions out okay um which is returning a glide record object and you just want to use that particular object to do certain kind of iteration you just want to read through that object okay but don't want to update through anything with respect to that object okay so in this in this particular scenario what if in other script wherever wherever you are calling that particular function out a person update certain attributes out so it's it's it's not a desirable behavior right in simple context i will give you one example so for example you you have a requirement where you have to create a function which should written a user from certain company okay if we have to just check if a user if there is any user belonging to that company if yes then you have to return that user out and we have to just perform certain attributions or certain operation based on that user but in this entire context what if a person or via certain script that user object only get updated so in that sense it's not a desirable behavior right because use this user is a core table and we don't want to update anything on that particular table so how to avoid this kind of scenarios okay so today we'll be focusing on how we can how we can avoid this kind of scenarios and what are the functions what are the hidden functions which are not documented so let's get started so first of all this is my service now personal development instead so let's go to the services user table first okay to get certain encoded query we can say or to perform certain you to define certain use case so this is user table okay as you can see and for example you want to create a function where the output will be like uh a person belonging to a company which company i should take to a company acme north america so what that function will do it will written if any user is there for company nor ekman automaker and then we will perform certain iterations so i will just copy this encoded query out and let's define the function so for as a editor i'll be using fixed script i really like fixed script when i use it as an editor so i'll go over here we create a new fixed script and here let's define the function so what name i should give to the function let's give it like get user so you get user function get user and what this function will do it will simply glide record the user object user table so it will be user equal to new glide record and which table so it will this is user table then we will define our query for acme north america so we will be doing gi user dot add and credit query and we will give the encoded query then we'll go gr dot gi user dot query if the user exists so if gi query user dot next if user exists then we want to return the first user so it will be like creator here user if it doesn't exist we will keep it as empty else we'll give we will make it return empty string so this is a function okay and if we use this function out so for example i will be i'll be i want to use expo for the same so if we take this function to this particular and execute it out so this and if i do [Music] yes dot info you get user so ideally it will return a user object vs dot info sorry so it should written is there object why what happened [Music] which particular line number we got there what is the error [Music] sorry it's not get user this wrong said gr user mypad so if we run it out so we got a user data okay right so it will be like echo cc name or i will simply give it over here dot name pattern dot to string so is syn bonnet okay so we are getting proper user object what if while using this object out so for example if a user use it like this or if a developer used it like this variable gr equal to get user okay so here you will get the user and then what he does he will be doing gr dot email for example he updates the email id equal to hello and gr dot update now this particular scenario is not a valid scenario because we don't want the user information to get updated right let's see first of all if it gets updated or not so we will run this out because the purpose of this function was just to get the user object and just to make it read only right we don't want to have perform any action so as we can see the user was seen on it okay this is the ss id for that user and if you go to the user table so if i go to the user table and if i add a query and this id starts with this so the email id got changed to hello okay if you want to give it a try one more time so if i give it high if i run again then it will get updated to hi which is not right practice right how to avoid this situation so over here in order to avoid this situation in order to avoid this manual error or coding error what we can do we can make this object what we are returning as read only how we can make the topic read only interesting right so there is a function which is not documented called gr user which is the user object dot make read only okay so what this function does it makes that particular entity it makes that particular object read only so whatever is coming out of this function will be read only you can read through the data but you can't update any entity out you don't believe me so let's give it now instead of hi hello we will give thank you let's try to make some some more mistakes so if i give thank you and if i run this code out so ideally as per the previous scenario it should get updated right but if i go over here and if i see again the record so if i refresh it out the record didn't get updated interesting right so you can use this particular function whenever you want to make certain objects which are you are returning from my script read only so you can make it make read only now this is one of the function now there is one more function which is as i mentioned there are two function so the second function is just a complementary function to this function called make read only so for example before doing certain iterations or before updating anything out if you want to check whether this particular is marked read-only or not okay so how you can do that because there should be possibility right we can check also whether else we will never come to nokia why exactly this function is not getting updated why this particular attribute is not getting updated so for that we have function called is read only so for example we got over here variable gi equal to get user and

View original source

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