Help

Re: Find all records with a min value using scripting

553 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Shubha_Bala
5 - Automation Enthusiast
5 - Automation Enthusiast

Hi!

I am new to scripting and thought this would be easy but I’m not getting it… I have one table, and want to find all the records that have the minimum value for FieldA (there could be multiple). I actually want to find all the records that have the minimum value for FieldA, and then out of those the ones that have the max for FieldB, and then add those records to the latest row in another table as a linked record. But for now I’d be happy just figuring out how to find the min value for FieldA :slightly_smiling_face:

2 Replies 2

To get the min value for a field (I’m assuming this is a number field) you can do this:

Screenshot 2020-08-21 at 17.16.26

let table = base.getTable('Scores');

let query = await table.selectRecordsAsync();

let min = query.records.reduce((prev, curr) => prev.getCellValue('Score') <= curr.getCellValue('Score') ? prev : curr);

console.log(min.getCellValue('Score'))

Here, we use a reduce method to iterate through the records returned by the query. We define the min record to be the result of the reduce function, which, at each step, passes to the next step the previous iteration record if that is less than the current record or the current record if that is less than the previous record.

In my example data set, the result returned is “2” (from the “B” record). Note that it does not return the “E” record as this is not less than B.

@JonathanBowen Reading the original post, it looks like @Shubha_Bala wants more than just the lowest value (emphasis added by me):

To do that would require another line after the min calculation:

let allMin = query.records.filter(record => record.getCellValue("Score") == min.getCellValue("Score"));