
OnStart, I currently have a few collections being created. Then, later in the PowerApps, I have additional items being added to the SharePoint lists that the collections come from, so I need to refresh the data and recreate the collections. Is there a way I can put all of my refreshes and clearcollects into a global variable or something to reference? I currently have the same exact code copied each time something is added to the lists. Thank you!
Hi @jmaur769 ,
Create a Context Variable with a Function: Use the Set function to create a global variable that holds a function to refresh your data sources and recreate collections.
Set(
RefreshAndCollect,
{
Run: function() {
Refresh(DataSource1);
ClearCollect(Collection1, DataSource1);
Refresh(DataSource2);
ClearCollect(Collection2, DataSource2);
// ... Add additional data sources and collections as needed.
}
}
);
​
Invoke the Function: Whenever you need to refresh the data and recreate the collections, you call the Run method of your global variable.
RefreshAndCollect.Run();
OnStart: You can invoke this in the OnStart of the app as well:
If(
// Check if the function exists and then call it
Not(IsEmpty(RefreshAndCollect)),
RefreshAndCollect.Run()
);
After Adding Items: Use the same call after items are added to your SharePoint lists to refresh and collect the data again.
Please note that while Power Apps does not support the creation of reusable functions in the same way traditional programming languages do, this pattern can emulate that functionality to some extent by encapsulating repetitive logic into a single context variable.
Best Regards,
Hassan Raza