
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-05-2019 07:59 PM
Hello,
I am new to REST API, and I have this requirement to call an outbound rest API in servicenow when a catalog is being submitted. The idea is when a user fills out the form in servicenow and submits it, it will call an api to POST/GET something in the 3rd party tool. Is this possible? Thanks in advance.
Regards,
Raphael
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-05-2019 09:19 PM
Hi Raphael,
Here is a sample script. Let me know if you need any modifications. Also, I added comments for your understanding
var request = new sn_ws.RESTMessageV2();
request.setEndpoint('ENDPOINT of thord party tool');
request.setHttpMethod('POST'); //mention here POST or GET
/*Use one of the below statement to set authentication*/
//request.setBasicAuth(user,password);
request.setRequestHeader('Authorization','Basic SU5UTFxBNjEzNjIzOklORllAMTAwOA==');
//Set here how you are sending the data i.e. XML or JSON
request.setRequestHeader("Accept","application/xml");
request.setRequestHeader('Content-Type','application/xml');
request.setRequestBody('Give the body here to send');
request.setMIDServer('MIDSERVER'); //Set Mid server if you are using any
var response = request.execute(); //It will send the post request to thord party tool
//Use below code to check response
//response.waitForResponse(50);
//response.setHttpTimeout(10000);
var responseBody = response.getBody(); //this now holds the XML inside the envelope, as a string
var xmlDoc = new XMLDocument2();
xmlDoc.parseXML(responseBody);
var val = xmlDoc.getNodeText('/ID');
Please mark this comment as Correct Answer/Helpful if it helped you.
Cheers,
Hardit Singh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-05-2019 08:15 PM
One method would be to use GlideAjax in a Catalog Client Script and send the data from the form to a Script Include with your REST API script. You would have to run the REST API script in the Script Include because the Catalog Client Script will not run on the Server.
Personally, I would create a Run Script activity that has the REST API script and put it in the workflow of your Catalog Item. That way it will easily run once the Catalog Item is submitted and you can easily access all the variables from the form. A workflow activity runs on the Server so you can put the REST API script or just call a Script Include.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-05-2019 08:30 PM
Hello,
Can I have a sample outbound REST API script that you can put in the workflow. Thank you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-05-2019 09:14 PM
Hi Raphel,
yes this is possible i.e. calling REST API and you can call it from any server side script such as BR, Script Include, Workflow etc
Sample outbound rest script below
https://developer.servicenow.com/app.do/app.do#!/lp/technology_partner_program/app_store_learnv2_rest_kingston_exercise_write_a_script_to_invoke_the_rest_message?v=kingston
Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-05-2019 09:19 PM
Hi Raphael,
Here is a sample script. Let me know if you need any modifications. Also, I added comments for your understanding
var request = new sn_ws.RESTMessageV2();
request.setEndpoint('ENDPOINT of thord party tool');
request.setHttpMethod('POST'); //mention here POST or GET
/*Use one of the below statement to set authentication*/
//request.setBasicAuth(user,password);
request.setRequestHeader('Authorization','Basic SU5UTFxBNjEzNjIzOklORllAMTAwOA==');
//Set here how you are sending the data i.e. XML or JSON
request.setRequestHeader("Accept","application/xml");
request.setRequestHeader('Content-Type','application/xml');
request.setRequestBody('Give the body here to send');
request.setMIDServer('MIDSERVER'); //Set Mid server if you are using any
var response = request.execute(); //It will send the post request to thord party tool
//Use below code to check response
//response.waitForResponse(50);
//response.setHttpTimeout(10000);
var responseBody = response.getBody(); //this now holds the XML inside the envelope, as a string
var xmlDoc = new XMLDocument2();
xmlDoc.parseXML(responseBody);
var val = xmlDoc.getNodeText('/ID');
Please mark this comment as Correct Answer/Helpful if it helped you.
Cheers,
Hardit Singh