How to send dynamic data with REST Message in ServiceNow | 4MV4D | S01L12
[Music] in order to subscribe to my channel please click here or click here please share comment and like my videos and channel hello guys welcome back to sas with service now this is part of servicenow four minutes video for developers this is the next lesson of series of rest integration in service now in this lesson we will learn about sending dynamic data with outbound rest methods in servicenow we will learn how to create post method for outbound rest methods and we will also learn sending dynamic data with outbound rest message so let's go to my personal developer instance so if you have seen my previous videos we just showed a demo with two instances so basically this is the secondary instance and we have another instance which is our primary instance so here is our primary instance that's basically the source instance and this is the secondary instance so whatever rest message we created in our in our previous videos we created in this primary instance so that it can fetch the data but as i mentioned this in this particular session i will be creating a record in another instance and for that we have to create a post method so in our previous sessions we talked about geth method but now i will create the racket in another instance and for that we will create post method so what i will do i will quickly go to rest method which is under system web services and i will go to the same rest message which we created earlier and as you can see here we already had this uh default get which we created last time which was automatically created basically we didn't create uh manually it was auto automatically created whenever you create a new rest message so what i will do now i will create a new method which is which will be for post and here i will just give it a name so maybe i will do create incident and i will select the post so maybe i will do create incident i will select post now here i have to provide the details what details these details should be from another instant that means your third party system which is servicenow instance as of now so i will go to that instance over here now as i mentioned earlier rest apa explorer is the best feature of servicenow which you can basically provide to your consumers or whomsoever wants to integrate with your instance because it gives you the ready things so i will go to a rest api explorer and we will create this incident in incident table so we will go to rest api explorer and i will select here post so you will see that get is already selected so i will click on post which already says create a racket with post and i will select the table so we will select the table quickly so this is the table incident okay that's what we are going to create the racket i'm not going to give any parameter over here uh request headers now here i have to generate the body now in order to create the incident you definitely have to send something because you cannot do it without any request body you want to populate some data so in that case we will populate maybe short description and i will just mention this is a test for youtube let's do that so i have short description at the same time we will also make caller so because caller is mandatory so i will just select caller and i can put callers email address as well so what i will do i will just go to maybe sis underscore user dot list because i want to get that email address so i will so we already have this email address selected for uh fred ludi we have this so i will come over here and i will put email address and we will see whether we are able to create incident or not so i will quickly click on send if i will do that it would ask me that you're going to modify the data that's okay so it says 201 it is created and incident is over here and if i go to list of incidents so let's say if i go to incident dot i do list it will open it in a new tab so here is this is the test for youtube now this one you can see color is also populated now this one was created from this own instance this this in particular instance only but we have to create it from the another instant that's a source instance which is this one so i will go back to my destination instance now here we have this response body now how exactly you can create it from another instance for that we will basically copy some details from here and we will put it in our rest method outbound rest method so i will copy this end point first i will copy go over here put the end point this is for post this time i will select the authentication people we can check the auto uh to get the authentication directly from our main parent rest method so that's fine you don't have to uh change anything here i will select the headers so if i go over here for header it says accept so i definitely i will go for accept because accept is something to accept the format so i will basically mention accept and i will select this application slash json because i want to accept the data in json now this content type now if i talk about get method idle you would you won't need uh you won't need content type because you're not sending any kind of content but in in basically post you have definitely have to send some data to your destination instance in that case you will select the content type as well that what in what format you are sending to to your source destination instance so that format is this time is json and here is the body that's json format so if i just copy this one and what i will do i will just put it over here i'm done you will see that same kind of incident but let's do one thing i will make it uh do it's a this is a second test just to identify and recognize this is the one which we are going to create so let's do that and i'm done i will just click on save this will create a post method for this particular rest method and if i created this so you can see we have this content now i just need to click on test to perform this testing whether it is able to create something in another instance or not if i click on test you can see the incident was created because we got this 201 here again and here is the content here is the end point and we got the response and the incident number is this one 20 and if i go over here let's check that whether that incident was created or not if i refresh the list absolutely you can see here it created this is a second test for youtube so this is how you create a post method in servicenow for rest methods which can create records in different third-party instances in third-party application now this is just a demo because in order to show you this demo i just used a second servicenow instance but as i mentioned earlier i will be creating an integration for youtube as well so just just watch my further videos of this series so you will find an integration for youtube as well and that is also direct integration and with record less as well but as of now i'm just showing you for our servicenow instances so i'm just integrating two servicenow instances so now so as of now we just created uh the racket directly now let's let's uh come to the main topic for which we have created this this video and that that topic is sending dynamic data because as of now if i talk about from this instance i'm just sending the static data which i have just mentioned in this in this i would say body uh now how exactly i can send the dynamic data now in order to create that ex script you don't need basically directly uh from from here there's another way best way i would say go over here go to your rest api explorer come over here and here is servicenow script just click on it you can see it will automatically give you the ready script on your table which you can use in your script in your source instance so i will just copy this okay it's gone i will click again i will select snippet i will copy this i will go to my source instance that's the primary instance i have and what i will do now i'm not going to use because i'm not going to use direct rest message i will be using the record list so what i will do i will go to incidents and we will create a business rule so i will come over here and if i go to business rules because now we are talking about sending dynamic data so i will click on new in order to create a new business rule i will give it a name maybe create incident in in the meantime because i don't want to lose my script so i will just paste it over here i will come over here what i will do i will just copy this one and i will just mention like this now here we have to put the condition that when exactly you want to run it so we will make it async because we want to run it in the back background we don't want to we don't want the response maybe right away so i will do it async and maybe i'll insert but at the same time i will put the condition as well let's say i will put short description starts with that another instance that's our destination instance so that's it i will go to advanced now here you can see that i have this uh basically script which will basically create this uh api call basically that's the event so whenever i will create a new incident it will trigger this api uh basically the script and trigger the api api calls with this rest message and send to that destination instance and create the racket so here you can see main main i would say main line of code so maybe let me show you with full screen so here's the request where we create the rest message api we declare it so we create the object for that then i'm just setting the end point which is the same which we copied it from there and we have this method so we method this time is not get it is post here we have username and the password and here also it says set basic authentication that's what we want to use and what i can do uh maybe i will just provide the password and the password would be uh i will not show you but i will just mention the password so i will do like this so i have provided the password and anything else i will come over here content type everything is there headers are there now here you can see here we have the request body i will just put it in the next line so that you understand it here you can see we are setting the request body which we were sending directly from the rest api explorer and that's how you can send the dynamic data but before changing this dynamic data what i will do i will quickly show you that how exactly i can send it from here from this script rather than the test from the rest method so what i will do this is a uh this is a third test and i will keep the user as it is now why why i'm just sending over here you can see we have this fred ludi so i'm just putting the static value for now and here we have response.this i can do one thing what i can do i can basically update i can update the response in the work nodes maybe so rather than gs.log i can just comment this i will come a little bit bottom maybe and what i can do i can let's say current dot work notes equal to and i can do response dot i can just do get body that's it so i can update it and i can do current dot update and you are done so we will just save the script now so i will click on save so our business rule is created now what i will do i will create a test so we will create a test incident so as you know we have mentioned the condition as the short description to start with this so i will just copy this one and i will go to incident let's say this one so we will just create a new incident and this time i'm just selecting the caller because as you know we are sending the static values we have not sent the dynamic yet and i will show you how we will convert this to dynamic quickly so i will start with dev that's it maybe i will put test oh that's it and if i click on save you will see that that we got the response that means incident was created and this is the incident number we got and if i go to my another instance and i go to incidents i refresh the list you can see it says this is a third test for youtube and i also got this caller over here so but as of now i'm still sending the static values so if i come over here and i go to my business rule let's say i will go to business rules because now i want to send the dynamic data rather than static data so i will go to our business rules i will go to advanced here is our script now if you will see the body as of now i'm just sending the static data this it is not dynamic if i need to send whatever short description i mention over here and whatever user's email is there if i send that how about that that means it's dynamic so whenever i create any incident in this particular instance that is primary instance and whatever data i select in my own instance then it should create same incident same information in another instance as well that's that's how you will call it as dynamic so in that case what i will do i will just remove it from here so maybe i will make it plus so i will make it plus but at the same time i have to because it's escape basically so that's why i'm using this this particular feature so maybe i will do [Music] like this and then i will put over here current i can just do like this and then i can put plus again and here i can do current dot shot description that means this way this will send this dynamic short description whatever data i will put in my new incident it will always send that to the another instance so i will send current dot short description then i will come over here now here we are sending the email i can again make it dynamic we will do plus and we will also make it this double quotations and i will do current dot color underscore id dot email if i do that and i will do plus and i will just remove this one and that's it so i can just remove this one so we we have these basically escape characters as well so like you can see we have added this slash because in as part of the json data we have to send this uh double quotations to to create the json data that's a format of json that's the reason we are using these so you have to make sure that you're not you're not making any mistake because syntax is very important over here so now i have provided uh the caller id email and i have provided the current short description so whatever data i will select it will send accordingly and i am getting this current dot caller underscore id dot email and i think that's it i don't have to do anything else so i will click on save now this will save our business rule and now i will create a new incident so that we can check whether we are able to send the dynamic data or not so i will go to new incident form now i will go to over here and we will create a new incident so we will select this abraham lincoln let's say and i'm sure this abraham lincoln is available in another instance as well because same personal developer instance now here i will just mention this name because that's what the condition we have and after that i will do test for dynamic data dynamic data from and we will just copy the name of our this instance that is primary instance and i will just click on save if i click on save this will create the record but let's see if it it will create the racket in another instance or not and if i come over here you can see i did not get any any response if i reload it okay i got it so it took a little bit more time but it it basically received the response and you can see it created this 2-5 incident and if i go over here i refresh the list absolutely you can see it says test for dynamic data from dev this this is and color is also populated the reason behind it because it's abraham lincoln is already available in this service now instance because we are already uh pulling up the data of of our servicenow instance and both our same instance like both our service now personal developer instance so in that case we have same data and we are able to send dynamic data to this destination instance this is how you can send dynamic data as of now we were sending short description and callers email it's up to you whatever data you want to send to populate in your destination instance this is in our servicenow instance but if you want to send it to any third-party application which is not a servicenow instance and you have to send the dynamic data you can also do that with this way so this is how you create post method and send dynamic data to other to third party applications
https://www.youtube.com/watch?v=rF6F-drJHVU