ServiceNow Introduction to addJoinQuery() method with demo | ServiceNow addJoinQuery ()method Learn
[Music] hi all and welcome back so in today's tutorial we'll be looking on one more glad record function okay which I find pretty amazing and I hope that after after this tutorial you will also find the advantage of this particular function and the function which would be exploring today is at join query okay so uh what is what this function does so it adds a filter to written record based on the relationship in a related table okay so for example uh find all the user that are in the database group okay so so over we have a relationship with the user record we have since you use a group member table related table right so based on that if you want to Define certain queries or if you can take another example would be uh find all problems that have assigned to that has assigned incident okay so if or another problem which you will be looking is like if you want to see he you want to find all the incident okay which has attachment attached to it okay so just think okay how you'll be proceeding okay if you don't know this function so I will simply go to script background okay background script okay and we'll be we'll be trying this use case so the use case for uh first we will be going with the Primitive then I will show you how you can ease it out using this function so the use case is like we have to find all the incident where it has attachment attached to it okay so first and foremost is active incidence let's like look for accuracy so first we will go for variable GI incident equal to equal to new Glide record right we will give it as incident then we want to check on the active records right so we will go for GR instrument Dot dot add active query perfect then the next step is like we we got all the incident value which I have to now okay now we'll be like um GI incident dot query so we'll get all the incident which are active Okay then over here okay I I hope this will be the method which will be following okay query then while GI incident dot next okay now there are two ways it will be based on your choices either you are iterating over each and every record or you will take all the active records all all the active incidents right push it to a array and then you will be doing like uh Society includes in this kind of thing okay but I will iterate over each and every record okay and over here I will Define a function to verify uh whether it has a attachment attached to it or not so I'll give it a flag as flag and over here I will Define a function which will take one attribute okay what it will attribute will be it will be GI incident says ID okay this is attribute and we'll be passing it out at the last okay and we'll be doing GI incident dot get unique value okay so it will return the sysid and what we'll be doing in this self invoking function is uh we'll be calling the this attachment table so uh it will be variable grad equal to new record and it will have this attachment attachment and in the query what I will be doing is uh I don't know the exact column for this so I will still I'm just put that dot left and over here the column name is table Society right I don't know the packet name also for the same so just oh I just clicked on this right so we want to we want to look on this record right so I so to configure table for the people who okay or not aware how to see the backend name okay so here we we know that it's getting stored into tables ID column right so this is so the backend name for it is like table Society so we'll go over here and we'll do grat dot add query foreign will give what this particular column should match the CIS ID of a record so it's gr influences ID okay and if it matches okay so I will do it grad Dot Dot query okay and we'll be doing okay it has any record okay so if it has any records so it will be like GR 80d dot next okay all don't have to go for we can go as next if it has something in it then we'll be like will return true if not okay if not then else will return false I think the code is easy right so this is how you can achieve it and now once we come out of this function so we have this flag okay and over here what we'll do we'll utilize the flag to print the incident which has it so if if flag if it's true means if it has attachment attached to it then then we will print the name so we'll give GS Dot info and we'll give CR incident dot get display value a lot of code right so let's see I hope it should work okay let's give it a try again try it out so if I run this out okay so it gave tons of incident right okay I I I will just how I will get I can do one thing with this I can also push it here I will give I will define an array variable count equal to I will define an array and at the end of this array we will be like count dot push we can push anything because we just have to find the length right so I'll push this display value only okay perfect and then we can simply take a count so I will give dot info and info what count Dot then okay so it's done let's see how many records we have so we have 53 records according to this query okay which has which has incident attached to it right let me just check once again yeah so we have 53 in attached at 53 records with the incident attached to it okay this is good now if you want to do the same okay using the joint query then let's see how exactly it works okay so we have to go over here okay we don't have to bag our head to drive this big logic okay okay so you can simply do variable GI accident equal to new light record it will have students then it will be like again here incident dot add active query because we want active records only now the change happens okay so here we'll be using here say dot all right add joint query okay so what this joint query does it it accept three attributes okay three parameters okay so as you can see the first attribute is join table which particular table you want to make a join with then primary field is like a field on the table Miss on the parent table which is incident table for us right now What fields uh defines the relationship okay so it would be side default right and the the last thing is the on another table so we are doing relationship with attachment table right so on attachment table what field you this uh field which you have set a primary field corresponds to okay so join table field okay so in this case we have a table called CIS underscore attachment right and we will be doing join right so join will be what so it will be a field called stress ID because on Instagram table we want to mark it as say study and what is the field it corresponds to when it comes to the incident table okay so it will be table Society right so where this Society gets stored on the attachment table boom and then you are done with the code okay now if you do GI incident dot query okay okay and then while yeah considered Dot dot next we will simply print it out okay GS dot info and it will be GI is today Dot um what what will get this value or you can do anything whatever you want I'll be just displaying the value that's it so you have to write so much of code including two client records right it can create a performance issue also okay you can get the entire thing done using this you can get rid of one Glide record if you use this query okay let's run and let's see if if we get the same result see if I turn this out I think I missed something on my previous just a second lag incident this attachment if you are interested in society okay query if it has something it was written true as it was written false I think this code is okay something messing over here let's check this attachment right Society let's see if something is wrong over here let's debug this out okay so it's a debugging experience now okay so first we have to say is like a flag right like so is it going into this or not okay so everything here no it didn't go anytime in this okay so something is fishy over here share incident Society okay say if I give this control X and if I put it over here right and I are not here also if I put it over here and I keep this value on each incidence mm-hmm so it's coming over here okay now something is visually assist attachment all right I think the table she said is the wrong field that's maybe the only thing yeah attachment table okay not this field I guess is character show form why I went to the schema my bad this is what I did foreign my bad my bad so it's 10 records okay now if you see okay over here and if I I will just comment this out with this High one okay now it's giving 10 records over here okay it's it's still record and if we do the same right so I will just comment this out okay and if I go over here and see okay so gr dot query and if I want to see the integral accounts I can do GS dot info get sorry GI incident dot get I just missed that brace out right over there okay and if I run so it also gives 10 records so it's the same so as you can see this length of the script has reduced to I think one third okay isn't it amazing okay let's look at one more example okay with the same okay so you got the query right okay so whenever you use add joint query okay the first attribute will be on which particular table you are referencing to then the second attribute is like the table on which you are acting okay which is flat record incident what which field you want to associate it with okay so it says ID over here and on the target table on this particular table which particular field this field matches to sits inside you okay let's go to one more example okay so the next example in our list is like I will just based out the statement okay so the next example which we'll be focusing will be around problem okay so we have to find problem that have incidents Associated where the incident caller ID now first thing find problems right so the Glide record will be on problem table first thing okay that have incident so the associated table the reference table or the related table will be incident okay where the incident caller ID so on incidentable caller ID that means third attribute okay will be caller ID for a field value will be equal to problem opened by so on the parent table the second attribute the missing attribute will be opened by okay pretty simple so let's find it out so how we can do the variable GR aph problem equal to new if you try thinking try is thinking this out okay on your own key how much time it will take if you are not using this out so you will get the answer okay how beneficial this apis or this functions to a problem right now the second thing what do we have to do is simply we will be adding query so gr problem dot add showing query and I'll explain you the first attribute is the related table which will be in our case incident because we want to look into the incident now we have to see if you go the incident is incident which table from which field from problem it will be the second attribute so it's open by so I will just copy it out and and paste it open back and the third thing is like you what that particular field corresponds to on incident table so it will be caller ID pretty easy to understand right no no magic story and done one thing you have to keep in mind okay this is not a true database join okay rather add joint query adds a sub query okay so while the result set is limited based on the join the only field that you have access to are those on the base table okay so you don't have uh access on incident Fields okay so you just keep this in mind and then simply GI problem dot query and ideally it should give all the problems which has Associated incident which has opened by equal to caller ID on the problem and then if I do she is I'm sorry see a problem Dot why what I'm doing while you have problem dot next simply print the SE Dot 4 GM problem dot get this private value right and and it should give the relation okay now keep one thing let's see so it give all the problem okay which has incident and I already explained it out keep one thing in mind in this Glide record as I mentioned you don't have access to incident record so if you want to do validation based on some related table okay records then you can use this add joint query but you will not have access to those information of those tables okay so this is all for today's tutorial hope you find it helpful okay uh sorry for the small mess which I did in the middle okay but I think the tutorial is particularly I find this function is pretty helpful okay if you have any queries any download please drop me a main on technomark123 gmail.com or you can reach out to me on LinkedIn or you can drop your queries on this video itself okay I will try to resolve it till then stay safe stay happy and have a nice
https://www.youtube.com/watch?v=Pr-uAuBDn1Q