Create Array.Object from record fields in Flow Designer

DylanB
Tera Guru

Hi everyone, in Flow Designer, I want to choose fields in a record and turn those fields into an array.object. I'm trying to do this because I need to use an array.object with the Create Row action on the Smartsheet spoke. 

 

I've created an action to take inputs and turn them into an array.object, but it's not returning anything. Screenshots and code below. Does anyone know what I'm doing wrong (probably in the script)?

DylanB_1-1723480428092.png

DylanB_2-1723480455206.png

(function execute(inputs, outputs) {
    var inputArray = [];
    // Loop through each input and add it to the array as an object
    for (var i = 1; i <= 3; i++) { // Adjust the loop limit based on the number of inputs
        var inputName = 'Input' + i;
        if (inputs[inputName]) {
            inputArray.push({
                name: inputName,
                value: inputs[inputName]
            });
        }
    }
    
    // Set the output to the array of objects
    outputs.arrayofobjects = inputArray;
})(inputs, outputs);

DylanB_3-1723480501341.png

DylanB_4-1723480508391.png

DylanB_5-1723480592873.png

 

1 ACCEPTED SOLUTION

DylanB
Tera Guru

After more research, I got an action to working that creates an array.object of a record query. Who knows how much I’ll use this, but it might come in handy for others, so I’ll share the info about it.

 

Inputs

DylanB_0-1723843515459.png

I wanted to make this action as table independent as possible, so I created input variables for the Table and Conditions. Previously, I didn’t know this was possible to create table and condition variables in an action so this was a useful find.

For the Conditions input variable, it’s important to go into Advanced options and set it so that it’s dependent on the Table input.

DylanB_1-1723843515461.png

The action output includes about 35 fields but since it’s table independent, I wanted to be able to define fields other than the included fields, so that’s what the ‘Additional Field’ input variables are for. For these to work, the field name must be entered, not the field label.

 

Look Up Records Step

DylanB_2-1723843515464.png

The Look Up Records step is mostly self-explanatory. Table and Conditions are pulled from their respective inputs.

 

Script Step

DylanB_3-1723843515467.png

 

 For the Script step, we grab the Record lookup results from the previous step, and the Additional Fields from the inputs.

Here’s the script I used (forgive any poor scripting, I don’t often write scripts 😊).

 

(function execute(inputs, outputs) {
  //Create an empty array
  var recordsArray = [];
  var i = 0;
  //Iterate through the list of records
  while(inputs.records.next()) {
    //Create an empty object for each iteration
    var recordObject = {};
    //Query records to assign object values
    recordObject.active = inputs.records.getDisplayValue('active');
    recordObject.comments = inputs.records.getDisplayValue('comments');
    recordObject.assigned_to = inputs.records.getDisplayValue('assigned_to');
    recordObject.assignment_group = inputs.records.getDisplayValue('assignment_group');
    recordObject.caller_id = inputs.records.getDisplayValue('caller_id');
    recordObject.category = inputs.records.getDisplayValue('category');
    recordObject.close_code = inputs.records.getDisplayValue('close_code');
    recordObject.close_notes = inputs.records.getDisplayValue('close_notes');
    recordObject.closed_at = inputs.records.getDisplayValue('closed_at');
    recordObject.closed_by = inputs.records.getDisplayValue('closed_by');
    recordObject.cmdb_ci = inputs.records.getDisplayValue('cmdb_ci');
    recordObject.contact_type = inputs.records.getDisplayValue('contact_type');
    recordObject.description = inputs.records.getDisplayValue('description');
    recordObject.sys_created_on = inputs.records.getDisplayValue('sys_created_on');
    recordObject.sys_created_by = inputs.records.getDisplayValue('sys_created_by');
    recordObject.number = inputs.records.getDisplayValue('number');
    recordObject.opened_at = inputs.records.getDisplayValue('opened_at');
    recordObject.opened_by = inputs.records.getDisplayValue('opened_by');
    recordObject.priority = inputs.records.getDisplayValue('priority');
    recordObject.resolved_at = inputs.records.getDisplayValue('resolved_at');
    recordObject.state = inputs.records.getDisplayValue('state');
    recordObject.short_description = inputs.records.getDisplayValue('short_description');
    recordObject.subcategory = inputs.records.getDisplayValue('subcategory');
    recordObject.sys_id = inputs.records.getDisplayValue('sys_id');
    recordObject.sys_updated_on = inputs.records.getDisplayValue('sys_updated_on');
    recordObject.sys_updated_by = inputs.records.getDisplayValue('sys_updated_by');
    recordObject.urgency = inputs.records.getDisplayValue('urgency');
    recordObject.work_notes = inputs.records.getDisplayValue('work_notes');

    //Declare the additional input variables and define them.
    var additionalfield1 = inputs.additional_field_1;
    recordObject.additional_field1 = inputs.records.getDisplayValue(additionalfield1);
    var additionalfield2 = inputs.additional_field_2;
    recordObject.additional_field2 = inputs.records.getDisplayValue(additionalfield2);
    var additionalfield3 = inputs.additional_field_3;
    recordObject.additional_field3 = inputs.records.getDisplayValue(additionalfield3);
    var additionalfield4 = inputs.additional_field_4;
    recordObject.additional_field4 = inputs.records.getDisplayValue(additionalfield4);
    var additionalfield5 = inputs.additional_field_5;
    recordObject.additional_field5 = inputs.records.getDisplayValue(additionalfield5);

    //Add current object to array
    recordsArray[i] = recordObject;
    i += 1;
  }
  outputs.records = recordsArray;
})(inputs, outputs);

 

 

