Jun 04, 2023 02:53 AM - edited Jun 06, 2023 12:26 AM
I want to use a single select that is submitted via a form to populate a field in this record called art_tone.
This is Airtable automation.
Note: the aim of this was to use this approach for a few thousand variations so IF statements with No code would hit the 25 actions limit very quickly.
The input variables are the airtable record id = recordid. The other input variable is the field heading name we are looking for in the 'Name 1' table. This is named name1fieldname, the value of this input is from the field 'tag' from art1. I am only interested in the first record [0]. I want the cell value from the field with the value 'tag' pulled through from the input variable name1fieldname.
Help?
// Access the record passed in from the automation
let inputConfig = input.config();
console.log(inputConfig);
// Get the values from the record fields
let recordid = inputConfig.recordid;
let parentingfieldname = inputConfig.parentingfieldname; // Input variable parentingfieldname
// Here's the main function:
async function main(params) {
// Read the input parameters:
let { parentingfieldname, recordid } = params;
// Log the input parameters for debugging:
console.log({ parentingfieldname, recordid });
// Define your tables:
let articlesTable = base.getTable('articles1');
let parentingTable = base.getTable('parenting');
// Get the record from the articlesTable:
let record = await articlesTable.selectRecordsAsync().then(result => result.getRecord(recordid));
// Get the parenting field value of the record:
let parentingFieldValue = record.getCellValue(parentingfieldname);
// Make sure the parentingFieldValue is not null or undefined:
if(parentingFieldValue === null || parentingFieldValue === undefined) {
throw new Error(`Field ${parentingfieldname} is not set for record ${recordid}`);
}
// Fetch the first record from the parentingTable:
let firstParentingRecord = await parentingTable.selectRecordsAsync().then(result => result.records[0]);
// Get and log the fields of the parenting table:
let parentingFields = parentingTable.fields;
console.log("Fields in the parenting table: ", parentingFields.map(field => field.name));
// Check if parentingfieldname is a valid field in the parenting table:
if(!parentingFields.map(field => field.name).includes(parentingfieldname)) {
throw new Error(`Field ${parentingfieldname} does not exist in the parenting table.`);
}
// Get the field value for the parentingfieldname:
let fieldValue = firstParentingRecord.getCellValue(parentingfieldname);
// Log the fieldValue for debugging:
console.log(`Field Value of ${parentingfieldname} : `, fieldValue);
// Now we update the articlesTable's record:
await articlesTable.updateRecordAsync(recordid, {
"prompt_tone": fieldValue
});
}
I have checked to ensure the input variable parentingfieldname coming from 'articles1' are identical to the field names found in the table 'parenting'.
Jun 04, 2023 07:56 AM
One thing is you never call your main function, however, wrapping everything in main here isn't necessary. Airtable wraps everything in an async function for you so you can use await at the top level.
It's difficult to follow your field names from the prompt and code as they seem to contradict. What fields/tables are you using?
Table: articles1, field: prompt_tone (single line text)
Table: parenting, field: tag (single-select)
Is that correct?
Lastly, this use case can pretty easily be accomplished with Airtable's no-code automations.