Hi
I have 8 fields with numerical values and I want to create a "summary" in a new field, which would show per each record which of those 8 are the 3 top ranked fields (name_field&value). Is that possible?
Thanks
Hi
I have 8 fields with numerical values and I want to create a "summary" in a new field, which would show per each record which of those 8 are the 3 top ranked fields (name_field&value). Is that possible?
Thanks
Best answer by Stephen_Orr1
In my previous post, I did not include the name of the field in the output, only the values. Here is the updated version that includes the name of the field:
//Automation script: Sort numeric field values of the trigger record and output top n values
//Input variables: "url", {Airtable record URL}
//by Stephen_Orr1, June 12 2023
//declare config variables
let trigRecUrl = await input.config().url
let trigBaseId = trigRecUrl.match(/(app[^\/]+)/)[0]
let trigTableId = trigRecUrl.match(/(tbl[^\/]+)/)[0]
let trigRecId = trigRecUrl.match(/(rec[^\/]+)/)[0]
//define numeric fields to capture
let trigRecFieldNames = [
'num1',
'num2',
'num3',
'num4',
'num5',
'num6',
'num7',
'num8',
]
//define number of sorted fields to ouput (n)
let n = 3
//query the record
let table = base.getTable(trigTableId)
let queryResult = await table.selectRecordsAsync({fields: trigRecFieldNames})
let record = queryResult.getRecord(trigRecId)
//create array of field value objects
let trigRecValues = trigRecFieldNames.map(f => ({field: f, val: parseFloat(record.getCellValue(f)) || parseFloat(record.getCellValue(f).name)}))
//sort array of field value objects descending
trigRecValues.sort((a, b) => b.val - a.val)
//convert array of field value objects to array of strings
let trigRecValuesStr = trigRecValues.map(f => `${f.field}: ${f.val}`)
//output top n values, reference topValues in next automation step
output.set('topValues', trigRecValuesStr.slice(0, n))
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.