How can i do a POST request to External API from a Business Rule

thisisauniqueus
Giga Expert

Hi

i m trying to access an external api from client script ajax but getting into CORS issues. Is it possiblle that i can call the external API from Business Rule ?

Regards

2 REPLIES 2

Brad Tilton
ServiceNow Employee

It is! You can do that by creating a REST Message record and setting up the authentication. Once you set everything up ServiceNow will generate the code you need to put in the business rule automatically.



Outbound REST web service


Create a REST message


Thanks Brad for your time, below is my Client script which is working



var settings = {


   


    "crossDomain": true,


    "url": "https://xyz.com/helloworld",


    "type": "POST",


    "headers": {


        "Content-Type": "application/json"


    },


    "data": "{\r\n   \"operation\": \"echo\",\r\n   \"payload\": {\r\n       \"somekey1\": \"somevalue1\",\r\n       \"somekey2\": \"somevalue2\"\r\n   }\r\n}"


  };



  $j.ajax(settings).done(function (response) {


  alert(response);


    console.log(response);


  });




but when i try to use it by creating a REST message it does not return any thing, here is the code i am using in the BR


find_real_file.png



try {


  var r = new sn_ws.RESTMessageV2('TD - Test', 'post');


  var response = r.execute();


  var responseBody = response.getBody();


  var httpStatus = response.getStatusCode();


  gs.addInfoMessage("responseBody: "+responseBody);


  gs.addInfoMessage("httpStatus: "+httpStatus);



  }


  catch(ex) {


  var message = ex.getMessage();


  gs.addErrorMessage("error:"+message);



  }



Regards