Relying on SharePoint-generated IDs can be risky, especially if multiple users are adding records concurrently. IDs are assigned by SharePoint independently of the order in which records are patched, and they aren't guaranteed to be sequential if there are other users adding data at the same time.
Instead of relying on SharePoint IDs, you can generate your own unique identifiers within Power Apps before patching the data to SharePoint. Here's how you can do it:
First,
Add a "UniqueId" Text Column in SharePoint List
Before beginning with the Power Apps formula, ensure that you have added a new Text column called "UniqueId" to your SharePoint list.
Then:
Generate Unique Identifiers in Power Apps
Before patching, you can generate unique identifiers for each record in your local collection (colMyCollection).
ClearCollect(
colUniqueIdCollection,
ForAll(
colMyCollection,
{
UniqueId: Text(Now(), "[$-en-US]yyyymmddhhmmss") & Text(Rand(), "[$-en-US]0000"),
Column1: Column1,
Column2: Column2
// Add all other fields you intend to patch
}
)
)
Patch the Entire Collection to SharePoint
With the collection prepared, you can patch it directly to your SharePoint list.
Patch(
SharePointList,
Defaults(SharePointList),
colUniqueIdCollection
)
Use Generated Unique Identifiers for Sorting or Referencing
Since you've patched the UniqueId to SharePoint, you can then sort your gallery by UniqueId to maintain the order of records based on when they were added to your Power Apps collection.
SortByColumns(
Filter(SharePointList, ...),
"UniqueId",
Ascending
)
See if it helps @Laurence_78