Service Now Integration : How to send JSON object in REST API call from Workflow ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2014 01:14 AM
Hi Team,
I have recently attended service now Scripting and Orchestration class.
I need your help and expertise to set JSON object is request parameter for sending REST message.
As we know in ServiceNow we have two ways of invoking REST call :
- Setting all parameters in REST Message and using REST Message activity in SN workflow , I am able to make successful connection by this approach. However it has limitation of data being static.
2. As per our use-case we need to use scripting to pass dynamic values in REST parameter. Setting JSON object in script is now working for for us. We are using below script:
---------------------------------------------------------------------------------------------------
connect();
function connect() {
gs.log("-->> Inside WF.connect() ");
try{
//var msg = new RESTMessage('UCSD Instance', 'post');
var msg = new RESTMessage('CentOS-Provisioning REST Call', 'post');
//Data
/*msg.setStringParameter('formatType','json');
msg.setStringParameter('opName','userAPISubmitWorkflowServiceRequest');*/
var data = '{param0:"centOSProvisioningWF",param1:{"list":[{"name":"cpuSize","value":"1"},{"name":"memorySize","value":"2048"},{"name":"storageSize","value":"50"}]},param2:0}';
//var obj = JSON.parse(data);
msg.setXMLParameter(' opData',data);
gs.log("-->> Before Calling UCSD");
var response = msg.execute();
if (response.getStatusCode() == 200) {
gs.log("-->> UCSD Call Sucessful");
} else {
gs.log("Response Code : " + response.getStatusCode() + " received from server");
gs.log("Response Body : " +response.getBody());
}
}catch (err) {
gs.log("Error in connection rule: " + err);
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
As per post : https://community.servicenow.com/message/730308#730308 setXMLParameter should be used for sending JSON object
We are using script
1 msg.setStringParameter('formatType','json');
2 msg.setStringParameter('opName','userAPISubmitWorkflowServiceRequest');
3 var data = '{param0:"centOSProvisioningWF",param1:{"list":[{"name":"cpuSize","value":"1"}, {"name":"memorySize","value":"2048"},{"name":"storageSize","value":"50"}]},param2:0}';
4 msg.setXMLParameter(' opData',data);
If we use first two statement are at script we are getting Error : HTTP Status 400 - Invalid operation name
If we move the the first two statements in "REST message" and last two statements are in script I am not getting connection error: HTTP status 200 . However request is failing for us after authentication with server with error(Improper request format). I am following steps as per SN wiki ( http://wiki.servicenow.com/index.php?title=REST_Message ) section 5.
Kindly let us know if I am missing something or their is any limitation/bug.
I have few questions regarding REST invocation from ServiceNow :
- If parameter are set in REST Message as well as in script which value will override other ?
- How can we take care of order when we set parameters in script ?
- If parameters are set both is REST message as well as in script. How order is managed?
Thanks & Regards,
- Alok Sah
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-27-2014 03:51 AM
Anybody who can help me with this scripting (JSON request) issue.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-27-2014 04:26 AM
Here is what I did with this a little while ago
https://community.servicenow.com/thread/167149
I did use a lot of trail and error with browsers and SOAPUI as I found with the device I was using, the commands and manual were giving some info, but were missing bits or good examples.
I found out via the community / support site or trail and error.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-28-2014 10:21 AM
Thanks Julian
Library Scriptable RESTMessage Library for ServiceNow-John James Andersen worked for me.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-27-2014 04:49 AM
Hi Alok,
Please follow the below code, once put this in the background script and run this, you can make use of this tecnique for making and sending a json object....
var data = {};
data['param_0'] = 'xyz';
var param_1 = {};
var list = [];
var o = {};
o['name'] = 'storage_size';
o['value'] = '20';
list.push(o);
param_1['list']=list;
data['param_1'] = param_1;
gs.print(data);
var json = new JSON();
var text = json.encode(data);
gs.print(text);