Incident to incident integration using Oauth
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-28-2023 02:29 AM - edited 12-28-2023 02:38 AM
Hi All,
I am trying to integrate two ServiceNow instances with each other. The functionality i am trying to build is to pull incident table records from 1 instance to other instance. For this i have a setup of rest message using oauth authentication and i am able to get the json object in return.
Below is the JSON object i am getting in return
As you can see above is the JSON structure for 1 incident record i am getting in return.
try {
var r = new sn_ws.RESTMessageV2('test incident rest call', 'Default GET');
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
var parser = new JSON();
var str = parser.decode(responseBody);
var str2 = JSON.parse(responseBody)
for(var i=0;i<100;i++){
var keyy = Object.keys(str2.result[i]);
var strlen2 =keyy.length;
for(var j=0;j<strlen2;j++){
var keystr = keyy[j].toString();
gs.log('testing rest API line no 29 testing rest '+ +' '+ i+ ' strlen2 ' + keyy[j] +' ' + str2.result[keyy[j]].value +' ');
}
}
catch(ex) {
var message = ex.message;
}
Above is the script which i am trying to use to print the field data.
when i execute the above code the values are returned as "undefined".
but if i mention the log as
gs.log('testing rest API line no 29 testing rest '+ +' '+ i+ ' strlen2 ' + keyy[j] +' ' + str2.result[i].sys_id +' ');
In return i am getting the sys_id of that record.
The result i am trying to achieve is without mentioning the static name of the field like sys_id or number i should be able to dynamically pick it from the array of key's which we can retrieve from the JSON object.
Regards
Ansh Mahajan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-28-2023 02:44 AM - edited 12-28-2023 02:44 AM
Hi @anshmahajan
you are writing: "Below is the JSON object i am getting in return". However, the output is not JSON, it is just plain text.
So, did you configure your REST message with a "Accept" header of "application/json".
Apart from that: It's a bit confusing what you are writing. Please explain better your issue.
Maik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-28-2023 03:13 AM
Hi @Maik Skoddow ,
yes in return i am getting plain text like this :-
i just made it more readable and added in the above question.
and let me eleborate my query.
I am trying to import all incident records from instance "A" (we can consider as data provider) to instance "B" (we can consider as recipient) via REST integration. for that the approach i am using is i'll configure a REST integration between two instances. In that i am creating oAuth authentication between 2 instances. Instance "B" will "GET" the data from Instance "A". Then we can validate the data inside our data source and push it into import set table and the final into incident table.
What i am trying to achieve here is when we will iterate from the 'Plain Text' there are specific values corresponding to every field mentioned like sys_id , number , category , sub_category etc.
Now the thing i i don't want to mention the field names as str2.result[i].sys_id while printing the data.
i want to mention it as tr2.result[i].keyy[j].
"keyy[j]" is the array of field names which i am pulling from the Plain text file which I'll get in return after hitting the instance.
While configuring the Rest message there was no option available to configure "message with a "Accept" header of "application/json"
I hope the Scenario mentioned above explains everything .If I am missing something then please let me know.
Regards
Ansh Mahajan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-18-2024 08:31 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-04-2024 08:42 AM
Hi @anshmahajan
Please configure your Rest Message with following HTTP Headers as shown in below screenshot.
Please Mark "Helpful' and 'Accept Solution' if this works for you.
Regards
Faiz Ahmed.