DylanB_4-1723843515469.png

The output variables are too long to take a full screenshot of, but the idea is simple. First, create an array.object output variable for ‘records’, then a child variable for ‘record’, then child item variables for each value you’re getting from the script.

 

Output Variables

DylanB_5-1723843515472.png

The Action outputs are set up to match the script step outputs.

 

Running the Action

DylanB_6-1723843515473.png

 

When running the action, you enter your table, conditions, and any additional fields you’d like to see data from.

The action then returns the fields related to the record(s) found. If fields are not available on that object, "null" is returned.

DylanB_7-1723843515479.png

 

@Josh Gold Thank you for the reply! Since I'm still having issues with Smartsheet creating rows from the array.object that I'm pushing to it, I may be looking into Unito.

 

View solution in original post

2 REPLIES 2

Josh Gold
Tera Expert

@DylanB It could be a case sensitivity issue with input naming. If there are any discrepancies, the script would miss those values. 

 

Otherwise, I'm not sure. I would start by adding some debugging statements to see if that helps you find the problem. 

 

You could use gs.log() within the script to print the contents of inputArray before setting the output. This will help you see if the array is being populated as expected.

 

If you're open to abandoning the spoke altogether though, I wrote this guide on how to sync ServiceNow records to Smartsheet rows with Unito. It uses a configurable, no-code flow builder that's a lot simpler to work through than a custom script.

DylanB
Tera Guru

After more research, I got an action to working that creates an array.object of a record query. Who knows how much I’ll use this, but it might come in handy for others, so I’ll share the info about it.

 

Inputs

DylanB_0-1723843515459.png

I wanted to make this action as table independent as possible, so I created input variables for the Table and Conditions. Previously, I didn’t know this was possible to create table and condition variables in an action so this was a useful find.

For the Conditions input variable, it’s important to go into Advanced options and set it so that it’s dependent on the Table input.

DylanB_1-1723843515461.png

The action output includes about 35 fields but since it’s table independent, I wanted to be able to define fields other than the included fields, so that’s what the ‘Additional Field’ input variables are for. For these to work, the field name must be entered, not the field label.

 

Look Up Records Step

DylanB_2-1723843515464.png

The Look Up Records step is mostly self-explanatory. Table and Conditions are pulled from their respective inputs.

 

Script Step

DylanB_3-1723843515467.png

 

 For the Script step, we grab the Record lookup results from the previous step, and the Additional Fields from the inputs.

Here’s the script I used (forgive any poor scripting, I don’t often write scripts 😊).

 

