Help

Help with Array Script!

Topic Labels: Automations
142 6
cancel
Showing results for 
Search instead for 
Did you mean: 

Hello! Trying to run a script to extract an Array into multiple fields for each submission. For example, we have a form where a user can select from multiple options which inputs the results (Array) into one field. Using automation would like to extract that Array and place it into another table. See below:

New to this, what am I missing:

// Create three records in the Merchant Selection table
console.log(`The value of "SelectedNonprofit" is ${inputConfig.["SelectedNonprofit"]}`);
console.log(`The value of "MerchandName" is ${inputConfig["MerchandName"]}`);


let table = base.getTable("Merchant Selection");
let rawInputs = input.config();

let inputArray = [];
rawInputs['SelectedNonprofit'].forEach( (SelectedNonprofit) => {
  inputArray.push({
    fields: {
      "MerchantName": rawInputs['MerchandName'][0],
      "SelectedNonprofit": rawInputs['SelectedNonprofit'][0],
    }
  });
});
let recordIds = await table.createRecordsAsync(inputArray);
console.log("Created " + inputArray.length + " records!");

6 Replies 6

HI @Ronny_Sage, please find my comments in the code below:

// Create three records in the Merchant Selection table

console.log(`The value of "SelectedNonprofit" is ${inputConfig.["SelectedNonprofit"]}`);
console.log(`The value of "MerchandName" is ${inputConfig["MerchandName"]}`);

/* COMMENT RUPERT: I guess "SelectedNonprofit" is the multi select field? 
Convention for variable naming would be to not have the first letter capitalised. 
Also inputConfig gives you an object with key / value pairs. The key is the name you gave it on the left side.
In Javascript, you either get the value by doing obj.key OR passed as a string like so: obj["key"]
You're combining both here in your first console.log
*/


let table = base.getTable("Merchant Selection");
let rawInputs = input.config();

let inputArray = [];
rawInputs['SelectedNonprofit'].forEach( (SelectedNonprofit) => {
  // COMMENT RUPERT: You're iterating over the array and each item ("SelectedNonprofit") is the object inside the array
  inputArray.push({
    fields: {
      "MerchantName": rawInputs['MerchandName'][0], // here you want to reference the iterated item (SelectedNonprofit) not the raw inputs again
      "SelectedNonprofit": rawInputs['SelectedNonprofit'][0], // same here
    }
  });
});
let recordIds = await table.createRecordsAsync(inputArray);
console.log("Created " + inputArray.length + " records!"); // COMMENT RUPERT: Here I would call .length on "recordsIds", not on inputArray

Thanks so much for this! Running into one more issue, we are trying to do a split of the Array and “split” is not a function in Airtable… any suggestions:

TypeError: rawInputs.selectedNonprofit.split is not a function
at main on line 4

let table = base.getTable("Merchant Selection");
let rawInputs = input.config();
let inputArray = [];
rawInputs['selectedNonprofit'].split(',').forEach( (s) => {
  inputArray.push({
    fields: {
      "merchantName": rawsInputs['merchantName'],
      "nonprofit":  s,
   }
  });
});
let recordIds = await table.createRecordsAsync(inputArray);
console.log("Created " + recordIds.length + " records!");```


![Screen Shot 2022-11-14 at 9.30.32 AM|700x255](upload://tqe242AiGGb4SVtbMZcFPBgZsvm.png)

Airtable scripting is javascript with some libraries so there definitely is a split function. I’m guessing the issue is that rawInputs['selectedNonprofit'] is not producting a String value and so whatever type it is doesn’t have a split function available.

Check the type of the token you are assigning to selectedNonprofit
image

If it isn’t a text type, then you might need explicitly cast the value to a string before you can split it.
rawInputs['selectedNonprofit'].toString().split(',').forEach( (s) => {

Thanks Nathaniel, now I’m running into a different issue:
Screen Shot 2022-11-14 at 1.01.05 PM

Screen Shot 2022-11-14 at 1.01.34 PM

I’ve updated the Value to be the record ID then extracted the record ID to be the linked record in the table but it’s not accepting the string for the record ID… any ideas?

Ah! I didn’t realize you were using a Link to Record field. I believe that the Link field is represented as an array already so you can just do the foreach on it directly and not need to split!

rawInputs['selectedNonprofit'].forEach( (s) => {

If that doesn’t solve the problem, what type of field is “nonprofit” in the “Merchant Selection” table?

@Nathaniel_Granor solved! Thank you!