logo

NJP

GlideRecord Concepts and Best Practices | Share the Wealth

Import · Apr 28, 2022 · video

slide record concepts in best practices after meeting with uh some folks yesterday too talking about this something i already knew needed to be talked about but it became even more apparent when i heard other people talking about and that's sort of like the quirks and weirdness behind glide record and you know we're do we have a varying very varying degree of expertise on this share of the well so i'm gonna be as as basic as i possibly can be while also getting into some more advanced topics so please at any point if anyone has any questions stop me i i intend for the share of the wells to always be interactive and i really you know if there's something that you disagree with please let me know having that disagreement you know typically leads to better uh solutions into better ideas some of this is opinionated some of it is just factual but it's not so opinionated i had to put a disclaimer like i did my last one so that's a good thing so anyway we'll talk about sort of the quirks of of glad record why it is the way that it is why it acts the way that it acts and it also covers some of the other you know general objects that you interact with in server code and servicenow as well because they are similar to glide record okay let me just get a head count here real quick 72 very nice okay so let's go ahead and start agenda made the agenda very very simple definition so we're going to go over definitions of what glide records are like i said we're starting very basic as well as some related terms with glide record we're going to go over some concepts basically in advance this is going to go into sort of why well it goes into quirkiness as well as the definition of you know what's happening behind the scenes with glad record and then we'll go into some best practices ended up being a lot less best practices you know on paper at least than i thought it was going to be but they are important ones so i want to make sure we go over all those and i i have demo in here but the demo is really going to be like i said going as we go through this process rather than you know going through verbiage and all that and then you know at the end getting to demo we're going to probably going back and forth to code along the way okay i will be opening up my visual studio here just for a little bit of code that i already wrote for this it's not much it's minimal okay all right cool so definitions let's start with glide record glide record very simply is the is a server-side object that contains one or more records from a single table right so we as developers create glide records we supply them with queries so that they know what information to go get from the database and then we act upon those records typically by iterating the glide record and either setting values and updating the database or getting values from the database so that we can use them in our code or display them to the user so in a nutshell that's what a glide worker does glide record object right this is the object that we act upon glad record api is is different right so glide record api is all of the programming behind that object right and that is our primary means of interfacing with the database from servicenow server-side code right there is a client-side glide record it's not recommended to be used but if you try to use glide record in client it actually works it should always be used asynchronously if you are going to use it but it really shouldn't be used right we could go into more client stuff when we get into glide ajax and in a different share of well all right so i wanted to bring up glide record secure here as well so glide record if you as a developer go out and put a glide record call out in a server script somewhere it's going to run that script and not check acls by default right that's what glide record does if you want it to enforce and check acl so that it fails if that if the user running the script or the user that initiated the script so the user that performed the action that initiates the script does not have access to it then you would want to use glide record secure so that acls will actually get evaluated and you know updates to the table i mean updates the database are either prevented or allowed okay so three different things here three different little concepts again glide records is what we interact with the api is what is the code and it's kind of kind of strange too when you look at the documentation of glide record it's you know it's a lot for one object for one class they've put you know there's a ton of methods in there for getting data from the database and then there's a ton of methods in there for pulling data like actually acting upon it it's a lot for one object but it is it is the primary api that we use in development in servicenow okay let's get into concepts right this is sort of a pretext as to why live records are weird glide record is so it's pulled from the server by servicenow and it is not actually a native javascript object it is a java object okay so behind the scenes servicenow is using java to interact with their database which is if if you don't know java or anything about java java is a server-side language completely different from javascript the names are similar but it's a completely different thing right it's only for server-side so java objects can be brought into javascript and they behave kind of like objects in javascript but not 100 like objects in javascript there's some weirdness to them right so that's why i'm bringing this up here a lot of your methods a lot of your classes in servicenow glide date time being one that's a prime example also a java object but a lot of your your classes in the servicenow api you'll call functions on them and they'll actually set internal properties rather than returning some value i want to say add or subtract one of those two with the glide record is is i mean with the glide date time is like that and so basically you have to call the function on it but you're not setting a variable with that value that's coming out of that function because it doesn't return anything and that can be massively confusing uh to some people who especially whenever you're in javascript i'm going to turn my heater off real quick sorry especially when you're in javascript and you're so used to you know returning values because it's really best practice in any function to return values even if it's a null value so anyway you have these void classes that are void functions that get called in in java that set some sort of internal property on that object and then you have to use another function to get that property right to get that property value so one of those going back to glide date time if you you know can't remember what function again it's add or subtract one of those internally it does the calculations that's the properties of that object and then you can use you know get display value to actually fetch what that value is i may be completely wrong on what those functions are but anyone who's dealt with glide daytime probably already knows that it's it's wonky right okay cool so one thing about java 2 in a lot of cases here is that these internal class properties that you're setting on these java objects hold references rather than values if you've ever developed in java or c sharp you i remember specifically in old visual basic being able to set parameters either by value or by reference and all that it means is that it's either going to be a pointer that's pointing to some reference in the code or in memory or it's going to be an actual value right references change based on perspective and values do not so one of the huge mysteries around glide record and what i see a lot of one of the things that prompted this show the record or share the world is people calling to string on things well the only time you have to call to string on something coming out of a glide record is when it's pointing to one of those internal properties that is a reference rather than a value and that's why you have to call it a string on it okay you can iterate through and i'll show you in some code in just a moment you can iterate through 10 10 glide record items the applied record iterations and every time you go through it the reference changes to the current glide record object and so if you were to go in and just start pushing glide record object fields to an array without calling to string on them all of them are going to be the same because you're actually pushing a reference each time and then on the last one the reference is the last record and so that's what it appears to be in the array okay actually show you that real quick let's say i hope i have this big enough too let's say var gr equals new glide record and we're just going to do in gr set limit so set limit allows us to say i'd only like to return back a number of records i'll do 10 gr query so this is just going to return the top 10 incidents here okay so right now i'll do i'll create an array let's say numbers and it's an empty array while gr next so this is i'll get into all of these things in a little bit but this is the iterator this is how you iterate a glide record and i'll explain the iteration of why you have to iterate a cloud variable as well let's push the number of each incidence to this array oops gr dot number right so gr being glad record that number being the field name that we're walking to by using our dot notation so we're pushing numbers into numbers and now we're going to do gs info numbers actually no we're going to do i'll go ahead and do numbers for each and we'll print each one that's going to make it look look better so jsonphone number so now let's run this and what we should see like i said since these are references within glide record we should see the same value 10 times and that's what we see right we see that same value 10 times because again we've pushed the reference into the array 10 times we haven't pushed the value into the array 10 times so what do we do to remedy this is we just come in here and say it's a string well not here i'm sorry we can call to string here on number or we can actually use getvalue which is preferred right because get value always returns a string value now we do it and we should see our proper top 10 numbers and there we go right so they're no longer a reference now now each one of them is an actual value and this is why i mean i see it all the time not not internally as much but some in the community or it's just like just throw it to string on it but there's a very specific time in place where calling to string or calling it value is valid and usable in any way you know actually it's going to help you and this is it so anytime you need to change a reference that is a java object something that you need to display to the user is i do it now another quirk here would be this if i were to just knock out this whole array and not do the array at all but then just do a print or gs info on gr number anyone want to guess what's going to happen and yeah jason you bring up a good point you oh yeah i'm sorry go ahead and we'll talk about what jason said you'll get different numbers yes and that is because gsinfo forces this to a string right it's not because we've changed anything it's still a reference when it hits gs info but gs info is actually going grabbing the value and saying let's print that value out all right so we hit it and now we see but still not great practice to use that to use gr dot any field name like this because there are there are places where it's not going to work that way and you know ease of reading the code and ease of use for everyone downstream that's going to be maintaining your code is just to use the methods that servicenow's provided you would get value and get displayed by you and yeah so there's also there's a question here in chat from jason that instead of using dot to string because here we could use two string actually let's do the let's go back to the push numbers push and gr number right now what we showed before is that these come out as references now gs info here doesn't turn it does actually turn them to strings but the problem is gs info is not running until they're all set to the same reference right so they do come out as strings whenever you whenever you print them this way but if you push them into the array first then the array is holding those references rather than values so here we could have used two string to get this to work and gotten different values instead of the get value or as jason brings up you can use empty string to force it to be a string so i don't i don't i find this to be less concise less clear especially if you have a long string that has a bunch of these or a concatenation of strings that has a bunch of these so i i prefer to string if i'm going to use the string and if i'm working on a glide record i prefer get value okay i think tostring in the grand scheme of things is slightly less performant than this but it's so negligible that it's not going to matter right we're not doing big data here or anything like that okay so that demonstrates what i'm talking about here with references and why it can be very very quirky okay so we went over references rather than values and then again references must be converted to values if they're being stored so again if we're going to put those into an array or anything like that then we need to make sure we are converting them to values preferably again with getvalue or get display value on the actual glide record call itself any questions so far that i that i can audibly answer hey good point chris about undefined not having a stream so you have to kind of you know be sure before you call to string on something that it's not undefined or null right so yeah i i would typically check to make sure gr number is not undefined or null so i'm gonna before i call to string on it just for good practice but again i still think two string in the long run is more concise and and more descriptive as to what it's actually doing easier to read easier to maintain overall if you're gonna use the string okay all right next okay some more oddities and these are i think more odd than than what we were just talking about okay so i see the use of glide record yes so i want to make sure we're all clear with what that does versus record.query okay so glide records are strange in the sense that they can they can represent two different things they can represent well three things really they can represent an empty glide record that's just an object that has had no act no actions put upon it yet so it has not queried anything yet it's just an object or it can be a single object right or it can be a collection of objects so if you call some let's let's use get as an example here we see using the get function retrieves a single record but that's not always true and that's the quirkiness we need to talk about so right after that i tell you how to pull multiple records is to use get or to use query so get sometimes retrieves multiple records right so the documentation for get tells us that the get function if you pass in a cis id and if it if the system recognizes it as a cis id it's going to go out and get that one record okay what i've found through working with get i typically don't use it any way other than just the passive id if i have a query i'll use query because again it's more concise but you can use gets and pass in two parameters rather than one you can pass in a field name and a value and if that results in a single record being returned it returns a single object if it if it results in two it returns an iterable object which can be really dangerous and have lots of side effects that you're not expecting if you're not 110 sure of what you're returning right so i can demonstrate this real quick we'll use get here so we'll still stick with gr but we're going to get rid of our gr i mean with incident i'm sorry or get rid of rgr query we'll get rid of our set limit here okay so i'm going to jump over here to incidents and look at let's look at open incidents and i'm just going to go into a record and grab a sis id okay copy that's this id come over here and i'm going to say gr get i'm gonna pass this id and then i'm gonna just do a gs info on gr.net so gr.next is what we use to iterate through a glide record that has a collection within it but it also will give us a true or false if if it can iterate to the next one right so what i would expect to happen here since this is a single object and not a collection of objects is that gr next is going to return false okay and it does so it does not actually iterate to a new object it stays on the one object that got returned now if we make this a little bit different and say uh short description and i'm gonna go do this real quick where we go back to our incidents and i'm actually going to uh group by short description real quick just so i can see if there are any that have multiple of the same and there really are because i looked at it earlier but let's look at this one first let's grab so we know that there's only one incident with this short description so again based on what i've already told you i would expect this short description being passed into the get function to return a single record so let's try that get short description here's the single record again gr next should be false because it is a single object not a collection of objects and it is okay now if we come down here and grab this one emr system is down we can see that we have two records here i'm having a hard time in this new ui which i love by the way i'm getting used to it so the er emr system is down we know we have two incidents with that short description so if i come in here and use that well now i have an iterating an object that has to be iterated right so let me actually do this let me go grab that top short description again assessment atf assessor and i'm going to comment out some code real quick so what we'll add gr get again short description and put in the old value and let me get rid of this so again this returns just a single record and so with that i can say gr get value let's look at the value of number right and we're going to print out the value of number oh maybe not hold on was this the one that only returned one maybe get value is a problem let me see if the number just works no okay sorry i may have thrown you all off on that one so this returns one but not with the next so it actually is a little bit different than i thought it was going to be let me check this real quick so if i get rid of this and do gr next and we know we have a next in here let's call so we we can move to the first record right we know we have a collection coming back from this frozen up okay we know we have a collection coming back from this so we can move to the first record and then we can call gr.number we should get the number there it is of that incident so i thought get if you pass in a value that was unique it was going to send you one that was just an object but it looks like it doesn't it looks like it actually fails which is actually a good thing i think that's better than it returning just a single object going back though to sysid pulling by sis id and copy this this id again this is how i recommend using get if you're going to use get right is just get it with a sys id that way you always know you're going to have one or zero objects return it's either going to come back and it's it's going to be null because it couldn't find anything or it's going to to actually grab the record that you wanted to grab okay now it's also best practice here that when you call to check to make sure the get function actually returns something and it's really convenient here that get returns a boolean value if it can grab the record so it's best practice to do an if statement and put your get in the if statement then within your if statement say gs info dot gr number i'll use get value here actually okay uh else gs info you know not found or something like that okay it's going to be but i just want to you know show that that's how you can do error how you can check here to make sure your glide records is actually returning something that you can act upon by doing the if yet okay so when we run this we should get our incident number again okay so calling get also again going back to the whole statement here of javascript does not always return a value sometimes it's void let's say we want to say you know i'm assuming here that var record i could just get it by calling g r get with that cis id right and then let's do a gs info on record and get rid of all this code and hit script here so it returns true it doesn't return the record at all right because again get only returns a true false value it does not return the operation that you might think get would return in most other cases because again it's only updating internally the the record that is associated with the glad record it's not actually returning the record itself it's just returning true or false okay so little quirks there with how get works again my recommendation on this is use get when you when you have a sys id it can be used for other things but it can also get really hairy if you start passing in you know something that could return multiple records now you could make the the general statement that okay well i'll use get but i'll always iterate right well the one time you forget that you pass an assist id you can't iterate because it's again one object rather than a collection of objects right if i tried to call again if i tried to call gr next on this to move to the next record i get a false because there is no next record so for safety and for your own sanity in troubleshooting if you're going to use get use it with assist id use query for everything else okay any arguments against that dave is there any downside to just always using query if you're always going to iterate i i'd say it's less again less concise right if i know i'm passing an assist id it's much easier for me to call let's just look at what those look like right if gr get let's say this is a function gr okay let's make this a function actually function get glide record or get incident actually because we already have incident in there uh so it's id okay so here's one of our functions sysid now becomes this this id i keep locking up i'm sorry i don't know if it's the instance or if it's the fact that i want zoom all right so we have a function now get incident let's do another another one for function get incident and also pass insist id and this time we'll use query so we're going to do gr ad query and we're going to add sysid equals sysid coming in and gr query now after that we have to say if gr has next because it's an iterated it's an iterated thing at this point then return gr we don't have to do it that way that's how i prefer to do it okay because then i know it's going to be i can come down here and now return null else so if i get an old value i know it's because my glide record was invalid or it could could not get a glide record based on what i passed in so this versus this in my opinion the more lines of code you have likely well in some cases the the more open you are for errors query would you still want to use set limit as well or no if it's just returning to the society you absolutely can do that yes if you're if you know you're passing an assist id then i i would do that as well so if you set limit to one knowing you're going to return one it's still going to return an iterated value though an iterated object so you're still going to have to do the the while you know once you pull this incident out you're still going to have to do a while on that and iterate into the first cloud record even if you do a set limit one so i think easier to work with once i call this top one so [Music] var incident equals get and i know this is a bit confusing having two functions the same name but if we pass in that same cis id oops i'm going to actually call this get incidents since we're doing an iteration on it so incident get incident here i can just say if incidents right if it comes back and it's not null or undefined then gs info can't type one-on-one camera incident number okay now get incidents we can come down here and call that as well let's say of our incidents equals get incidents passing in the same cis id again we have to say if incidence because remember it's going to return null if that doesn't if it doesn't find anything but now i have to call while on it too while next or while the incident's next gs info incidents i can't just call number well i can't just call numbers that'd be fine so we should get the same results in both of these but now you know we're talking about five lines of code or four lines of code versus this six right and they should both come to the same thing and they do so it's just a little more concise using get when you have a system that you're passing in so anything that you get by syste again it's just my recommendation to use to use that because it's you know it ends up being less code less issues and easier to read in my opinion so that's the first set of oddities let's go to another set of oddities right again this is just sort of explaining what we just went over right the get function which is a single record when the argument is passed in as a result and the results is a single record again this is really my first time messing with get in a way that i could try and return multiple records and so what i was testing with earlier it looked like it was showing multiple records but in fact it looks like it's actually airing out so that's actually not a correct statement here in the here in the presentation so i'll make sure i change that so it here it should be it retrieves nothing if the arguments passed in result in multiple records i think query function always returns or retrieves multiple records so if you know you're going to have multiple records or there's a chance of having anywhere from one's many the recommendation is to use query as we've been or as we did down here okay okay and again with query you have to add actually add the queries that you're going to be you know the terms that you're going to be querying in the database it's not just get it's actually adding all the terms so if we were getting anything other than this id here let's say we were getting things five different terms and this script becomes a lot longer again and then iteration so we've already gone over some of this in the code but this is how you iterate uh glide record the reason we iterate a glide record with next is because next returns a true false value and so you can call a while statement and as long as next is true when we have the setup as we do here where am i actually let's do this a little bit different okay i'm going to get rid of my functions here and we're just going to go back to var gr equals new glide record incidents and we're just going to do grad query add active query so we're just going to get our active incidents gr query and so now we do the iteration right so we know this is returning more than one a collection of of records of incidents so we call while on it again the way while works is whatever you put in this these parentheses is a boolean value and as long as it returns uh true the while loop will continue to go and it's just gonna it's just gonna go go until it does not return true well gr.next happens to return true if there's a next record and it happens to return false if there's not a next director so we we utilize that rather than doing any other sort of iteration method like like you would a typical object in javascript or an array we use this method here because we leverage that true false value of next okay and again we can do uh gs info gr number and get all of our active incidents that way right okay so that's really what i'm touching up on here is that the reason we use while and not some other iteration method is because this is a java object you can't call for each on it because it's not a an array you can do this so if we were to do instead of a while let's say we try and iterate like we iterate an object in javascript right so for var field i'm sorry record in gr right so we know glad record is a collection of records it's not just one record so i'm going to try and iterate all of them let's do a gs info gr okay actually to uh gr record is that how to do it for each record in yeah no i would just do record sorry so let's see what happens if i just do record what's that so looks like we got a bunch of metadata right which is something i was going to go into anyway because servicenow has i think altered how and again a lot all of this is black box i can only assume how they do things so if you tried to print a glide record into a string at like a json stringify string it returns this meta like that just did so i think behind the scenes servicenow takes this object when it moves it over into javascript and turns it into an actual object but it's not the actual object that you would expect it to be it's not you know an array of objects as you would expect it to be if it was pure javascript i guess is what i'm saying which is a good point um we could go back to that while gr next and do json or gs info and call json stringify on it so what json stringify does is takes an object that an object in javascript and it turns it into a json string so if i just did a json stringify on let's just do if next right because i only want to pull the first one let me do a set limit just to make sure okay let's pull one and then we're going to go to the first one using next and we're going to call json stringify on it and it's interesting because what you get is this this meta says meta object which is completely different than if you were to say you know gs info you know look at some property right like we have been doing with number it's a completely different object if you stringify it then it is if you iterate it and actually call upon the different properties i think servicenow probably did that so that you can pass the glide record into glide ajax like stringify it and send it to you know back to the client through glide ajax and not have a complete failure of whatever object it is that you expect right but again a lot of this stuff i i can only assume because i can't see exactly what cloud record is doing behind the scenes i leave stuff like that up to my car so if he's on the call let's see this and the record returned all the column names from the table available and the objects is the keys yes the values those are another set yes that's right so you'd have to use actually a record to get the values of each of those those were just the properties set from the glide record but the cis meta object doesn't follow that that logic at all right because this meta is its own object that's being returned whenever you do json stringify it's just weird okay all right so that's a little bit of the iteration of a glide record there yes this meta is rude it is okay dangers i wanted to make sure we bring this up right so encoded queries rather than passing in each line for those that aren't very familiar with cloud record if we wanted to pass in an encoded query right encoded query is just a string that represents a query okay add encoded query and let's say active equals true so it's a string query and let's also say uh number like and let's let's just start with the first number inc 0 0 0 right oops that's o okay now we should get records that meet these criteria so while gr next gsm ogre number so we only have one that's active that is let's do it starts with send limit oh thank you we could use starts with two yeah and let's take the set limit off there we go now we have the ones that start with inc zero zero zero and that are active so that's what an encoded query is so i bring up bring it up because if at any point in this encoder query you put something that's in rather than it failing it's actually going to apply the part of the query that is valid which if you're doing like a mass deletion or something like that you may delete a bunch of records you didn't want to delete so this one can be very very dangerous there's a system property to to actually mitigate that because i assume this got created after one of the early releases and people deleted a bunch of records and this is glide invalid query returns no rows by default i do believe that is set to false so it will return the rows if you do not set that to true okay so setting that to true actually makes it where if you have a failure at any point in your encoded query it's going to return no roots okay all right so i've got best practices now i wanted to show this piece here this part of best practice is good i'm glad i looked at that real quick oops excuse me there let's go back to presentation all right best practices all right and again some of these are opinionated but i i have arguments for all if we need to go into argument all right oh then when you stack add query so stacking add query is kind of strange right because when you do when you call add query and stack it each add query is actually returning something that's not a glide record it's returning a glide what's the class called uh glide query i can't remember exactly let's see glide query [Music] i can't remember what it is i i could talk to you about it after ben but at glidecr that actually returns a different object than glide record when you stack add queries and i find that can be confusing as well but yeah that's not a that's not an issue with with adding queries i don't believe it's only an encoded query issue as far as i understand that it it actually does that and returns all of those records okay okay okay so best practices i put in here first and foremost do not nest glide record calls almost every time i see an issue with glide record it's because we've got nested glide record calls right so that's the code that i wrote in here right so here single function get request catalog items are item names we have a glide record for request we then nest the glide record call for a requested item and then we could even go further than that and pull things that are you know use use another glide record call to pull things from that requested item that are that require glide record lookups let's try and avoid that if an oil is always possible to avoid that let's say that i'm not going to say it's possible let's avoid that because it can get very very confusing and when you go to troubleshoot it can be very very difficult to pinpoint where the problem is so a method that's much more concise and i think so more readable i think more maintainable i think more just easier to read overall is to break the two cloud record calls up into their own function right get request id by number and get requested items by request id and then you just call them up here and you don't have the nesting glide records actually sitting within glide records technically they are because you're calling functions within functions but it's still a lot cleaner so this does the same thing both of these return the same thing let's show that real quick i've actually got a request number in here that i i already know has certain variables so yeah they both return the same thing one's more one's easier to read why wouldn't you use it right so try to avoid nesting glide record calls again you could lose track of them very easily i also say here put every glider i could call in a function and i i think that that's true you should always put a glider calling a function i hear a lot of debate about naming glide record gr if every one of your glide record calls isn't a function it doesn't matter if you call it gr right because it's scoped to that one function and i'm not going to have another gr in there another glide record in there it's one single glide worker call so there's no harm no foul in using gr and i don't have to be creative with my names because this tells me what the function is doing not this right be very descriptive with this name doesn't really matter if you're descriptive with this name if gr was not a standard or an industry standard name i would 100 percent say don't use it but it's a very very standard name for developers in servicenow when you see gr you know exactly what it is it's a glider right okay always return a value from a function even though the value is done i mentioned that earlier and this is more of a general development thing so it if you always return a value even if that value is null and you get an undefined then you know an error occur or something you probably have to go in there and handle uh your error in some way that you may not have accounted for if it returns null then you know you probably went that path you know to where it would return a null rather than an undefined value okay use get for single functions so we talked about that earlier when when you're expecting to receive a single record use get again cis id right use get with sys ids get value and get display value i i think in readability for readability's sake these are preferred to string or you know the plus quote there's an exception to that that i'll get to in a moment but if you're calling of pulling a value directly from a glide record this is the most concise way to do it get value or get display done get display value and get value are always going to be the same if you're not pulling a reference right it's a reference it's only when you're pulling a reference that goes out and gets that display value of that reference so you in most most cases use get value instead of get display value okay set value and set display value i noticed today when looking through the documentation that set display value is no longer documented but i've i definitely use this quite often when i'm like trying to set the value of a group or something like that where i don't want to go out and get the cis id might not be the best practice because i know behind the scenes it's looking up that syst id so i'll probably take that out before we we put this out for anybody to actually use but set value definitely you want to use if at all possible okay okay set limit function so we saw that in a lot of our sample code here use set limit anytime you want to limit you know what's coming from the database let's say you are deleting some records and you want to test that first i would highly recommend doing a set limit on one record first or even five records to ensure what gets deleted is what you're trying to delete and then you know before you open the floodgates and say i'm going to run this automated script that deletes a bunch of records okay avoid using get row count this one's well documented and well talked about and the reason we don't want to use getro count is because it's going to count all rows if we have you know a million records that come back from an incident table from a customer's instance it's going to count a million records instead of that use the has next function which is instead of using next you can actually call gr.hasnext and it's going to return true if there's at least one record in its in its collection and it's going to return false if there's not one record in it in its collection since if you're trying to determine if it's iterable has next is the way to go if you do actually need the road count of what's being returned then you use glide aggregate which is not covered in this because that would be a whole another uh presentation is glide aggregate actually counts or you know specific accounting or providing averages and maxes and things like that from tables rather than retrieving actual rows so it's much much more performant when you're doing counting of rows or records okay okay avoid dot walking to reference field values from a glide record when possible we all do this and i didn't realize what a performance hit it is until really considering this uh presentation to be honest with you when you dot walk so if we go back in here and save rgr equals new glide record and let's do sis user assist user table and say gr ftr get and i'm going to use gs get user id so my own user id and then say a gs info gr let's get manager name i don't know if i have a manager in here so we're going to see if this fails yeah it's nothing in there so let me go to my user record real quick and set a manager and i am the sysadmin here i got to get used to this new thing okay admin here it's manager not show let's go back and put it in our list then how long did it take everyone to find that gear longer than i'd like to admit big taste he pointed it out early on so oh nice nice okay so abel's my my manager now let me come back here and we call what we call before oh let's see if gr get is this right oh g s get user id gs info gr manager name what am i missing anyone see what i'm missing oh i didn't set it on the right admin it needs to be here that'll do it we would have all been thinking about that one forever so let's try manage your name again right able tutor right so i call this out as a bad practice because when we do that sorry everything's going haywire on me when we do that we actually it goes in and actually pulls the whole user record for able tutor rather than just pulling that one value okay so dot walking in a return value is really convenient a lot of times but it's it if you're doing it a lot and you're doing it a lot in one script it's likely gonna gonna affect performance because every time it dot walks to a new level it goes out and grabs the whole object when if we just said gr get display value right on manager at this point it's going out and it's only returning that one value instead of the entire object so in this instance obviously it's not a huge deal but a bunch of things added on to each other you know dot walking can really get you can really blog it down okay so avoid it if you can if not if dot walking cannot be avoided again use to string function oh and that's saying so let's say you have to dot walk to manager again what i'm saying here is that if i dot walk to manager name or let's say it wasn't a string value that i was not walking to i can't call get i'm you may call be able to call get value on this i'm not sure how that works i think it would fail but to string will definitely as long as that manager exists and again you can do some code to check that but as long as that manager exists calling to string on it rather than a get value or get display value is is what you would do there that's why that's called out i thank everyone for your time hopefully this was enjoyable and you learned something thanks everyone [Music] you

View original source

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