Call Rest API from Script include from flow designer without integration hub
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-27-2022 09:00 AM
Hi All,
Could someone help me how to make Rest calls from Script include from flow designer for following flow :
We have the following workflow to update members of an entitlement on submitting a catalog request in servicenow :
1. Create a request to add members in an entitlement
2. Send for approvals and if the approval is done, then an API call has to be made from Script include function and trigger the Api call and the response from teh end point has to be updated in the RITM
3. Based on the response is success then we have to update the Entitlement table wiht the user list sent by the 3rd party and close the RITM otherwise failure , we have to create a manual task and assign to a particular group.
Could you please help me with the integration trigger without the integration hub
Thanks and Regards
- Labels:
-
Integrations
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-27-2022 11:35 AM
Hi, as long as you can run a script from your flow then all you should need to do is create a function within your script-include to make your rest calls using restmessageV2 API, then call the function from your script.
https://developer.servicenow.com/dev.do#!/reference/api/rome/server/sn_ws-namespace/c_RESTMessageV2API?navFilter=RESTMessageV2+
If your function allows you to pass in parameters like target table, date range etc, then you can utilize 1 function for multiple different requirements.
I would also recommend reviewing the developer portal REST course, the SNC API explorer
https://developer.servicenow.com/dev.do#!/learn/courses/rome#rest-integrations
https://docs.servicenow.com/bundle/rome-application-development/page/integrate/inbound-rest/concept/use-REST-API-Explorer.html
For processing returned payloads/data my preference would be a scripted rest API solution, but how you manage the returned result\data is entirely up to you.
https://docs.servicenow.com/bundle/rome-application-development/page/integrate/custom-web-services/concept/c_CustomWebServices.html
https://www.youtube.com/watch?v=DyIuma1cZrg

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-27-2022 04:50 PM
It's possible to create an Action with a Script step. In the Script step, have sn_ws.RESTMessageV2() to do the REST call.
(function execute(inputs, outputs) {
var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://<url>');
request.setHttpMethod('GET');
var user = '<username>';
var password = '<password>';
request.setBasicAuth(user,password);
request.setRequestHeader("Accept","application/json");
request.setRequestHeader('Content-Type','application/json');
var response = request.execute();
outputs.body = response.getBody();
})(inputs, outputs);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2022 06:12 AM
Hi
Flow action script is like below :
Input variables :
(function execute(inputs, outputs) {
// ... code ...
var creategrp = new global.oimIntegrationUtils;
var response = creategrp. createGroup(inputs.reqitem.reqItemFromCatalog, inputs.group.groupSysId);
gs.log("FD1" + reqItemFromCatalog) ;
outputs.variable = msg;
gs.log("FD2" + msg );
})(inputs, outputs);
Following is the script include function:
var IntegrationUtils = Class.create();
IntegrationUtils.prototype = {
initialize: function() {},
//Create Group - (Call the function with Requested Item Sysid, GroupSysID from variable and framed jsonBody)
createGroup: function(reqItemFromCatalog, groupSysId, jsonBody) {
//The function needs to be called with the defined Json as a parameter
try {
var restCreate = new sn_ws.RESTMessageV2('ServiceNow ABC Group', 'Create group');
var groupTable = 'u_aps_entitlements';
var groupObj = {};
//Generate the Json Request - Send this when a group(groupSysId) is created
var groupGR = new GlideRecord(groupTable);
groupGR.addQuery('sys_id', groupSysId);
groupGR.query();
if (groupGR.next()) {
groupObj.display_name = groupGR.getValue('u_group_name');
groupObj.description = groupGR.getValue('u_description');
groupObj.owner = groupGR.getValue('u_group_owner'); //Single User ID
groupObj.deputy = groupGR.getValue('u_deputy'); //Array of UserIDs
}
var finalJsonFromCatalog = {
"source_system": "PAGE",
"operation": "Create",
"owner": groupObj.owner,
"deputy": groupObj.deputy,
};
var sampleJson = {
"source_system": "PAGE",
"operation": "Create",
"owner": groupObj.owner,
"deputy": groupObj.deputy,
};
gs.info('RR From ABC Create Group: ' + JSON.stringify(finalJsonFromCatalog));
restCreate.setStringParameterNoEscape('createJson', finalJsonFromCatalog);
var response = restCreate.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
} catch (ex) {
var message = ex.message;
}
return 'The call to ABC to create Group is successful';
},
I will be grateful for immediate help as I need to show this demo by Monday.
Thanks and Regards.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-03-2022 07:01 PM
Hi Indira,
Not sure if you fix this problem but for me it's working fine in script step.
(function execute(inputs, outputs) {
var scriptInc = new Atlas();
var responseBody = scriptInc.putExpirationDate(inputs.1,inputs.2);
var atlas = JSON.parse(responseBody);
outputs.response_body = JSON.stringify(atlas, null, 2);
})(inputs, outputs);