POST REST

Raghu Loganatha
Kilo Guru

Hi ,

I am trying to do integration for the first time and needed some help.
We have REST API's for GET and POST calls. Figured out how to use GET REST and populate the data.
All this is for a new custom application which we are trying to create. Once the record is created. I want to send post call with all the details from the record.

Things i need help.

1) I have configured REST API, i need to call the REST API and send all the data from record.
2) Steps to do the above.

Let me know what else you need me to provide for help.

Thanks,
Raghu.

11 REPLIES 11

Chuck Tomasi
Tera Patron

Hi Raghu,



I recommend starting with the REST API Explorer to help quickly build your remote application and have it interact with ServiceNow.



REST API explorer



There is also some great training information on the Developer.ServiceNow.com site.


ramireddy
Mega Guru

Hi Raghu,



As per my understanding, You have API of 3rd party application. Whenever, there is a record inserted/updated in ServiceNow, you wants to send those details to your application via that API. If my understanding is correct, you can do the following.



1. Create a script include, which you can call it form multiple places in a servicenow. With in that script include, define a function like below.


sendDetails : function(){


  try


  {




  var r = new sn_ws.RESTMessageV2();


  r.setHttpMethod("


Hi Raghu,



As per my understanding, You have API of 3rd party application. Whenever, there is a record inserted/updated in ServiceNow, you wants to send those details to your application via that API. If my understanding is correct, you can do the following.



1. Create a script include, which you can call it form multiple places in a servicenow. With in that script include, define a function like below.


sendDetails : function(col1,col2,col3){




  r = new sn_ws.RESTMessageV2();


  r.setHttpMethod("get"); // GET or POST verbs, depends on your service


  r.setEndpoint("your API URL");


  r.setRequestHeader("Content-Type", "application/json");


// if any query parameters, you can pass like this.


r.setQueryParameter("col1", col1);


r.setQueryParameter("col2", col2);


r.setQueryParameter("col3", col3);


  // If you have authentication, you can pass like below.


  r.setBasicAuth(coreGR.username, coreGR.password.getDecryptedValue());


  var response = r.execute();


  var responseBody = response.getBody();


  var httpStatus = response.getStatusCode();


  var http_error = response.haveError();



),


2. When the record is inserted/updated, You can implement Business rules of the table in ServiceNow. With in those business rules, you can call the script include function defined in above step. All the data you wants to pass to that API, you can pass via parameters to that script include function form business rule.



current.col1 in business rule will have "col1" column of inserted/updated record.



You can find more methods of ServiceNow outbound REST API in below link.


RESTMessageV2 API - ServiceNow Wiki



Business rules in SNOW


Business Rules - ServiceNow Wiki



As this is the approach usually used, you need to get into the action to have it much more clear understanding. once you define that function in Step-1, you can testing it from a sample UI page as well by passing sample parameters to that function.



Thanks


Rami Reddy


Hi Ram,



This makes sense to me. But still,
If you have any sample Business rules can you post them. I can use them as reference.



Thanks,
Raghu.


This is an example code(I changed names) that I have in my business rule, which is calling a script include.


function onBefore(current, previous) {


var objInstance = new TestInclude();


objInstance.sendDetails(current);


}


In above code, "TestInclude" in the name of my script include and "sendDetails" is the function defined with in that script include. You can see, I am passing "current" object as parameter to that function, which contains the currently inserted/updated record details. In that function, I can directly access those columns by current.name, current.number etc.