Skip to content

Instantly share code, notes, and snippets.

@kuovonne
Last active March 16, 2020 19:20
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/1c38ca3bf87c5656af59381b14f46398 to your computer and use it in GitHub Desktop.
Save kuovonne/1c38ca3bf87c5656af59381b14f46398 to your computer and use it in GitHub Desktop.
getRecordsAsList - helper function for Airtable scripting block
/*******************************************************************************
Title: getRecordsAsList - 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 records and cell values from atable and
makes the cell values accessible with dot notation.
** Parameters **
table: an Airtable API table object
fieldNames: an array of field names in the table whose values are returned
sortFieldName: optional, a field for sorthing the records
sortDirection: optional, the direction for the sort
** Example usage of resulting array **
let records = await getRecordsAsList(table, fieldNames);
recordId = records[index].id;
recordName = records[index].name;
fieldValue = records[index].fields[fieldName];
*******************************************************************************
*/
async function getRecordsAsList(table, fieldNames, sortFieldName = null, sortDirection = "asc") {
// set the options for the query
let queryOptions = { fields: fieldNames };
if (sortFieldName) {
queryOptions["sorts"] = [{field: sortFieldName, direction: sortDirection}]
}
// run the query
let queryResult = await table.selectRecordsAsync(queryOptions);
// get the cell values
let recordsWithFieldValues = queryResult.records.map((record)=> {
let newRecord = { id: record.id,
name: record.name,
fields: {},
};
for (let fieldName of fieldNames) {
newRecord.fields[fieldName] = record.getCellValue(fieldName);
}
return newRecord;
});
return recordsWithFieldValues;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment