ServiceNow Advanced Reference Qualifier | How to use a Script Include in a reference qualifier
hello and welcome back in today's episode we're going to talk about reference qualifiers and more specifically than that we're going to talk about Advanced reference we want to get really specific we're going to talk about Advanced reference qualifiers that you can use to call a script include and do some more fancy stuff if you want to see the clever stuff stick around and I'll show you how [Music] we're going to look at Advanced reference qualifiers but more specifically we're going to do one that calls a script include does some more fancy stuff and then returns it but that'll be later on in the video so before we get to that let's just talk about what we know about reference qualifiers already so they apply to field types that are reference types so we've got different types of fields in service now one of which in reference and reference qualifiers uh clues in the name only apply to those types of field now those types of fields you can use on The fulfiller View so kind of the back end and stuff like uh caller IDs assignment groups Etc but you can also use the mon catalog item variables which is what I've done here because I just think it looks nicer on the eye right so we can apply reference qualifiers to reference fields so what different types of reference qualifiers have we got let's just just deal with that so let's just skip over here so we've got a simple qualifier which allows us to play with our condition builder in this case I've added in active is true therefore what that means is it will refine the data that I'm referencing in this case the user table and only bring back active records why would you want to bring someone back that's inactive odd um but that's a simple reference qualify and you've got access to your condition Builder and so on and so forth we have a dynamic reference qualifier which we're going to cover more in a separate video I think that that warrants its own and then we've got Advanced reference qualifier here which allows us to put things like encoded queries as you can see in here this is a format of an encoded query so we can start doing that and this is the one we're going to play with today so let's just skip back over to our item so we already know we've got a requester reference field um and we've got a simple qualifier that only brings back active so when we select that we can then pick our friend Abel and what we've done with the device one is we've said our device um and this is looking at the Alm Hardware table so these are assets right now what we've said here is our device that we're going to select should be relevant to Able therefore we don't want to select all devices on that table it could be many but we only want to select devices that are assigned to Able that Able's got so if I went if I say devices I mean like laptops or mobile phones so now when we select this this will only bring back one therefore Abel can select yes this is my device why would she do it on a laptop I don't know but it's the purpose of this demonstration let's just quickly take a look and see what we've done um on this variable so we go configure variable we've already seen the simple one let's just have a look at a bit more advanced one it's not too fancy just yet we're getting there stick around so this one is an advanced but when we select Advanced so we've already seen that we can put in a simple kind of encoded query now we can start being a bit more advanced and add in some JavaScript to do that we need to declare it is Javascript at the front so if you're going to take a screenshot or pause the video do it now and then you can you can write that down um so we need to declare JavaScript JavaScript and then we can start accessing it in the normal way as we might do in a um server side script so assigned to equals and that's in uh in our common speech marks you can either use singular or double I always use singular don't know why um well I'll just make it consistent whatever the scripts do anyway and then we'll go assigned to equals current dot variables.requester where did I get that from well current dot variables is the way that you access um the fields on a catalog item a current dot Fair variables and requester is the name of that variable so we come back here requester okay so now that makes it more dynamic because if I didn't have that I would have to come here I'd go assigned to is and I'd well able and then that's not Dynamic right it's it's not relevant based on something else that's on the form so that is one form of advanced reference qualifier that would be pretty sneaky and the other way we can use an advanced reference qualifier and let's face it this is why you're all here is to access a script include and do some more fancy things that we perhaps can't do or shouldn't do in a simple kind of text field I mean we can write JavaScript there so we can do lots of fancy stuff but why would you when you've got a proper script editor in something like a script include and it makes it more scalable that we can use elsewhere and that's what we need to be thinking about perhaps we've already got functions that we might want so in this scenario let's just go back to our item um just so it might give us more context so in this scenario I think what we're going to do is we're going to leave the simple qualifier alone and we're still going to use able so we're going to have able but instead of bringing back devices that are assigned to Able I want to bring back devices that are assigned to Able and that are in the same company as able okay now we can do that of course we can do that based on an advanced reference column a reference qualifier like this so we could just go Plus company equals we could we could do that but we're not going to for the purpose of this demo I want to show you how we can launch into a script include so the first thing we need to do we need to go and create our script include so let's head over there the one thing I would mention actually um I'll open a fair few tabs um the one thing I would mention is whenever you go through credit script include it it's I would urge you to go look at existing script includes um to see if the functionality you want is already there or if it isn't there it might just be there and then talk about adjusting it so if it's your use case so in this example we're going to call it catalog well I don't know get assets um you can probably think of a better name than that but we'll go with that we have our script include here and what we're going to do is create our option called okay that's it we go and so that's our function so now we've got our function let's just save that so we've created our scripting too we're creating the um the architecture so we've got our script include and then what we can do is grab script include name and we just say new what did I do that so this is us saying a new instantiation of um the script include dot function a new function is get assets okay like that and we need to pass something in to make it um relevant for the situation so get assets get assets of what um oh God put a double s in there Why did no one tell me right um so what we want to do is pass something in so in this case we're going to pass in current dot variables dot request that so we're passing in Abel's um sysid as it were okay so into our function we're going to pass in able so that's the user and then now we can start doing some scripty-based stuff to return what I'm going to do is I'm going to pause the video here I'm going to quickly write it so you don't have to watch me and then we'll just explain what I've done okay so I've now wrote the script in the magic of um video editing it's probably only taken me about a second in your time but let's just go through that and explain what I've done right um so let's just remind ourselves if we go back to uh device reference field this is our reference qualifier JavaScript we're declaring that we're calling our new script include we're calling the function and we're throwing in apologies we're throwing in the variable of requester and that's this here so now let's just expand this let's just take a deeper dive into what this actually is can we make it a bit bigger me more than you right oh look here we go okay okay so let's just split that this out Let's ignore that bit for now so with our function get assets so we're passing in the user sysid we are now declaring an empty variable empty variable an empty array we are using this function get user record to go and return the actual record now remember we're passing in the user sysid we want the record back the whole object right and that's what this function is doing get user record if we just do a quick search let's see what that's doing down here that's that one I moved right so let's take a quick look at that that is getting sending in the sys user ID now the sysid and it is querying the sys user table for that ID so able and it's returning the whole record so we started off with the sysid now we have the whole record why did we want the whole record I hear you ask well then when we come to do our Glide query we're looking at the hardware table which is the same and this is really key this is the same table that our reference is using but be careful there be careful you can go off as I've done here and do multiple queries around user tables you can look at change uh references if you really wanted to to say what um CI is associated to that asset are being used for a change record but then return change IDs but he is when you send it back to the reference field make sure it's the sys IDs that is on the table it's on uh the references on sound a lot of waffle if you get into that issue come back to this video it'll make sense trust me so excuse me so we're now doing a query on the hardware table we are now looking at a software assigned to is the user so the sysid Abel or a door condition the company is the same company is able again this probably isn't a real life scenario but I want to show you that you can do different things right so userecord dot company so user record is what we return from this function down here so user record dot company so this is the query device Hardware assets where the user is able or the company is the same as able then when we've got that while we've got one of those or a few of those we push those into that array devices array that we declared on line eight so we're pushing the sys ID get unique value that's what that is of the device the hardware okay so let's just let me just quickly summarize for you we are coming in to the script include with Abel sysid we're then querying the sys user table or able to return the record we're then doing a query on the the hardware assets table to look for assets that are assigned to Able or has the same company available and we're pushing that into an array meaning a long comma separated string how do I know it's comma separated because when we return it we're returning devices.join which is joining the the um the items inside the array with a comma and we return it in an encoded query fashion so sysid i n okay so what we're returning back is an encoded query so it's essentially saying show me the devices where the sys ID is in this list and that list is the the array that we're creating okay hope you're all following let's go and test it uh let's just give it a quick refresh oh that's quite large there we go see what that looks like right so able let's select you and remember when we selected the devices last time we had one so how many have we got this time oh we got 41 now so again we've got Abel's device plus all devices that are in the same company let's just have a quick recap what have we learned that we've learned we've that a reference is a type of field that we can create we've learned that there's three different types of reference qualifier we've got simple we've got Dynamic and advanced simple is allows us to use our condition Builder that we can simplistically do we've learned that we can do um that Dynamic is going to be a different video and we've learned that we can do an advanced reference qualifier to where we can actually type in JavaScript and then start putting things that are quite simple that make it relevant or make the values relevant based on something else that's on the form and we've also learned that we can create a script include one of the key takeaways there is check other scripted Clues to see if the functionality already exists but we've learned that we can call a script include we can write some kind of fantasy or non-fancy stuff inside the scripted food and return an encoded query which is a list of sys IDs key thing to remember the CIS IDs have to be on the same table you're referencing okay so it looks like that's it for another video from the service nerds um if you are new around here I've considered subscribing it'll be ever so nice of you if you're not and you've been here before then again I apologize as always but make sure you're smashing the Bell icon so you get notified when I upload videos just like this one until next time I've been Russ this is service nerd happy holidays [Music]
https://www.youtube.com/watch?v=zvSwzPRgAbw