REST API's not returning response ?

Gaurav Kumar15
Giga Guru

Hi Team,

I am using the OOB rest API for incident table :-

https://instance.service-now.com/api/now/table/incident

I have few doubts :-

1. For creating incident via REST API , if we have to pass the username of Caller

'caller_id':'cb3230342bb3d60083dac7ac17da15af'

e.g :- This i am using for populating a user, is there any other way of populating caller, because 3rd party tool might not aware of the sys id of user record. So how rest will create the incident for some other user.

2. When i am hitting this Rest API, then the incident is getting created but Response is showing as empty in other tool. Does anyone knows why it comes as empty.

Thanks & Regards,

Gaurav Kumar

35 REPLIES 35

sensiple


I'm not sure if you were able to figure out a solution yet but here is one possible way.


Create a scripted REST API within scoped application


Create a script that will redirect the calls to the scoped import set.


Take the response of the redirected call and return as the response for the scripted REST API.



Example below of the Scripted REST API:



(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {


 


  var body = request.body.data;


  var record = createOrUpdate(body);


  if(record){


    return record;


  }


 


  function createOrUpdate(attrs){


    var baseURL = gs.getProperty('glide.servlet.uri');


   


    var request = new sn_ws.RESTMessageV2();


    request.setEndpoint(baseURL + 'api/now/import/<x_place_your_scoped_table_here>');


    request.setHttpMethod('POST');


 


      //for ease of use place credentials in a system property


    var user = gs.getProperty('apiUser');


    var password = gs.getProperty('apiUserPassword');


   


    request.setBasicAuth(user,password);


    request.setRequestHeader("Accept","application/json");


    request.setRequestHeader('Content-Type', 'application/json');


      var data = JSON.stringify(attrs);


      request.setRequestBody(data);


    var response = request.execute();


 


      //logging response for visual check


    gs.log(response.getBody(), "API Test");


    response = JSON.parse(response.getBody());


    return response;


  }


 


})(request, response);



Since the system will view this as an internal transaction being performed on the import set it should give the correct response. Then you're just pushing that response out as the return response for your Scripted REST API.


NOTE: Be sure to point your API call to the Scripted REST API endpoint. You don't have to change the payload just the endpoint.


Create a scripted REST API (make sure you are not in the scoped application space).


Chris, I'm curious about your disclaimer to use global instead of scoped on this?


I used global because it seemed like the reason why the external REST API client was receiving the incorrect response was because of the import set was created within a scoped application. Thus if the Scripted REST API was created within the scoped application it too would not send a correct response.



If that made any sense.


Chuck,



You are correct. It should be set up in the Scoped Application.


Hi All,



I think, there was an issue with my scope application only.


I just deleted my ACL, staging table and i recreated again.



Now everything is working fine.



Thank you sooo much for all who was helping me for past two days.



Thanks & Regards,


Sensiple