Send attachment over an Integration | Rest API | ServiceNow | Chapter 7
hi everyone this is the seventh video in the series of integrations and in this video i'm going to show you how we can send an attachment over an integration from one application to the another in my case both the applications will be service constances i will send from instance a to instance b so if you remember in the chapter 4 we discussed about how we can create an incident in the instance b while an incident is created in instance a by sending the details over the api calls so i'm going to do certain modifications to the same script which i have written over there and let's see how things turn out so let's get started so this is the same business hole which was created at that time and i'm going to navigate to the advanced section and i'm going to edit the script so there are a couple of things which i want to tell you before i start writing the script i'm going to write a piece of code over here which will encode the file and send it over to the the api call and send it to the other instance so for now i am going to take into account only one attachment but if multiple attachments are also required then you can run a loop and call the same message over and over again so let's get started first and let me declare a couple of variables one sixth variable is let's say encoded file uh this will store the encoded version of the attachment the second variable which i might need is let's say file name and if anything else is required we can declare it later so first thing i need to do is i need to glide to the attachment table so the name of the attachment table is this underscore attachment and then to look for the record in the attachment table for the current incident record the way we can query is by checking for the table name and checking for the table says id so the field name is table underscore name and you can get the table name from either here get table name or you can hard code it as well but this is the recommended way since it is available to us and we don't have to hard code it then the second would be table says id so table says id is this is id of the current record which i can simply get from the current object if uh we are expecting only one attachment which i am assuming as part of this demo then i'm going to lift condition otherwise i will have to use while condition and my this list message call will go inside this while loop over here now here i am going to declare it like this attachment and let's just attachment input stream and it accepts the parameter of the ssid so let's go id this thing and then use and then i will declare another variable for the byte area output stream so here i can use the package call and packages dot server this is the java file in the back end which we do not have access to and we will have to sadly manually write it because it doesn't populate using control space now i will write these bytes into my glidesys attachment input stream so let's see how we can do that yes a dot write 2 is the name of the function uh in earlier versions it used to accept only one parameter but today we will have to pass to mole and then i can simply close the byte a output and then i can simply fetch the encoded file from there and store it in my variable and to fetch it i will have to use light string util and [Music] then i am going to call the encoding function so that i encode this into a base64 format and i declared another variable on the top named file name so let's make use of it and get the file name from the attachment table and another thing i can make use of is the content type of the attachment so let's fact that and let's declare it over the top now i have the file in the encoded format which needs to be sent over the api call i have the name of the file and i have the content type available with me now the only change that i need to do to my existing list message is to add these parameters named encoded file file name and the content type so so i'll give it a name let's say and call it string is the name and i can simply pass the encoded file here and my next parameter is filename and my last this one the type of file so i think this scripting part is complete the only thing is that i need two different variables to store the value of the items i need to pass for the attachment and for this purpose i have already created three columns in my staging table if you are not following up on the concept of staging table i recommend watching the chapter four of this integration series where i have explained that in detail so now i'm going to navigate to my instance b which is this one and this is my staging table which i created in that particular demo so to save at some time i have already created three fields over here to this table attachment data which will contain the encoded version of the attachment then contains the name of the attachment and the attachment type is basically the content type okay now i'll go back to the instance a and i will have to make certain changes to my rest message so that i am passing these variables which i have mentioned over here so let's go to the list message and this is the rest message which was used in that particular demo and this is the place where i am passing the parameters so let's add a few more first one is the attachment data and its value is in encoded string second one is attachment type and its value is in [Music] i think it was file type third and the last one is attachment name and for that if i'm not i used a variable with the name file name so now whatever i needed to do on instance a is already done i have an attachment attached to my incident record i am encoded that attachment and i am sending that attachment data to the next instance over api calls and it will be stored in this particular in these particular fields so when i'm receiving the data i need to save that data after decoding so that it is visible on instance be in the same form as it was sent from instance a so let's go to the transform map that was created for this purpose and so this is the transform map that was created in the chapter 4 of the series so here what i'm going to do is i am going to create a transform script which will help me decode the encoded data into the attachment so let's create it and i'm going to select on after then my data is coming as part of the source object so first of all i'm going to check if the attachment data is empty then i don't need to do anything so for that i can simply write source dot name of the column yeah i think i can simply use gs.nil instead of comparing with the blank so if it is nil i don't need to do anything i can simply say let's ignore this if it is not null then i need to do certain action so [Music] i am going to glide through the incident table then i'm going to simply get the incident record which is coming in and which is getting created so i'm going to get this this id of that record okay and then here i need to write a certain logic so that i decode that file and write that file into this particular record so that i get the same attachment which was transferred from there so i can do that using the string utils so let's okay and let me play available for attachment as well and then definitely it's not well because we have already taken care of that condition i can simply call attachment which again accepts couple of parameters first parameter being the table name which is incident yeah spelling is correct second parameter being the society of the incident which is getting created i can also use target.id one and the same thing second is the attachment name so [Music] okay yeah this one and i am like missing something over here my last parameter is going to be empty and then i can simply call string which i have to clear on top and i call the decoder function which will decode this in the form of bytes and i can pass the attachment over here so here we can simply pass the [Music] because we are getting the data as part of the source yeah i think we are done here and once i receive the file in the target instance this code should be able to copy that file on my instance b's incident record so let's save this now let's navigate back to the instance a and create an incident let's add some basic details over here i want is the short description and i'm going to add an attachment over here these are my boxing gloves actually yes so if i'm going to save this record it should create a similar record in instance b along with this attachment okay now let's hop to instance b so there is one i got created that's good news and also there is the same attachment which we attached over there let's verify the contents as well whether it's just the name or the whole file itself is the exact file yeah it's the same file which we attached on instance a and is transferred to instance b as well along with the ticket so this is how we can transfer an attachment from one uh instance or one application to other application using the scripts which i have written as part of this demo i have only taken into account one such attachment but there can be multiple other attachments we can send it an attachment to the update as well but we'll be discussing more about those use cases in the upcoming videos as well so thank you for watching and don't forget to subscribe
https://www.youtube.com/watch?v=ptPlW4tQXEw