Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Access External API from Business Rule

thisisauniqueus
Giga Expert

Hi,

Can someone please advise me on how can i access a REST full API's endpoint? I have to access an endpoint say for example

http://someotherdomain/api/doSomething/1

is there a way that i can do it in a business rule using the REST Client?

Regards

1 ACCEPTED SOLUTION

Hi John,



You have everything setup properly except for two notes.


1) I had a typo in mine: setHTTPMethod should be setHttpMethod


2) if you are going to use setQueryParameter(), you don't need to set the parameters on the endpoint url. (Anything after a "?" is a parameter)


      rm.setEndpoint('http://api.tvmaze.com/search/shows');


      rm.setQueryParameter("q", "girls");


     


Otherwise if you do both the endpoint would end up looking like this http://api.tvmaze.com/search/shows?q=girls?q=girls


View solution in original post

11 REPLIES 11

shloke04
Kilo Patron

HI,



You can refer the below thread on your query:



Third party User need to update the RITM(requested item) to service now instance using scripted web ...



Hope this helps.Mark the answer as correct/helpful based on impact.



Regards,


Shloke


Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Vyshnavi1
Kilo Contributor

Hi, I have the similar problem.

I created a business rule in such a way that when ever there is an update in the incidents table it have to get the updated data to External API

 

find_real_file.png

This is When condition

find_real_file.png

Just Created Action Script for sending incidents data using Rest API.

This is my action script:

(function executeRule(current, previous /*null when async*/) {
var method = 'post';
var endpoint = 'http://192.168.**.**:8010/incidents/api/servicenow';
var r = new sn_ws.RESTMessageV2();
r.setHttpMethod(method);
r.setEndpoint(endpoint);
r.setRequestHeader("Accept","application/json");
r.setRequestHeader('Content-Type','application/json');
r.setBasicAuth("admin", "Password@123");
var body = incident;
r.setRequestBody(body);
r.setStringParameter("data", incident);
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.addInfoMessage("Response Body:" + response);
gs.addInfoMessage("Response status:" + httpStatus);
})(current, previous);

 

The problem is I am unable to hit this endpoint URL. The code at the ENDPOINT url is 

 

@api_view(['POST'])
def servicenow(request):
if request.method == 'POST':
from ast import literal_eval
d = literal_eval(request.data)
try:
sn = IncidentsServicenow.objects.get(number=d['number'])
sn.number = d['number']
sn.title = d['title']
sn.description = d['description']
sn.state = d['state']
except ServiceNow.DoesNotExist:
sn = ServiceNow()
sn.number = d['number']
sn.title = d['title']
sn.description = d['description']
sn.state = d['state']

sn.save()
# sn = ServiceNowSerializer(data=request.data)
# print (request.data)
# if sn.is_valid():
# sn.save()
return Response({"message": "Got some data!", "data": request.data})
return Response({"message": "Data mismatching"})

 

ANY HELP PLEASE