The code I'm using consistently returns the next most recent entry in a SharePoint list as opposed to the most recent one. I built out a whole screen to test this so I could see everything going on. This allowed me to determine definitively that the filter part of the function is what causes this behavior. With the filter included it fails to find the very last qualifying record. Take the filter out and it works perfectly (except I need to have the filter of course).
Sequence is run code > add a record to SP > run code again in a minute or two and see failure.
I'm at a loss... how can a documented function behave so intermittently? How are we supposed to build reliable code blocks when the simpliest little piece of code only sometimes returns the last record?
The only other thing that helps is waiting a LONG time (like 15 minutes). Then it works the first time but executing again even a few minutes later it fails the first time again.
The target list is brand new, containing only plain text, number and date columns. Explicitly setting the ID using LookUp easily allows me to return the correct record, just not when I find it based on the filter condition.
So to clarify, with the filter in place in the example below, I expect to get record ID: 5 but I consistently get ID: 6. If I execute the code again, I then get the correct result (5) which appears to demonstrate that it is not the terms of the filter but that the last record is somehow not in a state that allows it to be returned in the result set the first time the code executes (even making all the records have filter parameters that qualify does not fix further demonstrating this).
Does not work
Events
| ID | EventClass | TaskID | |
| 8 | Task | 983 | I want |
| 7 | Task | 983 | I get |
| 6 | Task | 983 | |
| 5 | Task | 983 | |
//find most recent previous entry matching conditions
Refresh(Events);
Set(varPreviousEvent,
First(
Sort(
Filter(
Events,
EventClass = varCurrentEventClass, TaskID = varTask.ID),
ID, Descending)
)
);
Works
Events
| ID | EventClass | TaskID | |
| 8 | Task | 983 | I want & I get |
| 7 | Task | 983 | |
| 6 | Task | 983 | |
| 5 | Task | 983 | |
Refresh(Events);
Set(varPreviousEvent,
First(
Sort(
//Filter(
Events,
//EventClass = varCurrentEventClass, TaskID = varTask.ID),
ID, Descending)
)
);
Some things that didn't work:
- Substituting actual values instead of variables in filter.
- Collecting the list first, then filtering the collection (collection didn't include the very last result so didn't matter).
- Refreshing before, after, before & after.
- Switching the order of the Sort & Filter statements.
- Running the same block of code twice in succession.
- Using only one filter statement, tried both separately.
Update: I just recreated another SP list and a new app and was EASILY able to recreate this issue... code only works the *second* time you click the button unless you wait several minuts. I need to use this code block on the fly to find and update records and can't have it only work sometimes, when it feels like it. I really don't know what to do... I need this simple function in dozens of places around my app. Is this a bug?