Flow designer action - output - inserting/updating data with Script Include
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-10-2023 10:52 AM
Hi All,
I have created a new ACTION where I am using REST API to get data from 3rd party.
I am getting a Response from third-party app and then I am passing that response to a script include to insert/update records in a table.
var STDMResourcesUTILS = Class.create();
STDMResourcesUTILS.prototype = {
initialize: function() {},
STDMGetResources: function(response_body) {
var responsebody = response_body;
for (var i = 0; i < responsebody.data.length; i++) {
var ResourceID = responsebody.data[i].id;
var ResourceName = responsebody.data[i].name;
var TypeAlias = responsebody.data[i].typeAlias;
var TypeName = responsebody.data[i].typeName;
var HashID = responsebody.data[i].hash;
var Resourcetags = responsebody.data[i].tags;
gs.info("priniting tags " + Resourcetags);
var ResourcetagsString = Resourcetags.toString();
gs.info("priniting tags after adding tostring " + ResourcetagsString);
var gr = new GlideRecord('x_stdm_access_mgmt_resources');
gr.addQuery('resource_id', ResourceID);
gr.query();
if (!gr.next()) {
gs.info("##SI-when resource ID doesn't exist ");
gr.resource_id = ResourceID;
gr.resource_name = ResourceName;
gr.resource_tags = ResourcetagsString;
gr.type_alias = TypeAlias;
gr.type_name = TypeName;
gr.hash = HashID;
gr.insert();
} else {
gs.info("##SI-when resource ID exist " + ResourceID);
var existingHashID = gr.hash;
if (existingHashID != HashID) {
gr.resource_name = ResourceName;
gr.resource_tags = ResourcetagsString;
gr.type_alias = TypeAlias;
gr.type_name = TypeName;
gr.hash = HashID;
gr.update();
}
}
}
},
type: 'STDMResourcesUTILS'
};
Above is the screenshot and code for the same.
My question here is, I want to store the output coming from the response in the output variables and output action as well, but I am confused because via script include I am already inserting and updating the data in the table.
Q1) So, if I create output variables here and then output action also, is it going to store the values twice in the table?
Q2) As I am inserting and updating the data via script include how can I justify my IF and else conditions here to define the output variables?
Q3)Also, I am getting multiple values of the output as I am using for loop to insert and update data, how can I justify to show the multiple values in output variables.
Basically I want output variables and output action step so that I can easily use them in flow designer flow in future.
Please guide me.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-10-2023 06:16 PM
This isn't really a ServiceNow problem, the logic of your javascript needs adjusting. You're just saving over the variable each time you iterate through your loop, if you want to do something like this you'll need to use push and store them in an array.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-12-2023 03:01 PM
Hi Ken,
I did some changes in the script as you mentioned about Push method above.
(function execute(inputs, outputs) {
// ... code ...
if (inputs.status_code == '200') {
// Parse the response_body input variable and save parsed object as responseBody
var responseBody = JSON.parse(inputs.response_body);
gs.info("Here is response body" + responseBody);
var ResIDArray=[];
var ResNameArray = [];
var typealiasArray = [];
var typenameArray = [];
var hashidArray = [];
// var resourcetagsArray = [];
for (var i = 0; i < responseBody.data.length; i++) {
ResIDArray.push(responseBody.data[i].id);
ResNameArray.push(responseBody.data[i].name);
typealiasArray.push(responseBody.data[i].typeAlias);
typenameArray.push(responseBody.data[i].typeName);
hashidArray.push(responseBody.data[i].hash);
// resourcetagsArray.push(responseBody.data[i].tags);
}
ResIDArray.join();
ResNameArray.join();
typealiasArray.join();
typenameArray.join();
hashidArray.join();
// resourcetagsArray();
outputs.resourceid = ResIDArray;
outputs.resourcename = ResNameArray;
outputs.typealias = typealiasArray;
outputs.typename = typenameArray;
outputs.hashid = hashidArray;
// outputs.resourcetags = resourcetagsArray;
var resourceResponse = new x_stdm_access_mgmt.STDMResourcesUTILS();
var payload = resourceResponse.STDMGetResources(responseBody);
}
})(inputs, outputs);
I defined the output variables as aray.string
and action outputs like this below:
Here is the execution result:
I am only confused with 1 thing.
When we get the response body, we parse it and then do the insert/update.
Here I have designed the outputs as array.string.
If I will be using flow designer flow or subflow to call this action and use it's outputs, will it be easy for me to use the outputs the way I have configured.
Please suggest! If there is another way to store multiple values coming in the response body for a particular variable.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-26-2025 04:51 AM - edited ‎02-26-2025 04:52 AM
@geet did you able to fix the issue? need help on the same. Thank you in advance.