Unless you do THIS, .get() is DANGEROUS on ServiceNow
hey everyone thanks for joining me on another servicenow toolbox in this episode I am pleased to bring you a good practice to do when you're using dot get in your Glide record queries and hopefully is going to save you from heaps of trouble now if you're new to Glide record you might not know the difference between dot get and Dot query dot query is what you do after you've added a bunch of queries to Glide records or using ad query add encoded query uh not null query what have you and then you're tapping it all off with query to actually do the query now that's good for getting you lots of Records in the Glide record but what if you already knew you only wanted one record and you knew exactly what that record is in this example we're going to go to the sys user table and we're going to use dot get with the sys ID of the record that we want to actually retrieve and we're going to log out the first name and last name of that assistant record let's run the script and we see that that has got us blah blah blah so it went found one record and it returned us to results that we want now if you use dot get it is critical that you wrap it in an if statement because whether or not DOT get returns anything the rest of your script is just going to keep on going so let's see that in action okay in this scenario we have a specific user that we want to update via script so we've gone ahead and established a Glide record called Bob and we're using bob.get except you put a typo in the sysid and that ensures that we are not going to get a result now the rest of the script says go ahead and set the title to man with the plan and go ahead and set the employee number to zero zero zero one then go ahead and do the update now let's see what happens knowing that this is not going to return what we want run the script so you see the problem here you said update it said it inserted why did it do that so that's the danger you've instantiated a Glide record variable and you've hit update and now we know that if update doesn't find a record to update it will insert pretty trivial for just this lesson but imagine if this was tied into some larger more complex Solution on your production system so if our intent is to do something with records that we retrieved with DOT get we should always route the dot get in an if statement to control for the scenarios where we don't get results returned in this example script again we have a Glide record variable called Bob and it's on the sys user table except in this case we are wrapping the dot get in an if statement and that if statement says if Bob dot get and whatever the text is hopefully it's valid it's this ID for a record that actually exists and if it is it will go ahead and do the stuff you wanted it to update the title update the employee number and then go ahead and make that update to the database but if it doesn't return a record it's going to give us an info message no record return on variable Bond now your own output message there make sure it's good and informative this is just an example let's go see what this script does and we're hoping that it Returns the else because we know that there isn't a CIS user record with a sysid of typo and we see is probably taking the else course and it's output the message that tells us that it did not find a record in that Glide record query now this is a lesson that's absolutely necessary when you're doing Dot get on glide records but in abstract it's something you should always be thinking about with your code what happens if the thing that I'm doing the function that I'm running doesn't return the results that I expected so you should always in the back of your head be thinking about ways that you can test for the outputs that you're expecting and either return that output or take a different path to warn something that your code isn't working quite properly hope you learned something on this one and I will see you on the next one if you're a servicenow expert looking for better opportunities but maybe your resume or LinkedIn profile isn't doing you justice Reach Out to me via LinkedIn or the email pictured here as I offer both career coaching and recruitment services and if you're a servicenow customer or partner you heard that right Robert fedoric now does servicenow recruiting with a 1500 subscriber YouTube channel and mailing list and thousands of LinkedIn followers let's make sure your open positions get first go at the prodigious pool of servicenow resources reach out via the emailed picture gear
https://www.youtube.com/watch?v=WxszcO3aKPE