- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-10-2023 11:20 PM
My requirement is I need to get all the active incidents from Instance A and Store those values in the custom table of instances B.
*Custom table has 4 fields (u_number, u_inc_short_descriptio, u_inc_description, u_inc_channel) I have to set these field values to the response from instance A get method.
*Please suggest to me how I can achieve this using scripted rest api.
with ServiceNow instance A and ServiceNow instance B
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-11-2023 12:29 AM
Why not? There we go.
sm.setEndpoint('<instance_name>/api/now/table/incident');
sm.setHttpMethod('GET');
sm.setQueryParameter("sysparm_query","active=true");
sm.setQueryParameter("sysparm_fields","number,short_description,description,contact_type");
var response = sm.execute();
var responseBody = response.getBody(); //this is return an object contains result array of incidents
So from the Instance B, we can leverage the method GET to pull the data from the Instance A.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-10-2023 11:54 PM
Hi @Kishor O
If you start from Instance A, let's try a POST.
POST https://<instance_name>/api/now/table/{your_custom_table}
Create your Fix Script. Sample below
//Authentication in the Instance B
var username = "<your_user_name>";
var password = "<your_user_password>";
var grIncident = new GlideRecord('incident');
grIncident.addActiveQuery();
grIncident.setLimit(1); //Test with 1 record first
grIncident.query();
while(grIncident.next()){
var sm = new sn_ws.RESTMessageV2();
sm.setBasicAuth(username, password);
//Replace instance_name and table as yours
sm.setEndpoint('https://<instance_name>/api/now/table/{table}');
sm.setHttpMethod('POST');
sm.setRequestHeader("Accept","application/json");
sm.setRequestHeader('Content-Type','application/json');
//Replace with your predefined fields name
var body = {};
body.u_number = grIncident.getValue('number');
body.u_inc_short_description = grIncident.getValue('short_description');
body.u_inc_description = grIncident.getValue('description');
body.u_inc_channel = grIncident.getValue('contact_type');
sm.setRequestBody(JSON.stringify(body));
sm.execute(); //Return response
}
Let me know if it works for you!
Cheers,
Tai Vu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-10-2023 11:59 PM
@Tai Vu Can you please brief me on what I need to configure in instance A and Instance B in the above script?
*In which instance I have to write fix script?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-11-2023 12:11 AM
Hi @Kishor O
From the Instance A where you can get all active Incidents. Create your Fix Script.
In the Instance B, you need to create an User who can create records in the custom table.
Then from the referenced script, we need to replace the username (line 2), password (line 3). Also, we need to correct the <instance_name> and {table} (line 14).
Finally, make sure you're using the correct field names for the body (line 21-24)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-11-2023 12:27 AM
@Tai Vu Do I need to create an endpoint in the Instance B ?
I am very new to Rest integration please guide me If I am wrong?