How to use Scripted get rest api in servicenow

Kishor O
Tera Sage

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

1 ACCEPTED SOLUTION

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.

View solution in original post

7 REPLIES 7

Tai Vu
Kilo Patron
Kilo Patron

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

@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?

Hi @Kishor O 

From the Instance A where you can get all active Incidents. Create your Fix Script.

TaiVu_0-1697008089368.png

 

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)

 

@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?