Flow designer action - output - inserting/updating data with Script Include

geet
Tera Guru

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.

geet_0-1696959510157.png

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?

geet_1-1696960038341.png

 

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.


 

7 REPLIES 7

Sandeep Rajput
Tera Patron
Tera Patron

@geet Since you are already inserting/updating data using Script include, why would you like to define the output variable in the first place? If I were in your place, I would have simply used another script step where the input to that step could be the output of the rest step, inside my script step I would have called my script include and stored/update the data in the database.

 

Also, you could have chosen to create 1 custom action for making the API, could have set the output variables of that action using the output of the REST API. Created another action (custom action 2 ) where the Output from the Rest action could have been passed as an input and storage of data could have been taken care of in the custom action 2.

Hi Sandeep,

Here are my responses to your questions:

Since you are already inserting/updating data using Script include, why would you like to define the output variable in the first place?

I want output variables and output action step so that I can easily use them in flow designer flow in future. Also, I wanted to do error handling and logging for the output I am getting.

geet_0-1696961581796.png

 

If I were in your place, I would have simply used another script step where the input to that step could be the output of the rest step, inside my script step I would have called my script include and stored/update the data in the database.

 

But, I am already using the output of the REST step as a Response body and then passing that response to script include to store my data. 

If you are referring something different than this, could you please share an example?

geet_1-1696961701959.png

geet_2-1696961736935.png

 

 

Also, you could have chosen to create 1 custom action for making the API, could have set the output variables of that action using the output of the REST API. Created another action (custom action 2 ) where the Output from the Rest action could have been passed as an input and storage of data could have been taken care of in the custom action 2.

I am using script Include to insert/update the data as once I deliver the custom app to 3rdparty people and if they want to change something in the code they can go in the script include and change it, they don't need to go into actions to change the code and there are more chances of error if they make mistakes using actions as they are not ServiceNow trained.




KevinBellardine
Kilo Sage

Just to restate the problem here, you've got a script step in your action that is using a script include to do the updates to your record. You'd like to have the variables you're passing to the script include be output by the script step.

 

Some points I noticed

 

1. There are no inputs/outputs configured for the script step

2. Your payload variable is being used to call a function that is actually writing to the SN table, but is not returning anything to the variable

 

As far as the outputs, creating outputs for the script action wouldn't by itself cause additional updates. You could create outputs for each of the values you're returning, you would just have to do the parsing in the script step itself. You would write something like this after creating an output for ID.

 

outputs.ID = responsebody.data[i].id

 

 Overall I'd recommend using a script include for heavy data massaging and logic, and use the script actions themselves for the functionality. For example:

 

1. Use the Script Include to send a JSON object back with the fields

2. Output the object from the script step

3. Pass the object to a JSON Parser step

4. Use an Update Record step to update the records

 

You could even forgo the script include entirely and just pass the responseBody out of the script step and into the JSON Parser. Flow Designer is intended to be a no code/low code tool, you should lean into that as much as you can.

 

https://docs.servicenow.com/bundle/vancouver-integrate-applications/page/administer/flow-designer/re...

Hi @KevinBellardine 

I tried what you suggested but in the execution I see only one output but in the actual table I am getting the full data because of the script include.
Here is how I added the outputs that you suggested above,

geet_0-1696984340240.pnggeet_1-1696984364155.pnggeet_2-1696984391862.png

Execution has only 1 record:

geet_3-1696984456627.pnggeet_4-1696984495840.png

But my table got all the 4 values because of the script include code:

geet_5-1696984544511.png

why Output step in execution has just 1 value even if I used the for loop.