(function execute(inputs, outputs) {
  //Create an empty array
  var recordsArray = [];
  var i = 0;
  //Iterate through the list of records
  while(inputs.records.next()) {
    //Create an empty object for each iteration
    var recordObject = {};
    //Query records to assign object values
    recordObject.active = inputs.records.getDisplayValue('active');
    recordObject.comments = inputs.records.getDisplayValue('comments');
    recordObject.assigned_to = inputs.records.getDisplayValue('assigned_to');
    recordObject.assignment_group = inputs.records.getDisplayValue('assignment_group');
    recordObject.caller_id = inputs.records.getDisplayValue('caller_id');
    recordObject.category = inputs.records.getDisplayValue('category');
    recordObject.close_code = inputs.records.getDisplayValue('close_code');
    recordObject.close_notes = inputs.records.getDisplayValue('close_notes');
    recordObject.closed_at = inputs.records.getDisplayValue('closed_at');
    recordObject.closed_by = inputs.records.getDisplayValue('closed_by');
    recordObject.cmdb_ci = inputs.records.getDisplayValue('cmdb_ci');
    recordObject.contact_type = inputs.records.getDisplayValue('contact_type');
    recordObject.description = inputs.records.getDisplayValue('description');
    recordObject.sys_created_on = inputs.records.getDisplayValue('sys_created_on');
    recordObject.sys_created_by = inputs.records.getDisplayValue('sys_created_by');
    recordObject.number = inputs.records.getDisplayValue('number');
    recordObject.opened_at = inputs.records.getDisplayValue('opened_at');
    recordObject.opened_by = inputs.records.getDisplayValue('opened_by');
    recordObject.priority = inputs.records.getDisplayValue('priority');
    recordObject.resolved_at = inputs.records.getDisplayValue('resolved_at');
    recordObject.state = inputs.records.getDisplayValue('state');
    recordObject.short_description = inputs.records.getDisplayValue('short_description');
    recordObject.subcategory = inputs.records.getDisplayValue('subcategory');
    recordObject.sys_id = inputs.records.getDisplayValue('sys_id');
    recordObject.sys_updated_on = inputs.records.getDisplayValue('sys_updated_on');
    recordObject.sys_updated_by = inputs.records.getDisplayValue('sys_updated_by');
    recordObject.urgency = inputs.records.getDisplayValue('urgency');
    recordObject.work_notes = inputs.records.getDisplayValue('work_notes');

    //Declare the additional input variables and define them.
    var additionalfield1 = inputs.additional_field_1;
    recordObject.additional_field1 = inputs.records.getDisplayValue(additionalfield1);
    var additionalfield2 = inputs.additional_field_2;
    recordObject.additional_field2 = inputs.records.getDisplayValue(additionalfield2);
    var additionalfield3 = inputs.additional_field_3;
    recordObject.additional_field3 = inputs.records.getDisplayValue(additionalfield3);
    var additionalfield4 = inputs.additional_field_4;
    recordObject.additional_field4 = inputs.records.getDisplayValue(additionalfield4);
    var additionalfield5 = inputs.additional_field_5;
    recordObject.additional_field5 = inputs.records.getDisplayValue(additionalfield5);

    //Add current object to array
    recordsArray[i] = recordObject;
    i += 1;
  }
  outputs.records = recordsArray;
})(inputs, outputs);

 

 

DylanB_4-1723843515469.png

The output variables are too long to take a full screenshot of, but the idea is simple. First, create an array.object output variable for ‘records’, then a child variable for ‘record’, then child item variables for each value you’re getting from the script.

 

Output Variables

DylanB_5-1723843515472.png

The Action outputs are set up to match the script step outputs.

 

Running the Action

DylanB_6-1723843515473.png

 

When running the action, you enter your table, conditions, and any additional fields you’d like to see data from.

The action then returns the fields related to the record(s) found. If fields are not available on that object, "null" is returned.

DylanB_7-1723843515479.png

 

@Josh Gold Thank you for the reply! Since I'm still having issues with Smartsheet creating rows from the array.object that I'm pushing to it, I may be looking into Unito.