Filter Records Based on Metadata for a Field

Last updated: April 21, 2025

You may want to write an action or perform some behavior on the data in your table based on the metadata for a specific field. One way you could accomplish this would be with code like the below example. This would go inside the listener for a sheet action:

const { jobId, sheetId } = event.context;

try {
 // Retrieve all fields from the sheet and filter for Length fields 
 const fieldsResponse = await api.sheets.get(sheetId);
 const lengthFields = fieldsResponse.data.fields.filter(field => field.metadata?.subtype === 'length');
 
 // Retrieve all records from the sheet
 const recordsResponse = await api.records.get(sheetId);
 const records = recordsResponse.data.records;

 // Convert the values of length fields in each record
 const updatedRecords = records.map(record => {
  const updatedRecord = {...record}; // Shallow copy of the record object 

  lengthFields.forEach(field => {
   const fieldValue = record[field.id];
   if (fieldValue) {
    // transform records here
   }
  });
  return updatedRecord;
 });
 console.log(updatedRecords); 
} catch (error) {
 console.error("Failed to process sheet data:", error);
 // maybe signal a job failure
 await api.jobs.fail(jobId, {
  info: "Failed to process length fields", outcome: { message: error.message, },
 });
}