How to insert the data into the table using "REST Message" get Method()?

jenny32
Tera Guru

Hi,

I have the API which return "JSON" object, configured the API with Rest Message. It's responding properly, But i need to insert the "Json" object response into one table.

Can anyone suggest how to do it?

Regards,

Jennifer

8 REPLIES 8

Rushit Patel2
Tera Guru

Hi Jennifer,



see below example.



it is calling other instance for incident data. which is JSON. i am then using that data to create records in my instance. modify according to your need.



    var restMessage = new sn_ws.RESTMessageV2();

restMessage.setBasicAuth("username", "passwr");


restMessage.setHttpMethod("get");


restMessage.setEndpoint("http://zuzuinstance.service-now.com/api/now/table/incident");


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


restMessage.setQueryParameter('sysparm_limit','2');


restMessage.setQueryParameter('sysparm_fields','number,short_description');

var response = restMessage.execute();


gs.addInfoMessage("executed");

var res = response.getBody();


var js = JSON.parse(res);
var result = js.result;
for (var i = 0;i < result.length ; i++){
var number = result[i].number;
var short_desc = result[i].short_description;
var gr = new GlideRecord('incident');
gr.initialize();
gr.number = number;
gs.addInfoMessage(number);
gr.short_description = short_desc;
gr.insert();

}



Let me know if you have questions.



Hi


I tried the above code but it's not working. There is no error also no response.


can you suggest other way to do?



Regards


Jennifer


Hi Jennifer,



Have you validated that your API end point is retrieving the information that you're expecting? You could validate your API and the credentials/headers used by using products like postman or runscope.



I hope this helps!



Thanks,


Berny


Hi Jennifer,



1) where are   you executing that code from? make sure that code is getting executed.


2)make sure endpoint and credential are correct(if any). try to query from POSTMAN.


3)can you do console.log(js) after this line var js = JSON.parse(res); see the result in browser's console. tell me what data is coming.