Kim_Trager1 wrote:
I tried a few things like putting a for-loop inside a for-loop
but now it seems my code never breaks out of the inner for-loop which I’m not sure why.
let luPlanRecords = await prodView.selectRecordsAsync();
console.log(luPlanRecords.records)
for(i = 0; i < luPlanRecords.records.length; i++){
console.log('el1',i)
let newUpd = await prodView.selectRecordsAsync();
for(let record of newUpd.records){
let startDate = record.getCellValue(prodStartDate);
let depDate = record.getCellValue(prevFinishDate)
console.log('el2',record)
if(startDate !== depDate && depDate !== null ){
await prodTable.updateRecordAsync(record,
{'start (vs2)': new Date(depDate)});
}
}
}
but added a break
now it runs through the code as intended but it still does not update my startDate with my depDate except for the first record
let luPlanRecords = await prodView.selectRecordsAsync();
console.log(luPlanRecords.records)
for(i = 0; i < luPlanRecords.records.length; i++){
console.log('el1',i)
let newUpd = await prodView.selectRecordsAsync();
for(let record of newUpd.records){
let startDate = record.getCellValue(prodStartDate);
let depDate = record.getCellValue(prevFinishDate)
console.log('el2',record)
if(startDate !== depDate && depDate !== null ){
await prodTable.updateRecordAsync(record,
{'start (vs2)': new Date(depDate)});
break
}
break
}
}
Anyone who know how best to deal with this problem?
I assume it has to do with the lookup delay.
In general, @Kim_Trager1, it’s a bad idea to make a query inside a loop (I did an internship last year as a software developer and got dinged on code-reviews for that very thing several times before I learned my lesson)-- and your particular structure may be causing issues because you are querying inside a loop based on your previous query – might not be a problem, but it just looks strange to me. I feel like there could be a conflict in the state of the records between the two queries, and perhaps you have an incorrect assumption about what the record
under iteration in the internal loop looks like after the first loop is done :man_shrugging:t2: .
I’d suggest trying to do the work that your Lookup
field is currently doing in your Scripting Block logic instead, if that’s possible (and it should be). I’m still not wrapping my head around exactly what you are trying to do, so I’m just going to make that general suggestion for now. But basically, you’ll want to build an array out of modified records (adding one at a time) that can emulate the structure of your table enough so that you can make the date checks you want for each subsequent record.
It may take significantly more code, but ultimately I think it will be simpler in that it will be easier to follow what is happening in the Script, because there isn’t this black box of the logic that the Lookup
field in your base is doing. When you go back to look at this 6 months from now because something broke, you won’t be scratching your head thinking, “Why am I making another query to the same records here? What was the purpose of doing that inside my loop? What fields was I waiting on?”.
That’s just my humble, and perhaps only slightly informed take on your situation here :slightly_smiling_face: