ARRAYUTIL SERVICENOW API | IMPLEMENTATION OF USE CASES OF ALL THE METHODS OF ARRAYUTIL
Hello friends welcome back to my YouTube channel basico service not learning so in today's video we are going to talk about one of the servicenow API that is array utils so this API contains ready-made functions which are very useful to perform array Logics while doing scripting so in this video through use case implementation we will cover each and every aspect of array utilities so without wasting your much time so let's see what is error YouTube API in service now so in servicenow API is basically a out of the box script include which contain useful functions which we can use while writing scriptings so those functions are concat unique Union intersect insure array contains an index of I am not saying that all of the error Logics we can perform through array YouTube's but yes some of the Logics some of the things we can perform through array utils so in this video we will discuss all those Concepts in detail through use cases so let's see what all use cases we are going to implement today so the use case scenarios are we have to merge two array Second Use case is that check if array contains the required value right sometimes we got a requirement where some value has been given where we have to check first that whether that value is available in the array or not and then we have a Boolean reply right true or false if value is available then perform this task if value is not available then perform that task right the third use case is find what all distinct value or different value given array contains in comparison to other available arrays the fourth use case is find out the index or position or required alignment in an array the fifth use case is find what all elements are common in all given arrays let's say we have three arrays so there can be a possibility that all those three arrays must be containing some common values right that we have to find out six combine all arrays but it should only contain unique value seventh find out find what all unique values a single array contains here we are talking about only single array right let's say we have an array which contains 50 values and there are lots of values which are duplicate in that way we have to find all the values which are unique in that particular array and the last use case which we are going to implement that is convert JavaScript object to JavaScript array okay so let's move to service now personal development instance so we can Implement these use case scenarios in our instance okay so friends this is my service now personal development instance so before implementing uh the use cases let me show you the script include which is there out of the box in service now instance here that's open in another browser so type array over here so here we have four script includes this is the one which we need that is array YouTube so see this is the script include which is already there in service now personal development resistance where we have the functions declared let's say index of initial array concat convert array so instead of writing this code what we have to do we only have to call this method and accordingly the implementation will be done okay so we have we don't have to write code over here right what we have to do we have to write logic where we required in server side so we can use this array util anywhere in this Riverside script so the today demonstration or today use cases we are going to implement in background script so for that background the tab see here okay so this is basically a return if you don't have this type of editor install browser extension that is SN uterus so that you will be getting a you will be giving you a solution while writing the scripts so here today we are going to explore array right so I have already created three arrays just to save time let me paste it over here so I have pasted all those three arrays over here that is first area second area and the third array I have given the name very simple so that at least we can understand that what array we are using and what is happening the colors are given over here green brown red orange yellow and some of the colors are common in all these three arrays that is green and orange see in second array we also have green and orange and the third array we also have green and orange okay so that while executing the use case we should know that what is common what is not common okay so let's see how we can declare the array uterus and how we can use all the methods which we have in error so prior to that I'll showcase you one more thing which you need that is how to find array length so to find error length just info what we have to do we have to type array name and there we have a property that is array length telling you this because most of the times when we are writing array Concepts then we have to find out the length of array 2 right so see for the first area the length is 5 that is one two three four five right just for a demonstration I have given so now not wasting your much time let's start with arrow uterus now so here the same way we Define for script include we have to define the object let me give the name the same as it is you can give any name you want new array utils okay in this way we can declare the array you till because it is also a script include right or we can also use the API name but here I am using the common way to call script include in anywhere in the server side script so this array util object will be used to call any of the method which are mentioned in this script right first use case is let me see that we have to merge two array right so how we can merge we have a function called concat so by using this array util dot on cut we can merge two arrays so let's say first array and then we can give the name of the second array okay let me start here so execute see both the arrays get concat merge green brown red orange see but the duplicate values are not removed all the values let's say five here and five here means 10 values are getting displayed over here see here right one two three four five six seven eight nine ten right so if we are concatenating two arrays it means what all values it contain will get concatenate right so we are done with the first use case it's very simple to implement that's what I have said it's a very simple API it basically reduces our time while writing array logics let's play now the Second Use case is check if array contains the required value let's say if I am talking about this first array only and I have to check that whether this array contains red value or not currently we have only given fair value so it is easy for us to just see and check right but let's say if contains two thousand value 2000 elements five thousand elements right then what to do then we have another method that is uh array utils dot contains yeah this is the method which we have to use what we have to pass we have to pass two parameters the array name for which we have to check the value okay and the string or the any value which we have to search let's say red is just string value that's what I am giving into a string if it is a numerical value you can directly give it a numerical value over here it will return true or false to us if it contains the value it will return true if it will not contain the value it will return false okay so let me see let me write the thing over here okay let me print it now I'm writing this because I will be pasting this script into my website so it will be like available for you from there you can copy and you can use right see here true because it contains red right that's why it is returning to if I am giving any random value let's say this okay it will turn you know right let's see if I am giving your love see here it is returning true so in that way we can search the required value is available in the array or not right the Second Use case is done now let's navigate to the third use case so the third use case say that we have to find out all distinct or we can say different value in a given array contains in comparison to other available arrays so let's say we have three arrays over here the first array second array and the third array and we want that what all distinct values different values first array contains which are not there in the second array and in the third array we can do that let's see the different values required array that you can see like yes dot info printing that now error util that is an object dot if we only have to use and we have to give the array name firstly we have to give the right name for which we are checking that is first array and another time we have to give the other arrays right this secondary there's one so it is giving me a two values that is brown red yellow means it says that the value brown red yellow is only available in the first array these values are not available in the second array in that way if there is a requirement where two arrays are containing lots of values and we have to see that what all values are different uh there in the first array in that way we can do even though we can also pass the third array to see in all areas right let's say we have number of Errors we can type all the arrays but the logic is that this array will be showing the different values only see here okay in this way the third logic is also done so let me comment this also let me comment this also let's see what is the fourth use case so the fourth use case is find out the index or position of a required element in an array so let's say we have a requirement where uh we have thousands of values right and there is a value let's say some incident number let's say 9048 and we want our location of the index that in the array where it basically located what is the exactly the index number is right so in that way how we can find out right how we can find out same way add a utils object dot index of index of we have to give the array name in which array we are looking for to find finding that value let's say first array basically we are finding out the position of Orange let's say okay let me give the position of array and here orange and run script okay let me print it yes dot info okay sorry let me print it is in third see zero one two three right so the first position is of an element is zero you all know right 0 1 2 3 so orange is in third place if I place yellow see here then the position will get changed it is showing me 4 or if let's say we have a 2 orange then what will be the output let's see here Orange let's see run script oh let me change over here also so it is displaying me third means whatever the first value it found it will be displaying that value so we also have a orange on the one two three four fifth position but it is only displaying as a third position so in case if you have multiple uh values available then it will display the first value it found okay so in this way we can found out the element position okay here we also have one more thing which we can pass the third parameter let's say if I pass the parameter let's say 4 okay so let's see how it will be displaying over here displaying me 5 why it is displaying because this 4 basically is saying that we have to start searching from the fourth element means this one zero one two three four right so from here the particular cursor will begin to find out the element let's see if I am giving two okay so it will give me three see here why because from the second element it starts searching so 0 1 2 from the red it starts searching so the first position is orange so it will give us the position of this element I think now you have an idea right what this particular parameter do right okay so the so the fourth use case is also done so now we have to implement the fifth use case that find what all elements are common in all given arrays right so let's say common it means we have to intersect means let's green and orange are two values which are common in all this right so we have to only find out the common not all so for that let me comment this okay same way object dot then we have to use in terms of common intersect intersect and we have to pass all the arrays where we need that intersection in this way then we have to give the secondary right and then we can give the third array we can give more than two arrays here right let me execute so see as I told earlier that all these three arrays have a common thing that is green and orange okay so it is intersection is basically displaying as the common elements in the available arrays okay we are done with that use case also now let's begin to another use case that we have to combine all arrays but it should only contain unique values means now what we have to do we have to combine all these arrays means we have to display the result of all these arrays in a single go or we can like uh put that result in a particular different array and we can print that Arrow also right in that way so but the requirement says that it should contains the unique values not the duplicate one what happens earlier when we merge the two arrays earlier it will display as uh the all the values right not unique but now we need a unique values only so for that what we have to do here we have done intersect now we have to do Union I believe intersection and Union we already have like studied in our classes I think seventh or years right so same object so why I am using this object again and again because see here we already have written the code if you are give if you have given some another object and you have to use that object only right Union what I'm going to do that I am copying all these array here because I have three arrays only I am using that now I'm saying GS dot info info okay see here we have output brown red yellow purple black blue white orange green see orange and green were common everywhere right but here it is only displayed once and pink so these are basically unique colors okay or how we can do this also that we can create a new variable where we can print this fourth array so that depends on you how you are want to display and what exactly logic says what exactly the requirements say okay because I am just directly printing it I am not storing it anywhere in the other array because I am giving a demonstration but you can also use uh if you have to use that array or values anywhere right you have to store that in another I can say uh array or another variable in that way you can also display I think we are done with the sixth use case now let's implement the Salmon's use case so it is saying that what all unique value is a single array contains we are talking about unique values for a single array this okay same then we have to use unique okay unique and then what we have to do we have to give the array name that array name is let's say first so we have two orange over here right so one orange needs to be removed name over here then I have to give GS info just to print GS in so see it is displaying me only five elements here we have 6 so it removed one of the orange and only displaying the first orange right so in that way we can find out the unique elements of an array also right these are basically a basic syntaxes which we have to remember by lighting the code sometimes interviewer asks right otherwise you can search over the Google and you can implement it right so it's not difficult to do the the demonstration which I am giving basically providing you a more clarity that how you can use it how simple it is basically some of the times when we have not used right we always think it's very difficult to use the apis right where we have to use how we have to use but believe me whatever apis we have in service now all the apis are meant to be simple for us right it's our meant or created so that we can use it in a simple way and easily to make the things easier for service not developers right so here we are done with the seventh also now we are going to the eighth one where we are converting the JavaScript object to a JavaScript array so this is the use case but uh practically if I say I have I never did this earlier but for demonstration perspective I am giving you a bit idea of it otherwise uh when I do code I never found any scenario in that way that I will be using this particular function so let's see how we can use so let me mention what JavaScript JavaScript array so I believe you all are aware of JavaScript object right so I have one of the JavaScript object I have created earlier let me paste it over here so friends here I am pasting the JavaScript objects I believe you all are aware of this right and how we can access these values let me demonstrate you also give me a second that how we can access ts.info here if I have to show this particular name then how I can use demo object Just for information I am sharing if you know that then that is great see here it is see here or if I have to display this or I have to use this value belly tables in that way we can access the object oh sorry we can access the objects manuscript see here it is showing me being so vegetables basically is beans so when we do coding in service now right most of the scenarios are basically coming from arrays and objects right so these are the very important things we should know how to manipulate the data how to write the code where to use in the script right it's look like very simple but writing the code remembering all these things is very much required right where what to implement how to implement so this is not required just for demonstration I have set so the use case says that we have to convert the JavaScript object to the JavaScript array and already we have an object right demo object convert into the array so what is the function or method we are going to execute let's see okay so same way it is Dot this is the particular method which we have to use here we have to give the name see here we can get the name in that way and yes dot info way we can convert so it is displaying as object and object it means we have to stringify it to display the data how we can stringify we can stingify in that way Json and we have two write this here or you can store it any option variable and then you can use but I am using it directly see here all right here right let's see see here now this is a particular object right see here now we also have this brackets over here of an array it means whatever object we have we have converted the same into an array in that way but I don't think like where it will be useful if you have any idea if you have any use case where you have implemented or you have seen somewhere then please provide that in the comment box so at least I can from mind also okay so there is one more thing I want to tell you that how you can see the particular variable is an array or not so there is a syntax called array this is just for your understanding array is error if you have to check that whether this is an array or not you can check in that way you have to give this particular syntax it will return the Boolean value okay true and false okay so let me comment this because we already done with this see it is saying false because we have not converted this demo object here to an array and we are asking that is array or not but it is object right see here so it is saying false but if we say here just a second so what I have done here is that I have only given demonstration just to find out whether the particular variable is an array or not if we can check through this particular syntax right so friends we are done with all the use cases given over here I think all the methods which are there in the array utils we have covered in this video so friends this is all for this video and if you are looking for more servicenow topics you can navigate to our website that is www.basicoservational learning Dot N okay so if you feel that this video is helpful for you then please like share and subscribe and please do not forget to provide your suggestions feedback and questions in comment section below thank you friends have a nice day bye bye
https://www.youtube.com/watch?v=5zXfBU1DzUU