How to trap errors when loading a data source.
I am creating a function to display a collection retrieved from a table by ClearCollect() on the screen or insert it into another table.
Is there any way to detect an error in ClearCollect() or Collect()?
The method of using IsEmpty() or CountRows()=0 cannot determine if the read failed, because the state where the result of ClearCollect is zero is sometimes correct.
It would be very nice if there was a simple way to nest a function into IsError to catch it, for example, as follows
IsError(Patch())
Thank you for your answer.
Solved! Go to Solution.
What is currently happening is that the error from the data source is being inserted into the collection as an error record (I briefly talked about those in this post). When the CountRows function tries to count the rows of the collection and it contains an error record, it will return the error itself, which is why this works for you. You can get a similar result if you try to check for the first element of the collection. We are probably going to change this behavior over the next couple of weeks so that the [Clear]Collect function itself will return an error, so that it will be easier to check the error with an expression such as the following:
If(
IsError(ClearCollect(Collection, DataSourceName)),
Notify("Err"),
Notify("NotErr")
)
So to make sure that your expression will work now and when this change is made, you can use something along the lines of
Set(collectResult, ClearCollect(Collection, DataSourceName));
If(
IsError(collectResult) Or IsError(First(Collection)),
Notify("Err"),
Notify("NotErr"))
Hope this helps!
I was able to catch the error when loading the data source by trying the following:
[1] Publish an app with the following code set to Button.OnSelect
ClearCollect(Collection,DataSourceName);
If(
IsError(CountRows(Collection)),
Notify("Err"),
Notify("NotErr")
)
[2] Delete the SharePoint list that was set in the app[1].
[3] Click the button in app[1] .
It seems that in version 3.21123.25, it is not possible to use IsError to catch errors in the process of retrieving data from the Collect data source, but
However, it seems to be possible to catch the error by using a function such as CountRows on the collection after getting the data.
However, I don't think this is wise because I can't be sure that CountRows(Collection) will always return the correct error.
Do you have any additional information or advice?
Thank you in advance.
What is currently happening is that the error from the data source is being inserted into the collection as an error record (I briefly talked about those in this post). When the CountRows function tries to count the rows of the collection and it contains an error record, it will return the error itself, which is why this works for you. You can get a similar result if you try to check for the first element of the collection. We are probably going to change this behavior over the next couple of weeks so that the [Clear]Collect function itself will return an error, so that it will be easier to check the error with an expression such as the following:
If(
IsError(ClearCollect(Collection, DataSourceName)),
Notify("Err"),
Notify("NotErr")
)
So to make sure that your expression will work now and when this change is made, you can use something along the lines of
Set(collectResult, ClearCollect(Collection, DataSourceName));
If(
IsError(collectResult) Or IsError(First(Collection)),
Notify("Err"),
Notify("NotErr"))
Hope this helps!
CarlosFigueira.
Thanks for the reply.
I'm relieved to hear that IsError(Collect()) will be able to catch errors when retrieving data.
Until then, we will have to deal with
I guess using First rather than CountRows will give better performance.
Thank you for your help.