Skip to content

Instantly share code, notes, and snippets.

@kuovonne
Created March 16, 2020 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuovonne/098e319d513d1a57dc357f4bbaa70606 to your computer and use it in GitHub Desktop.
Save kuovonne/098e319d513d1a57dc357f4bbaa70606 to your computer and use it in GitHub Desktop.
getRecordsAsHash - helper function for Airtable scripting block
/*******************************************************************************
Title: getRecordsAsHash - helper function for Airtable scripting block
Author: Kuovonne Vorderbruggen
Date Created: March 16, 2020
Copyright (c) 2020 by Kuovonne Vorderbruggen
License: MIT License
** Descripton **
This function gets the specified field values for all the records in the table
and then stores them in a hash table, with the record id as the key.
This provides very quick lookup of field values based on record id.
** Parameters **
table: a scripting API table object
fieldNames: an array of field names for the given table
** Example usage of resulting hash table **
let recordsHash = await getRecordsAsHash(table, fieldNames);
let fieldValue = recordsHash[recordId][fieldName];
*******************************************************************************
*/
async function getRecordsAsHash(table, fieldNames) {
// get the records with just the desired fields
let queryResult = await table.selectRecordsAsync({ fields: fieldNames });
// create the hash and fill it with the values (including any nulls);
let recordHash = {};
queryResult.records.forEach((record)=> {
let fieldValues = {};
for (let fieldName of fieldNames) {
fieldValues[fieldName] = record.getCellValue(fieldName);
}
recordHash[record.id] = fieldValues;
});
return recordHash;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment