logo

NJP

Integration with ServiceNow using Scripted REST API | multiple use-cases | Chapter 2

Import · Aug 05, 2021 · video

hi everyone this is the second video in the series of integrations in my previous video i have shown you how we can send data from third party application to service now instance you know post request basic auth and the table api of service now in this video i'm going to demonstrate the same using scripted sd plies so let me just navigate to the script produced api and i'm going to create a new one over here the name of my list api is scripted and i'm going to save this record and let's add a new resource over here and this is the area where my script needs to fit in so let's get started first of all i am going to get the request body from the first object now request data object over here contains the payload data which is coming from the third party application in my case that application would be postman and if the payload contains data for multiple records then it would be an instance of an array and if it contains only data for a single record then it will be simply an object so if my request data is an instance of any then i'm going to process those records over here if not then here so whatever the data is coming let me create a function in a script include and i am going to save the data in my table using input set i don't want to flood this script area part using a big script let's say i am using a script include so the name of my script include is this which i generally use for any of my demos insert incident data is the name of my function and then i'm going to pass the request data object over here so my scripted list api part almost complete i just need to code this function so that i am able to insert the data into my incident record so if you have not already watched my previous video where i have demonstrated the inbound integration using table api i created a staging table and i am going to import the import the records in the staging table instead of directly importing it into my incident table and that's the recommended practice as well so here is my requested object now the name of my staging table is degradation i am going to initialize this let's say i am receiving short description and description so let me check the exact name in the back end so this is my short description field and this is the request data let's assume the name of my field is short underscore description in the payload now again i'm going to go back for description this is the name of my description field in the payload i mean all these are assumptions until unless i jump to postman and set the actual field values over there along with the field names so let's say next one is impact and then urgency and then i am going to call dot insert function so now the data which is incoming will be imported in my staging table and using a transform map it will be saved in my incident table as well so let us take a look at the transform map as well then meanwhile this is getting saved this is the same transform map which we created for the table api demo so i have the field map for description short description impact agency i think that is what is required for now now i am going to jump back to my scripted list api and i'm going to copy this file and paste it to my postman and i'm going to update the one in my postman double slash i'm using the authorization as basic auth using the same user which we created in the last video okay and this is the sample payload which i have already demonstrated using but now let's update it impact is one agency is two now let's see how things turn out i am going to click on send status here is 200 okay now let's go back to the service now instance and i am going to check in the incident table if a new incident is created so this is the same short description and description which i have given in my postman tool which in your case you need to assume it in a way that the request is being sent by a third-party application so this is how we can create a ticket by a third-party tool and using scripted rest apis in service now now let's add a catch to it so i'm going to consider a couple of other scenarios as part of the same demo so that we have a clear picture of how things are working now this was the case when only data for a single record was sent now consider a scenario where data for multiple records has been sent then how we are going to process the same so if things are not already clear it will be clear in a second and this is the data for the first incident this is for the second one so when i send this data it should be creating two incidents in servicenow instance which will not happen now because when i click on send the script to test api will be called it will enter into this if condition because it is an instance of an array but it will not work because i don't have anything over here i simply did not write any code in this particular if condition there is no hard and fast tool as such to insert multiple incidents let me simply go to the postman and try clicking on signed just to make sure things are not working as expected i fish the list this incident is still the first one in the list and let us just simply call it out here so i am going to request in request data i'm going to call this particular function again and again that's it let's see if this works out we will have to maybe take some other route to postman click on send it says 200 okay again back to incident list something is created but without a description and short description created by the same user let's try to find out reasons for the same before checking anything in the logs or debugging everything let me try simply declaring this request so request is the name of the out of the box object so we cannot use the same name so i'm going to declare it as request object and let's save this i will try it one more time send data is sent now i am go back going back to the incident list all right looks like we missed something take the request in here so now it is updated i will send again mining mistakes now we are again missing one more thing i mean these are the steps we need to follow when we are writing a particular script and we need to be careful about it otherwise if more it would take more time so now let's try to run through the script while it is not working when i say variable x in an array variable with variables x y z this signifies the position of the element in the array so in this case i am simply passing the position not the object at that position so to pass the object at that particular position i need to pass this object along with the parameters right here okay so consider it as let's say there is a new car and this gives me the first position and this gives me the second position the element at the second position so what earlier we were doing was i was simply passing this particular element position value either zero or one so it's obvious that it won't work however it was creating an empty incident without any details because our script was getting called anyway now let's save this and see if my understanding of this particular script was right or not so i'm going to go back to this application i will click on send again let's reload the list see both the incidents are created now first one with the criticality or the priority of one and the second one the priority of moderate and this contains one this contains two in the short description and description so far i have taken into account two use cases first one is when i was sending the data to my servicenow instance for only one single record and the second use case was when i was taking into account that the data for multiple incident records is being sent to my service now instance now let's just add another use case to it where i don't want any duplicate incidents to be created now if i'm going to click on send button again it will again couple of it will again insert couple of incidents into the system let's see so this is creating a duplicate tickets in the system though the incident number is different so how it how can we make sure that this does not happen for that i am going to include the coalition id field so i'm going to navigate back to my web service and now i have added this correlation id to my web service okay let's click on update continue so the coalition id is successfully added to my web service or to my import set table now let's go back to the transform map which was created and map this coordination id field [Music] my first field is correlation id and my target field on the incident table as well is correlation id and i'm going to mark this field as police let's save the this mapping for the correlation id field and i am also going to add another field to the payload request which is being sent from postman let's press correlation id so let's say the coalition id for the first incident data is inc you one two three and in the same way okay let's make it so this is one zero zero zero two okay as of now there will be no effect because i'm not saying this field value so now let us navigate back to our scripted list api and see where this is happening so this is happening in this function which is there in my script include and here is my script include so let's save correlation id as well let's save it okay so this code is saved now and let's go back to our transform map so in my transform map i can see in the list that coalition id is the coalition field and everything else is not if you're not already aware what colors field is all about then it marks the uniqueness of the data so if the same incident number is being sent by the third party application multiple times it is only going to update the existing record it will not create a new record this is how it works so i've marked the collis added in the function there is no need to update my scripted list api for now and let me take the request just to identify i am changing it to three and four in the short description click on send navigate back to the incident list refresh it so these are the incidents which are created now and let's see if coalition id is present yes it is now let us trigger the same request again without changing the correlation ids if i click on send let's refresh the list you can observe that the tickets are not created now because i am using the coalition id functionality usually in uh practical cases when you are working on any integration let's say there is an integration between pega and service now so when there is a ticket created in pega something should be created in service now as well so pega request should include coalition id which means the ticket number or any unique identifier at tiger end so that same ticket is not created multiple times even when an update happens on the ticket at the back end now if i change the correlation id here see and this one is four if i click on send it should be able to create two more incidents in that particular list let's refresh it so i can see three four three four twice and if i go to position id one two three four so this time two incidents are created which was not the case earlier because incident number for the coalition id was same so this was about the fact that a new incident will not be created but if i use the same coordination ids will it update the existing incident so i am changing the short description to this just to verify whether an update works send and the ticket number for 3 and four is three eight nine and three nine zero let me refresh the list and you can observe the short description since this ticket number remains the same but but the short description changes the whole short description is not taken into account probably due to the limitations that it cannot be more than a particular value of about let's say 40 characters so it looks fine in the shot description field probably then yep since my staging table is having a limit of 40 characters that is why it did not include anything over that but this is how it works this is how we can use scripted list api to create incident records or records in any other table not necessarily an incident record but this is how stuff for the step players if you want to take a look at the user setup you might wanna watch my previous video because user setup is most important in such cases we don't give admin roles to the functional accounts which are created specifically for integrations so that's pretty much it and don't forget to mark your user as to web service access only and unless your user is supposed to do a lot of other things in the system but that also invite licensing implications if it is only web service access only and the user is just loading some data in the system there as for my understanding are no licensing implications so just make sure you're following the best practices and using the right rules for your user that's it and thank you for watching don't forget to subscribe

View original source

https://www.youtube.com/watch?v=7POS69Rcs6g