Incident to incident integration using Oauth

anshmahajan
Tera Contributor

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

 

 

object {1}
result [67]
0 {92}
parent:
made_sla : true
caused_by:
watch_list:
upon_reject : cancel
sys_updated_on : 2016-12-14 02:46:44
child_incidents : 0
hold_reason:
origin_table:
task_effective_number : INC0000060
approval_history:
number : INC0000060
resolved_by {2}
value : 5137153cc611227c000bbd1bd8cd2007
sys_updated_by : employee
opened_by {2}
value : 681ccaf9c0a8016400b98a06818d57c7
user_input:
sys_created_on : 2016-12-12 15:19:57
sys_domain {2}
value : global
state : 7
route_reason:
sys_created_by : employee
knowledge : false
order:
calendar_stc : 102197
closed_at : 2016-12-14 02:46:44
cmdb_ci {2}
value : 109562a3c611227500a7b7ff98cc0dc7
delivery_plan:
contract:
impact : 2
active : false
work_notes_list:
business_service {2}
value : 27d32778c0a8000b00db970eeaa60f16
business_impact:
priority : 3
sys_domain_path : /
rfc:
time_worked:
expected_start:
opened_at : 2016-12-12 15:19:57
business_duration : 1970-01-01 08:00:00
group_list:
work_end:
caller_id {2}
value : 681ccaf9c0a8016400b98a06818d57c7
reopened_time:
resolved_at : 2016-12-13 21:43:14
approval_set:
subcategory : email
work_notes:
universal_request:
short_description : Unable to connect to email
close_code : Solved (Permanently)
correlation_display:
delivery_task:
work_start:
assignment_group {2}
value : 287ebd7da9fe198100f92cc8d1d2154e
additional_assignee_list:
business_stc : 28800
cause:
description : I am unable to connect to the email server. It appears to be down.
origin_id:
calendar_duration : 1970-01-02 04:23:17
close_notes : This incident is resolved.
notify : 1
service_offering:
sys_class_name : incident
closed_by {2}
value : 681ccaf9c0a8016400b98a06818d57c7
follow_up:
parent_incident:
sys_id : 1c741bd70b2322007518478d83673af3
contact_type : self-service
reopened_by:
incident_state : 7
urgency : 2
problem_id:
company {2}
value : 31bea3d53790200044e0bfc8bcbe5dec
reassignment_count : 2
activity_due : 2016-12-13 01:26:36
assigned_to {2}
value : 5137153cc611227c000bbd1bd8cd2007
severity : 3
comments:
approval : not requested
sla_due:
comments_and_work_notes:
due_date:
sys_mod_count : 15
reopen_count : 0
sys_tags:
escalation : 0
upon_approval : proceed
correlation_id:
location:
category : inquiry

 

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

4 REPLIES 4

Maik Skoddow
Tera Patron
Tera Patron

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

anshmahajan
Tera Contributor

Hi @Maik Skoddow ,

 

yes in return i am getting plain text like this :-

 

anshmahajan_0-1703760581504.png

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

Hi @anshmahajan 

 

Can you share the screenshot of the REST Message, you configured?

 

Regards,

Amit

Faiz Ahmed Moha
Giga Expert

Hi @anshmahajan 

 

Please configure your Rest Message with following HTTP Headers as shown in below screenshot.

FaizAhmedMoha_0-1704386505694.png

Please Mark "Helpful' and 'Accept Solution' if this works for you.

 

Regards

Faiz Ahmed.