TechNow Ep 86 | Debugging Your ServiceNow Code
[Music] hello and welcome to tech now the web series for servicenow admins builders and developers on a wide variety of platform topics my name is chuck tomasi senior developer advocate at servicenow and it is my pleasure to bring you another topic in our series this time about debugging your servicenow code there are times when we need to get into coding in the platform and this is the episode to help you resolve those issues quickly we've got a special guest before you today but first let's get through the quick intros i already mentioned my name so this should be pretty quick i've been with servicenow since 2010 was a customer for a couple years before that and i'm creeping up on 40 years in the industry so it's uh it's it's coming up quick i'm getting to be an old man i'm going to start thinking about how i'm going gonna get off this train but it's too much fun so lots of other hobbies and whatnot that you can read on the slide i'm not gonna bore you with that i am going to turn it over to our next guest jeremy duncan thank you papa chuck uh my name is jeremy duncan platform architect here at service now uh 16 years of experience as an engineer in the enterprise mostly focused on it did start as a customer around 2011 so pre aspen uh a servicenow customer and have just fallen in love and and been an advocate for the platform since then i spent some time in the consulting world and recently here well recently it's been almost two years now here at servicenow i was recently dub certified master architect so it's been six months if you've got any questions about that program please feel free to to reach out and just a quick fun fact about myself i am a reserve police officer so i do get to take bad guys to jail or girls bad girls too to jail when i'm not working here at servicenow or camping with my wife and kids so enough about me i will pass it over to mr barnes well thank you jeremy i'm andrew barnes uh developer advocate and servicenow i have been with servicenow for about uh what's two and a half years now and i've been in the ecosystem for seven years i've been at a couple of customers and was at an implementation partner prior to joining servicenow in this role you can find a lot of my content over at developer.servicenow.com where we have our blog and our youtube from there and one interesting thing in the servicenow developer community is i started one of the first developer meetups and then once joining servicenow i ran the global developer meetup system which has over 60 servicenow developer meetups around the world right now most of them are virtual but you can also find those on the events page of developer.servicenow.com prior to all of my servicenow stuff i was running enterprise application systems for a university and then prior to that i taught computer science for a bunch of years because discrete math is fun and uh thanks for letting me join you today and talk about servicenow debugging so we'll move right on over thank you andrew to our agenda so andrew will be wowing us with the demo yeah andrew is being modest about some of the things he's done but i want to just just say thank you andrew you the community um will never be the same uh thanks to the focus that we've had on developer the folks the focus we've had on uh meetups the developer meetups the organization that we've seen around that um the baton has been passed in in some ways we've got over 50 developer meetups that i see that are global i know we've got a lot more than that but just the last time i counted and and um and being a leader of our nashville developer meetup i'm just grateful for the attention and the care and feeding that goes into that so thank you uh for for all you do there for the community so next up uh you might have seen this advertising the community linkedin well here on tech now a break point is a a podcast for servicenow admins developers builders of all skill levels join chuck tomasi and a special guest each week to discuss the now platform features cool apps that folks have built and so much more so if you check out that link below you can sign up or see the past episodes um that chuck and his special guests uh have had and knowledge let's talk a little bit about that k21 officially has wrapped but don't worry right uh you have access to the content as a registered attendee um you can see that with the link below it did wrap with a song or several songs actually uh chuck led us in a four hour karaoke event where he raised over four thousand dollars for code.org so round of applause for chuck on that and thank you for all the donations that came in for that great cause there's still a ton of content available to you as an attendee you have roughly six months to access that content don't hold me to that time frame but get out there and check it out and stay tuned for k22 i'm hearing sweet whispers of nothing that we might be seeing each other again right so look forward to that and if you're wondering what content is a must see that again i'll mention that link at the bottom again those uh the content that's there via that link is is what our developer advocates chuck andrew barnes and and brad tilton have curated uh for your viewing pleasure so check that out um and with that i'm going to turn it back over and i think we're going to have our first poll of the day uh so uh this poll uh is how often do you use the uh script debugger or breakpoints um so if you don't know what that is for example go ahead and hit n a but if you do know what it is uh let us know how often you use either of those tools the break points or script debuggers i could be daily once a week once a month let us know because we're interested in how often you use that functionality and we'll see how that changes over time in the future all right looks like half of the folks uh don't uh don't use it or don't know what it is um so hopefully our session today will potentially change that um but daily oh only 10 so let's see what we can do uh to potentially um you know inform you of uh the script debugger and breakpoints and other debugging things and with that i will go ahead and move us right on to our screen share so let me share my screen do we have any questions while i'm doing that chuck uh we have one person that said they're interested in updates that tracker so jason i'm going to refer you to get in contact with andrew probably linkedin would be the easiest way andrew barnes on linkedin service now there should only be one of them and he can tell you all about updates that tracker he said he manages over 250 servicenow instances oh wow that is quite a number of instances yes feel free to reach out to me uh via twitter or linkedin absolutely so you may also want to look at our december show that jeremy did was it december was it february jeremy for moving things around your instances i'll move it yeah it was episode i think 78 and that was last year yeah oh that was last year already okay yeah it's been a while yeah so there are other options to update sets yes there are and i believe episode 75 had chris uh chris on uh talking about uh ci cd um yes yes and we did a live coding happy hour as well uh where we talked about moving apps and and using uh the latest and greatest in quebec so lots of options out there for folks to go check we can take care certainly so uh if you can see my screen now i'm in agent workspace and i'm on the incident table and i'm going to show you uh so when you're debugging in-service now obviously i can't you know immediately just drop into a problem so i've had to contrive this problem a little bit to walk us through debugging because doing debugging on demand is a little difficult because if you don't make a mistake you don't need to debug very much so i've introduced a mistake and we're going to debug that mistake here on the show and walk through some of the tools and capabilities and how i go about debugging on the platform and we're going to see some newer features like script tracer and some of the older features on how to do debugging but right now i'm in agent workspace i'm i'm going to open a new incident and i'm going to fill out a couple of values so i'm going to choose fred luddy because he's my fun test user today i'm going to set a couple of fields and here we go and i'm going to set a problem record let's see this problem record and before i hit save i'm going to show you the business rule that i've created so i've got a story in my backlog i've created this business rule so this is a after business rule that's going to check to see if my incident is active and the problem field is not empty this is going to run on insert or update and what it's going to do is under advanced in the script area we can see that it's going to go and grab our problem record it's going to validate that we we have a valid problem record and then we're going to set a value on that problem record the contact type to the same value as we have on our incident subcategory so this is doing a little data synchronization for me and so let's see that in operation so i've got this new incident and right now if i open up this problem record let's take a look at it we can see let's see where is um let's go ahead and do our update and then we'll see what happens on our problem record so i'm going to hit save and now our expectation is that the problem record a field on the problem record got updated and did i not put that field on this form i was using the regular out of box form bad me i have it open right here let's take a look so the contact type was set to email you see the little has been updated by another user not in this tab was updated to email because that came from my incident record so everything looks great right yes right now everything looks amazing but if i come back and i'm doing my testing as i built my story and i come back and i update my instant record again so let's change the contact to virtual agent and we're going to add a description hulu and we'll hit save then we'll notice my subcategory on incident is is different it's that's bad uh let's go check our problem record and that's synchronized to our problem record that's not that's not what we wanted why did this happen so we've now seen uh i'm in a scenario in which i don't know why i have uh this problem right um i i'm on my incident record and i've made a change sub category has changed to a value that doesn't even look like a valid value and i didn't i didn't cause this so now we have to start our debugging process so to do that let's go look at what our options are so i'm going to return to the normal you know developer view of the platform the main view here and i'm going to go to diagnostics and just show you some of the options that you have available so under system diagnostics right here we've got a whole bunch of options to inspect your servicenow instance under that we have debugging so there's a whole category here called session debugging and i can hit enable all or i can go and choose a particular area to do debugging so i could perhaps maybe i think it's a business rule so i could turn on debug business rule or i could debug sql or let's see debug security i don't know what's causing this yet so i could potentially turn on debug all and see what's happening that's going to give you a lot of information that is going to give me a lot of information absolutely so in quebec there's actually this really cool new feature in quebec called script tracer which you'll find all the way up the top here of the diagnostics window script training andrew we're not we're not seeing that window there it is uh yeah i just opened it so okay uh there's there's the script tracer window which i've just invoked it's not doing anything yet and it may be familiar to some of you who are using the script debugger you remember that poll question we had are you using uh the script debugger so let's let's try the script debugger first and see if that gets us what we need what so we're going to try that out does this do client scripts and scripting and ui policies as well so er the script debugger can uh debug any um script field that is a synchronous operation so yes to some of them so yes to those of those that are synchronous so for example a business rule a before or after business rule synchronous a asynchronous business rule not uh not able to use the script debugger for that so in order to invoke the script debugger i'm going to go to my script and i'm going to click the line so line five now i click that that sets a break point if you right click on it you can see to remove breakpoint add a condition so i've set a breakpoint now what this will do is the next time that i invoke the uh this business rule it will pause my operation at that point in time so let's go invoke that so if we go and the pause is just for you on this session if anyone else is on this instance they're not impacted absolutely that is certainly true even a different login from me won't be affected as long as it's a different session so in my script debugger window you can see i've got this breakpoint that's ready to go right here and let's go invoke it so i'm going to hit save in my window and this should cause yep so now we have hit our break point where we set it on line five and in this interface usually the most important area for me is going to be this top right area where i can go inspect the object's values so i can go take a look at the current glide record which is my incident record and see what the values are and if i scroll down here to subcategory you'll see that it has already been changed so what's happening is there's something earlier in the execution than my business rule if it hadn't changed yet then what i'd be able to do is step forward and find the operation that's causing this but unfortunately since i can now show that it happened before my business rule was invoked i'm i'm i'm a little bit stuck right because i can't go backward in time from this execution i can only go forward in time from here where i've put this breakpoint and let's step forward just so we can see what that looks like so i can i can step forward in my execution and it will even show me the next business rule and then the next business rule and so on and then i can go ahead and hit uh resume execution to to let that that thing finish but i've now shown for sure that the thing that's causing my problem it's happening before my business rule is being invoked so prior to quebec one thing that you might know how to approach a problem like this is i might go to the business rule table and even potentially sort it to the incident table business rules and just look to see if there's been a business rule that's been updated recently because that that could show me some potential places that were likely to be causing my issue i don't see anything jumping out here i've got my business rule and then it's been months since anyone adjusted a business rule so let's try one more step uh that i would have done prior to quebec so we'll just inspect that script field and we'll see if subcategory is in here one thing i did prior to quebec is just a lot of gs.info statements potentially uh that that could help me if i could find the business rule that might be causing the issue yeah um but but right now i i have no idea what's causing this issue um so i'm i'm in a bit of a quandary so i could go to uh debugger i've gotta spell it right i could go enable all of the debugging and see what's happening but in quebec i've got i've got a new tool so let's try that new tool out that new tool is in the same window with the session log and the script debugger is called script tracer so i'm going to start trace and now any operation that i do in the platform that's synchronous is going to show up all of the logic and business rules that are running synchronously against my transaction they'll show up here so let's go see what that looks like so i'll return to my incident i will go make another change here let's change to walk in and let's even you know uh put some description in goodbye and for tracer so i've changed a few values and yeah let's even change the assign to because fred probably shouldn't solve his own thing so let's hit save so let's show you this interface a little bit now that that we definitely have a working uh version here so i can use a filter um so i can search for particular things like sub category search for a file or field name i can switch i can toggle the business rules on or off let's get all of those back let's toggle off ui action toggle it back on so i have ways that i can narrow this down if i have a lot i only have three here so let's go check out these three um i i gave the game away a little bit because i told you that it was this one but let's see how i would have figured that out if this was my first time coming to this window which is let's make it stop flashing that's a direct question right out of the chat is i've got 50 business rules how do i find the one that's affecting my record or affecting that field absolutely so if you've got a bunch of them uh you start filtering out the tables and feel and types that are most likely not causing your issue so for example if you've got a bunch of task sla ones running you'd you don't that's a very unlikely place to to be causing your issue with a the subcategory changing so you can filter these down relatively quickly what you can see in this window now that it stopped flashing at me is in the state box here i've got a little check box that says show only change values and so what this is doing is it will tell me all of the field names and the values that have changed during my transaction so i've got type user value system name and i've got no i got distracted that is not even the right one all right so we'll show how it it should work um which as you can see with this business rule is it's showing me all of the values that should be you know here's the field and here's the change you can also see all of the values but i like to narrow down to just the ones that have changed you can go view the script you can open up the whole script from here you can view the transaction details which this is similar to the script debugging window that shows you the transaction details here and we can go if this is the thing that's causing it if for example this said subcategory is changing to one then we'd know oh okay this is the script that's causing my problem um unfortunately i i don't actually uh i i'm not invoking it correctly for some reason uh it was of course working right before i started um but let's go see what's causing our issue since we actually know what that is so so andrew just a quick question like back in the old days i can remember watching a particular field is there a similar functionalities we're using skip trade excuse me script tracer and any of the new functionality um to where we could we could narrow it down to a field that's changing that we want to go investigate so it won't show you just one field it'll show you all of them so right now there's not a capability to just watch one field in particular it'll actually show you all of the tables all of the records that are being affected in your transaction not just one because the thing you're debugging may not be just the one field on the incident form you may need to see the bigger picture but that's that's something that we could potentially add in the future so here is my script that's causing the problem and it looks like on the incident table there's this sync field that's got a property that is sending to a script include that's causing our issue so this script include is updating a record for me but i'm passing it the wrong field so let's go take a look at this property and see what it is and we'll see that for some reason the developer put subcategory in this property where it should have been the active field and so this is what's causing my my issues so if we change this from subcategory to active and we try and replicate our problem which is changing subcategory and hitting save we'll see we're no longer updating subcategory which is exactly what we wanted we wanted subcategory not to be adjusted so now i need to reach out to the developer that adjusted that property figure out why they did and figure out what our our next step is so that is a script tracer then the next thing that i'd like to show though is what if scriptchaser didn't didn't solve my problem like potentially it didn't here i really wanted it to but it didn't the other things in this window are similar to those settings i showed you in the app nav so i can click settings here and i can select any of those debug options here so i could for example turn on business rules detailed and hit enable and let's see what happens when i perform the same action which is updating this incident and changing the subcategory and hitting save we'll see here all of the business rule details for which business rules ran during that transaction so let's let's see if we can uh look for a subcategory here which we won't be able to find it because we've already turned that off um but what we will be able to do is our flag and we'll see that this one ran this is one of the things that ran during that transaction which is potentially one of the things that caused my issue so there are a lot of different ways to go after debugging and script tracer is just one of those new tools again i'd like to highlight that it is for synchronous operations so if you have something that's you know not a synchronous operation or is not running in the same session as the user you'll have to figure out a way to sort of coerce that to be synchronous so potentially you could toggle uh your synchronous rules to run uh or you're not synchronous rules to run synchronously um but that's that's one of the ways uh to use these tools so that's the script tracer the script debugger and the session log the last thing i'll show you is one of the things that i did a long time ago and i haven't had to do in a while but it's still a very useful uh tactic when i'm looking at business rules i don't know if you've ever done this uh jeremy or uh chuck let me turn off the no i've never done a current update [Laughter] so one thing that i i will do is i will go to the business rule table if i'm sure that it's a business rule and i just don't know which one it is um i i have done some binary tree uh sorting here and for example i will go okay well there's 28 business rules on the incident table that are active let's go turn off 14 of those and so i'll multi-select half of these and i will send this scoping this is where scoping can be fun that's assuming everything's in the global scope that is true and i'll turn it off and then i'll perform my operation and if it's still causing the same issue i'll know it's not those 14 and it's in those other 14. uh so that's another little debugging trick there are there any other hints or areas of debugging that you might want to suggest to the other folks other than the tools that i've shown here i've got one more in my my toolbox that i'll show if y'all don't have a suggestion right now so as we encourage people to migrate from business rules to flows in a lot of cases and they are a much better solution for many of the business rules less scripting of course how would you get into that to say wait a minute um it's not even part of the script it's out of the scope of that where would you go to i know this gets beyond the topic of how to debug your code but there may be times when as people migrate to go it's not in code anymore how would you recognize that that's a super excellent question chuck and for example the script tracer won't currently find that change in a flow right now you would be able to find it from the record watcher potentially but the script tracer wouldn't find that the best way to debug flows is actually in the flow execution so i've opened up flow designer and gone to flow executions and if i look at created by and narrow it down to the the table in the operation or flow that's running i can open up that execution and inspect what happened during that execution this is actually one of my favorite areas in the platform is the flow designer execution tab because it it brings all of that data that i would have potentially logged somewhere else and and surfaces it right here in one interface for me so i can see here is the trigger for this flow and it will show me i can open up that record from here it'll show me the values the runtime values of all of those inputs for this trigger and for all of the actions i can also see here are the values the runtime values of all of the things that this update record action we're interacting with during this execution so it really centralizes all of this stuff that flow interacted with right here together for me so this is this is a great area of the platform the next thing that i wanted to show though was adding a log point chuck i believe you mentioned well i'm gonna i will add in some some uh you know gs.infos and uh wherever sprinkle them around to tell me values and look we've even got some here in this this script include a gs info here right yeah and what that leads to is it it actually makes it harder to go into your system log and find what you're looking for when you've got these sprinkled all over the place especially if you're you know dumping out a stringified json object it's like all right what am i looking for in this log because it's now so polluted with my app and everybody else's changes with gs info gs log gs debug everybody's dumping strings out here i can't find what i'm looking for in this particular case so i'll go ahead and show them log points i love this awesome so as you as you alluded to at a log point is right here along with my ad break point and conditional break points which invoke that the script debugger window add a log point will perform a similar operation but for logs i can you know log values here i can say the current field is and do current dot and then say field and get those values of those variables how much code can you get in there oh that's a good question chuck i i don't know i've put up i've put some fairly long log points here uh i haven't run into a limit though let's get the display value for this guy and yeah now i have a log point which is also visible over in the script debugger tab has my log points in a similar way that they do for the breakpoints so i can see here i've now got a log point in this script include super helpful and is only for my session and so this helps you not have to store logs that that are transitory like this line here this this doesn't belong in production i should remove this before i promote it up but if i use a log point then it's even easier because that won't move at all when i move this code up and i can even add these in production so if you're trying to debug something in production because you can't replicate it in your sub production you can add a log point in production and you're not actually modifying the record so from a change perspective you're still good to go there so log points really can help with that debugging and production for those production issues at a log point where you know throw script tracer in find the correct uh you know script include to work on and put in a log point where where you need it even in production so with that i hope these debugging tools that i've shown off today script debugger session log log points breakpoints and script tracer will help you debug on servicenow a little bit better and i'm going to stop the screen share and then i believe we'll move into our next uh poll question yeah while we do that you asked for some other suggestions andrew i know we were talking a lot about server side stuff um so the one other place i go if i can't find a particular field that i'm looking for or whatever a couple things that help me snutils so i can see the tech names of the fields that i'm looking at and also cis metadata i know it's querying a lot of the back end but cis metadata digging in and finding a payload like where maybe a one was hard coded um looking at a you know particular table that way um another thing to think about like is we have a lot of portal devs out there that are debugging portal issues as well there's there's code there that you got to think about i love using using my browser tools so f12 you can see your bangs you can see stuff that are going on on the browser side as well so lots of other cool tools out there to consider as your because there's many different places that that code is is running what's going on behind the scenes so yeah if you want to get more into if you want to get more into service portal debugging there is a video on these service now i think it's support or community channel i can't remember i keep referring to it because i did the voiceover for it but it shows you how you can debug widgets and get into google.log to um i think on the mac it's option right click or control right click and you can see the data schema it's like wow that's that's handy stuff we also have one in the chat from john says console.table or console.log to see your objects as you put that into the client script because sometimes client-side debugging can be really challenging absolutely and along with the browser developer tools there's also some really excellent plugins out there i know you mentioned sn utils but when you're doing service portal debugging there's some great uh you know web developer plugins around angular and bootstrap to debug your service portal that that i find handy as well so we've got a poll up so after watching this session are you likely to use the debugging tools so uh hopefully this has inspired you a little bit to uh you know use more of the debugging tools and and and get you familiar with some of them uh that that you may not be regularly using so let us know in the poll here while we're doing that do we have any questions we've got one says sean says are there any specific roles associated with granting the ability to do debugging ooh excellent question um aside from sure yeah i'm not sure the answer to that one so admin certainly has that uh but they're i i'm i will have to get back to that so we will answer that one offline uh because i'm i'm not sure are we ready to take a look at how we're doing on that poll yes i'm i'm dying of curiosity let's take a look somewhat increased about the same and greatly increased wow that that's fantastic so 46 somewhat increase and 44 greatly increase uh so that sounds like uh this was helpful to you uh which i'm super excited that that you found this helpful today so with that let us move into our top takeaways so for me the top takeaways from today's session are use breakpoints so when you know which business rule or script include you know has an area that you want to inspect the values throw in a break point then step forward and you can see all those values that will take you into the script debugger which will show you all of those break points allow you to see the values of all of those records at that time step forward in time you can also see all of the log points there in the script debugger area so super valuable tool there in the same window with the script debugger if you're in quebec or forward is script tracer now script tracer again only and breakpoints only work on that synchronous code but but when it is something synchronous those are really powerful tools um and there's nothing uh licensing related to using any of the things that we've shown today so script tracers script debugging log points break points those are all core platform foundation items and you can use them today if you're in a quebec instance and if you're not in a quebec instance the only thing you'll be missing out on um is script tracer unless you're you know running uh berlin which which no one is anymore [Laughter] so thank you uh for for following along and now i'll hand it back to chuck all right just want to give you a real quick promo we do have this as a monthly series tech now comes out once a month and our next month episode is going to be on devops we mentioned that chris faulkner was on a little while ago about ci cd i think that was march of 2020 we're going to be visiting that at all at a higher level with charnlin he is one of our product managers around the whole devops stuff this really tells the story and where we are on this journey we are by no means done implementing uh devops to help improve and make it easier for you to get your changes from dev to production as quickly and as easily as possible with the highest quality so invite you to look for that we do have a registration link if for those of you those of you that are watching right now for in the resources panel you should have a registration link for uh the next episode the june 29th episode of tech now so register there if not we will have that on the community shortly after we post this uh some other resources i mentioned the community and i forgot to mention the true spirit of community we really have a great q and a session going on here john has told us that the role script underscore debugger is used to run the script debugger that's almost too logical for us but i want to point you at some of these resources as well it should have been yeah you'd think the uh developer portal free personal developer instance you can try all this stuff out in a risk-free environment all the api docs for scripting so if you're under scripting great place to go a lot of information over there blogs and newsletter great stuff and we are still slamming these out once a month we've been doing this since 2013 if you want to know where all the other ones are we mentioned chris faulkner we mentioned uh jeremy's session we mentioned episode 75 it's a go over to devlin.sn tech now it will take you to that master index of every single tech now episode with the embedded video and the discussions that have been going on if we didn't get your question answered understand that you know we're a bit limited on time here we will answer it offline and you will get an email with the results where to go closer to the community right to your question so with that i think we've just got a few minutes left for q a uh what have we got jeremy while i was talking yeah so we had um some questions about instance wide searching uh my response back is you know sys metadata is a good option for for broad stroke searching keep in mind within studio your studio ide you've got searching capabilities there which will search the your application that you're you're in it's not a great global option necessarily but it's there for you definitely multiple options for global searches out of the box it's just finding the one that works for you that you that you kind of get used to right uh there was also mention of a you know individual field uh but you know as we mentioned before a suggestion of field level tracing in the future so thank you all for your suggestions as well as your questions um so what there's in here there's another one about uh searching um a given string across the entire instance so the jeremy uh you mentioned the code search tool in studio for a given application but there is a check box in that interface that will search all applications you can use that interface to search for any uh code in nearly any script box in the platform right there from in studio in the top right code search another question that i saw come through was asking about uh the log points um in and how to enable that um and i i did not cover that uh which i will do right now which there is a system property to control log points and by default that system property is off so you'll need to go to your system properties and adjust that you'll you'll have to adjust that property and i am going to not remember the name of that system property but it's got log underscore point in the name so if you just go to your system properties and search you will find it and hopefully i can come up with it here shortly which is log points and it is glide dot debug dot log underscore point and you set that true to be able to enable that log point uh when you're right in the gutter of your script box all right i think we got most of them we are out of time thank you everybody for joining us today don't forget there's more content if you go over to the developer portal at developer.servicenow.com you will get it all subscribe to the youtube channel the servicenow developer program youtube channel you get notifications when we do our live streams there are several of them and of course breakpoint podcast so enough of the commercials thank you again for joining us thank you andrew for all of the uh wonderful information demonstration and uh we will talk to you again real soon thanks everybody thanks everybody thank you [Music] you
https://www.youtube.com/watch?v=uPXKyTfKW1g