Help

Re: Is there a way to select the record above a current record

Solved
Jump to Solution
2224 4
cancel
Showing results for 
Search instead for 
Did you mean: 

I’m trying to make a script that’ll work along side the Gantt chart.
I want the script to automatically to find the record above the current record and push that into a link field to create a dependency.

Does anyone know if that possible?
I’ve been looking through the documentation, and haven’t found anything to get me started.

1 Solution

Accepted Solutions

Sorry about that, @Kim_Trager1 – I gave you a bad example. I’m no JavaScript wizard, and was trying patch together some help for you while on my phone.

I looked up the way I’ve done this in the past, and I’ve been able to use the Array.map() function to get the index of the record under iteration:

const luPlanRecords = await prodView.selectRecordsAsync();

let someNewArrayOfLuPlanRecords = luPlanRecords.records.map(record, index => {
  let recordAbove = luPlanRecords.records[index - 1]
  ...
});

Array.map allows you to express a variable to hold the current index under iteration as well (separated by a comma after the array object under iteration). So inside the map arrow function, you can directly ask for the index of the record.

However, 2 things to note about this:

  • as I said before, you need to be careful of the way this behaves with the first record - I’m not sure exactly what it will do, but you’ll need some sort of guard clause to treat the first record differently
  • since this is a .map function, it’s not behaving quite the same way as a for loop – you need to return something from the arrow function, and the ‘something’ that gets returned will be saved into the new array – not sure if this will work for what you are wanting to do; you can still process logic in the arrow function that’s unrelated to whatever gets returned, but it’s best to return some value that represents the effect of what you did in the arrow function

Hope that makes sense.

Another approach, if .map isn’t quite working the way you want, is to just use an iteration counter with the for loop, and let that iteration counter represent the value of the index under iteration:

const luPlanRecords = await prodView.selectRecordsAsync();

let index = 0
for (let record in luPlanRecords.records) {
  console.log(index)   //expect '0' on first loop, '1' on second, etc.
  let recordAbove = luPlanRecords.records[index - 1];
  ... //processing logic

  index++   //iterate index for next loop
}

This is more similar to what you are trying already. Again though, need to be careful with that first record.

See Solution in Thread

7 Replies 7

As someone who knows nothing about the scripting block, I would not think it is possible since you can change the sort from one view to another?

Maybe if you use an autonumber field and choose the number above that one?

In the script block you can pass a RecordQueryResult which can be based of a view.
I’m assuming that there must be something that controls the sorting order that potentially can be used to determine the above record, but I can’t find anything on sort. So was wondering if I’m this is possible at all.

For what it’s worth, I don’t think the order of records in a RecordQueryResult can be trusted to match the order of those records in the View itself — I’m pretty sure the JavaScript API is going to pull the records into an array in whatever order they happen to load fastest — if a record at the bottom finishes processing before one higher up, it might end up in the array first.

That’s probably why Airtable offers the “sort” option in the table.selectRecordsAsync() method. So if you use that option to ensure your records are sorted by the same criteria as your View, then you should be safe to access “the record before(above) some other record” by something like:

for (let record in records) {
   let recordAbove = records[record.index -1]
   ...
}

(Syntax might be a bit off, and I’m not sure the index is actually accessible off the record like that, but it gets at the idea…)

You just have to be careful how you use that because it might throw an error for the first record, or it might return the last record as being above the first record.

Thank you Jeremy

I’ll see what I can come up with and revert back.

Hi @Jeremy_Oglesby I tried all sorts of different ways to get the index from my record.
However .index does not seem to be valid.
Then I tried with various things, but kind of stuck getting the index of the any of the records.

So i’m not sure if the index is that easily available.

const luPlanRecords = await prodView.selectRecordsAsync();

console.log(luPlanRecords.records)

for (let record in luPlanRecords.records) {
    //console.log(luPlanRecords.records.indexOf())
    console.log(luPlanRecords.records[luPlanRecords.records.indexOf(record.id)])
   //let recordAbove = luPlanRecords.records[record.index() -1]
}

Sorry about that, @Kim_Trager1 – I gave you a bad example. I’m no JavaScript wizard, and was trying patch together some help for you while on my phone.

I looked up the way I’ve done this in the past, and I’ve been able to use the Array.map() function to get the index of the record under iteration:

const luPlanRecords = await prodView.selectRecordsAsync();

let someNewArrayOfLuPlanRecords = luPlanRecords.records.map(record, index => {
  let recordAbove = luPlanRecords.records[index - 1]
  ...
});

Array.map allows you to express a variable to hold the current index under iteration as well (separated by a comma after the array object under iteration). So inside the map arrow function, you can directly ask for the index of the record.

However, 2 things to note about this:

  • as I said before, you need to be careful of the way this behaves with the first record - I’m not sure exactly what it will do, but you’ll need some sort of guard clause to treat the first record differently
  • since this is a .map function, it’s not behaving quite the same way as a for loop – you need to return something from the arrow function, and the ‘something’ that gets returned will be saved into the new array – not sure if this will work for what you are wanting to do; you can still process logic in the arrow function that’s unrelated to whatever gets returned, but it’s best to return some value that represents the effect of what you did in the arrow function

Hope that makes sense.

Another approach, if .map isn’t quite working the way you want, is to just use an iteration counter with the for loop, and let that iteration counter represent the value of the index under iteration:

const luPlanRecords = await prodView.selectRecordsAsync();

let index = 0
for (let record in luPlanRecords.records) {
  console.log(index)   //expect '0' on first loop, '1' on second, etc.
  let recordAbove = luPlanRecords.records[index - 1];
  ... //processing logic

  index++   //iterate index for next loop
}

This is more similar to what you are trying already. Again though, need to be careful with that first record.

Thank you @Jeremy_Oglesby, I completely forgot about the array methods, as my head was locked on forloops, but yes the map() does have an index function.
Thank you.