The Zurich release has arrived! Interested in new features and functionalities? Click here for more

send POST request when a business rule is activated

xstaffelbach
Kilo Contributor

I am trying to activate a POST request whenever a business rule is activated, because I want to send the contents of a ticket to my local database whenever a new ticket is made.

I have tried to add this code just to see if I can get the 2 servers talking (obviously this would not send the ticket contents):

(function executeRule(current, previous /*null when async*/) {

var xhttp = new XMLHttpRequest();      

xhttp.open("POST", "https://my_ip:5000/todos", true);

xhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");

xhttp.send(JSON.stringify({ "todo_id": 10, "task":"FROM SNOW :D"}));

})(current, previous);

as a script in the advanced section of the business rule with a blank condition but it does not run with the business is activated. I have a message being sent to myself, so I know the rule is being activated but my server does not hear the POST.

I can runt the 4 middle lines in my firefox console and they work.

For more context if necessary (both of these questions are mine):

python - Adding a curl request into a serviceNow business rule (javascript) - Stack Overflow

python - Calling flask via javascript run on an https external site - Stack Overflow

1 ACCEPTED SOLUTION

mohamadcharafed
ServiceNow Employee
ServiceNow Employee

Hi Matthew,



As Sirni mentioned, the WIki link above is a very good resource.


If you want to send an outbound message its better to use the provided ServiceNow features. So you can :



1) Choose wether you want REST or SOAP and create the appropriate outbound message in the   "System Web Services" App of your instance


rest1.png


2) After you create the message you can click on it, and go to 'HTTP Methods' at the bottom and click one of them. Then click 'Preview Script Usage' and it will give you the code you need to use this messaeg in the Business Rules.


rest2.png



The script displayed can be used in your Business Rules now.



Cheers


View solution in original post

5 REPLIES 5

Srinivas Balusu
Kilo Guru

Did you setup the outbound message first?



Check 4.1 in below URL.



Outbound REST Web Service - ServiceNow Wiki




This give you the script to be used in Business Rules


The ServiceNow Wiki content is no longer supported. Updated information about this topic is located here:
Outbound REST Web Service



Visit http://docs.servicenow.com for the latest product documentation


mohamadcharafed
ServiceNow Employee
ServiceNow Employee

Hi Matthew,



As Sirni mentioned, the WIki link above is a very good resource.


If you want to send an outbound message its better to use the provided ServiceNow features. So you can :



1) Choose wether you want REST or SOAP and create the appropriate outbound message in the   "System Web Services" App of your instance


rest1.png


2) After you create the message you can click on it, and go to 'HTTP Methods' at the bottom and click one of them. Then click 'Preview Script Usage' and it will give you the code you need to use this messaeg in the Business Rules.


rest2.png



The script displayed can be used in your Business Rules now.



Cheers


Thanks for the help.



I am trying to make a put call that has the same functionality as:


curl http://[my_ip]:5000/todos/ -X GET -v


I first start at this page:



Screen Shot 2017-08-10 at 2.13.17 PM.png





Then I click submit



And then I make the GET call here:



Screen Shot 2017-08-11 at 11.06.06 AM.png



But when I hit test I keep getting a failed response.



Screen Shot 2017-08-11 at 11.03.41 AM.png



Even if I just click the endpoint link as written in the HTTP Method chrome will get a valid response; it will print the JSON from my server.



Am I missing a step?