logo

NJP

ServiceNow Query Validation Made Easy: Demystifying the isValidEncodedQuery Method with demo

Import · Jul 10, 2023 · video

[Music] hi all welcome back to our Channel today we have an exciting topic to discuss that will surely make your life as a service now developer a whole lot easier we are going to introduce you to the magical world of is valid encoded query method so grab your favorite drinks sit back and let's drive into it but before we jump into the details let's take a moment to understand why this method is so important as you know as you may know service now is a powerful platform that allows you to automate and streamline your business process and to work around a lot of data right and one of the key aspect of service now is working with queries to drive and manipulate data now imagine a scenario where you receive user input from up through anything okay and you need to validate and process that input to perform a query to the to your database this is where the is valid query method comes in to your rescue it's a built-in function that enables you to validate the provided query okay string efficiently the is valid and query methods allowed you to check if a query string is correctly encoded according to servicenow query standards it ensures that your query are secure providing an unexpected Behavior or potential security vulnerability you can consider it's like having a trusted gatekeeper to your data so access okay so without wasting any further time okay let's jump on to the um onto the system so as you can see on my screen this is my personal development instance and for this particular example let's do one thing we'll go to the task dot list so consider the scenario that I want to update all the tasks for incident okay for the incident we want to update so and the critical may be different but for ease of understanding I am doing this okay so what I will do I will just put the filter condition first okay if you want to do wire task so I can to type which is nothing but this class name okay type 2 incident thank you and let's do one more thing and active is true okay and if I run this query out so how many record it returns it returns 486 records let's work on this record so I will just do some cheating okay all shortcuts okay using acids so what I will do I'll just go over here and I will do background script on the list View okay and both we got the code for it and here what we'll do we will remove the set limit once okay because we want to see all the records how exactly just behaving and also I will not go with a while for now and just to save the performance and we will include GS dot info which is dot info and I can um make it two some record count which is Plus um list dot get row count perfect right and let's try to run it out so if I run the script we are getting 46 record that's completely perfect now let's do one thing let's mess around the encoded query so I will just add sys class name make query invalid okay now if we add this you know that this query is not making any sense now okay because there is no column with the name sys last name make query invalid right so ideally it should written Zero Records now that's our expectation right let's see and run this again so if I go down to see how many records are there so we can see three three four four one we are expecting zero but it's giving all the records to us isn't it strange right so what exactly is doing it's let's try to figure it out so what I will do I will just copy this query and try to put the same on our list to you okay just to see what all data is coming over there so I will just go to the list view again as in utils I'll just double click over here and I can add the filter condition I'll just put the filter condition over here I click ok and let's see how many records is showing on the list here owl so you can see the query has been formulated over here this class name make query invalid okay incident which is Rocky react equal to true and we can see 33441 record oh this is my issue why because it's not whenever the query is invalid actually it throws all the record to you okay yeah I know you are also not impressed with this so how we can avoid this thing out right you must be thinking about that so for that you can avoid this thing by enabling a plug system properties with the name I will just go p and I will put p stand for property on the S Note is okay I will just try to search for this particular property and by default it should be first but it's not present only okay if it's not present you can create your own so I will just click new I'll just go I will just paste this particular name it's Glide invalid query if what it said if it's Android query little no rows okay and we will make it to true and we will submit it out so we have submitted made it true now let's go jump back to wow background script okay so if I run this again okay run this again let's see the difference so I ran it out okay and if we scroll down down so now you can see script record count is zero that means now it's not returning all the records out let's verify the same on the list view also okay so I will go up I'll just copy this query out says I will just disable this out and I will just copy come on copy this out and we will go jump back to this while it's coming again some this and what will go to the history and Industry we will go to the task let's try to put this query now okay so I will just put the same screwed up query and let's see now it's not returning any record okay Society is not valid okay so it's not running any record with this query which is perfectly what we expect right now let's go further let's go somewhat further so if I there is also one function to rescue it out because if you see uh if you if this particular property disabled okay and you want to see if certain queries invalid okay or if you want to throw certain exceptions around the same so you can how you can do that so for that we have a function so what I will do I will just put this query as a different entity control X and just go over here variable query equal to let's put this string one okay sorry it's already been there in the inverted commodity so I'll just remove this out and one so this is a query and before adding it to encoded query what we can do if list Dot is valid and ordered query so this is a function okay which a lot of people are not aware about what exactly it does it looks into a cloud record looks into the query which we will passing we will be passing so I will be passing this query right and it will check whether this particular query is valid or not okay and if it's valid then it will return true if it's not there is a written false okay so what I want to do I want to what I will do I will just copy this out Ctrl X I'll paste it over here so if it's a valid query okay then what we have to do I will add it as a query okay and let's go with the row count if it's not then we want it to go to else and there we can throw an exception okay let's give the exception hey bro the query you have provided is invalid perfect right okay let's try and see now what happens with this environment query so if I run this out so and let's try to get this message if if we get this message Ctrl f [Music] hey so it's not coming over here here you can see Mozilla JavaScript JavaScript exception so exception got thrown okay which says hey bro the query your provided is invalid on which particular line line number nine perfect right on line number nine we are throwing the exception this is how you should code okay this is how the coding should be there this is also not the perfect one let's catch this particular exception now okay so how to catch it out so what you can do you can do try and try you can add this code out control X okay so what we are doing we are trying it out okay we are trying this thing out and if it's not working okay if if it throws exception if there is any exception which we are doing online number now it turned out to be 13. so if it's exception then we have to catch it out so you can do catch and in catch you have to type the exception which is ex you can give anything and now you can play around this exception whenever you get this exception out so how you can simply do what we and GS dot info free I don't know we can give we got an exception with details as colon and then you can simply add Plus X right now if we run it out so we will not get those many logs okay so we got hit that particular script we are getting this one we got an exception with detail hey bro the query you have provided is invalid okay which we are passing over you so this is how you should code okay always try to validate the query okay because if if your query is not valid okay it must grow up all the records okay instead of a bunch of subset of records and it it's a used plunder okay so this is all for today I didn't update it out okay because everyone knows how to update the records right the main thing what I want to show I've already forecasted it over there so this is all for today's tutorial I hope you like it out okay if you like it out please subscribe to my channel so that you will get notified with my upcoming videos till then stay safe stay happy and have a nice day thank you foreign

View original source

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