So I have a recurring issue that I am trying to solve. It isn't difficult to fix; it is just a bit annoying.
What I have is code in the OnVisible section of a loading screen for an offline-capable app. I have the code sectioned in four main groups: initial values that need to be set, local data load, the collection of missing tables if a data connection exists (primarily for the initial setup of the app), and collection/initialization of any other values needed before the app fully launches.
When I edit a code in this area (which happens a lot as I am in the process of building it now), I will occasionally get errors in two areas: the initial load of a local table and the attempt to collect that table if it does not exist. Here is a section of that code with the offending areas highlighted in red:
// Initial setup ... // Load local data // Load data related to color settings UpdateContext({LoadMessage: "Loading local data"}); Concurrent( LoadData(AppColorSchemeCollection, "LocalAppColorScheme", true), LoadData(AppIndicatorColorCollection, "LocalAppIndicatorColor", true), LoadData(ColorSchemeCollection, "LocalColorSchemeCollection", true), // <- Error here LoadData(IndicatorColorCollection, "LocalIndicatorColorCollection", true) ); UpdateContext({LoadProgress: LoadProgress + 4}); ... // If no color settings exist, create them // ColorScheme If( IsEmpty(ColorSchemeCollection), // <- Error here UpdateContext({LoadMessage: "Collecting local color schemes"}); Collect(ColorSchemeCollection, 1); //<- Error here SaveData(ColorSchemeCollection, "LocalColorSchemeCollection") // <- Error here ); Set(ColorScheme, First(ColorSchemeCollection).Value); UpdateContext({LoadProgress: LoadProgress + 1}); // IndicatorColor If( IsEmpty(IndicatorColorCollection), UpdateContext({LoadMessage: "Collecting local color schemes"}); Collect(IndicatorColorCollection, 1); SaveData(IndicatorColorCollection, "LocalIndicatorColorCollection") ); Set(IndicatorColor, First(IndicatorColorCollection).Value); UpdateContext({LoadProgress: LoadProgress + 1}); ... // Code ends
What is happening is that I am initializing and saving a singular value (which references the ID of a table of color options) and putting it in a global variable to avoid using a nested LookUp to reference this value. The user can choose another value when using the app, which is saved locally so that their color choice on that device persists.
All the errors are stemming from the ColorScheme section statement:
Collect(ColorSchemeCollection, 1);
The specific error is "The function Collect has some invalid arguments. Expected a table or record value."
Fair enough, but if I simply comment out the offending sections of code, save, close, reopen, and uncomment, everything works again until I add something new to the code. And when I do comment the code out, it doesn't throw the same error for the identically coded IndicatorColor section.
So, what am I doing to break this? Is there a better way to save a singular value locally so that I can avoid a LookUp situation? Again, not the worst thing in the world but it would be nice to not have to repeat the comment/save/close/reopen/uncomment loop.
@v-micsh-msftThank you for the reply! I am more than happy to explain why I am using Collect as I do.
In a word, persistence. Basically, I have a table of color schemes that the user can select from. These are referenced by an ID (the primary key of the table). In the color and fill sections of the various parts of the app, I have something like this:
ColorValue( LookUp( AppColorSchemeCollection, ID = ColorScheme ).OnSurfaceColor )
which looks up the user selected ID (ColorScheme) from the AppColorSchemeCollection, returning a hex-coded color that the ColorValue function interprets.
Now, as you may have noticed, I actually do use Set() to make a global variable but, unfortunately, SaveData() does not work with Set() so there is no way to persist that variable for the user when they close PowerApps. So, I collect that variable, using SaveData(), and LoadData() to keep it.
While I could have stored the row and just persisted that, it seemed a bit less intensive to just store the variable. Thinking on it now, I probably could store the row and use
ColorValue( First(ColorSchemeRow).OnSurfaceColor )
to do the same thing, which I assume would be a bit better as it is just making one call to look up the info, rather than fetching the global variable and then looking up the info. Then, when initializing the variable (the code from my first post) I would just collect the row as well, rather than collecting and setting the variable.
Would that be the recommended approach? And could you maybe explain a bit more what you meant by "The message reminder should be caused by the function valid checking."? If the function or usage is invalid, why doesn't it give the same error for the second usage? And why is it so easy to clear that error and then have working code? I would love to know more about that process, so anything you can offer would be appreciated!
Thanks again for the reply!
The message reminder should be caused by the function valid checking.
Could you please explain a bit for why writing the collection as?
Collect(ColorSchemeCollection, 1);
Collect() is better to be used with Table or record, for single value, it is recommended to take use of Variables with function Set().
If the colorschemecollection has a defined schema, then the suggested way to set the initial value is as below:
Collect(ColorSchemeCollection, {FieldName:"",.Field2Name:""})
For how to work with collect, see:
Collect, clear, and ClearCollect functions in PowerApps
Regards,
Michael
WarrenBelz
146,631
Most Valuable Professional
RandyHayes
76,287
Super User 2024 Season 1
Pstork1
65,991
Most Valuable Professional