Hi everyone,
I'm trying to figure out how to filter the gallery so that the current user only sees its own data entries.
I've read many posts in the forum but none was able to provide me the correct solution.
This is what I did so far:
Set(VarUser; User()) //declared in App
SortByColumns( //in Items in Gallery
Search(
Filter(
[@MonthlyStatement];
"EmployeeName" = VarUser.FullName);
TextSearchBox1.Text;
"crb06_MonthlyStatementID");
"crb06_MonthlyStatementID";
If(SortDescending1; Descending; Ascending)
)
This shows me no results at all even though the entry was successful.
This is the code without the filter and it's working properly:
SortByColumns( //in Items in Gallery
Search(
[@MonthlyStatement];
TextSearchBox1.Text;
"crb06_MonthlyStatementID");
"crb06_MonthlyStatementID";
If(SortDescending1; Descending; Ascending)
)
My table from which I'm retrieving the data looks like this:
MonthlyStatementID | EmployeeName | Project |
0001 | Jack Wick | Data security |
The data is connected via Dataverse.
Any suggestion on what I could try?
Thanks!
Solved! Go to Solution.
The solution that works for me is to create a global variable in the OnStart of the app.
Set(useremail, User().Email) or Set(useremail;User().Email)
Then the Items property of the gallery
Filter(
AddColumns(
Orders, "UsersEmail",'Created By'.'Primary Email'
), UsersEmail = User().Email
)
This takes advantage of the metadata stored in a Dataverse table for the individual who created the record in the Orders table. In your case:
With(
{wMonthlyStatement:Filter(
AddColumns(
[@MonthlyStatement];"UsersEmail";'Created By'.'Primary Email'
); UsersEmail = User().Email
)
};
SortByColumns(
Search(
wMonthlyStatement;
TextSearchBox1.Text;
"crb06_MonthlyStatementID"
);
"crb06_MonthlyStatementID";
If(SortDescending1; Descending; Ascending
)
)
)
Yep,
You need to use
Lookup([@monthlystatement],ID= Browsegallery1.Selected.ID)
Since you changed the datasource in the gallery it is no longer identical to the datasource in the form so you need a more specific item property that matches the datasource.
The solution that works for me is to create a global variable in the OnStart of the app.
Set(useremail, User().Email) or Set(useremail;User().Email)
Then the Items property of the gallery
Filter(
AddColumns(
Orders, "UsersEmail",'Created By'.'Primary Email'
), UsersEmail = User().Email
)
This takes advantage of the metadata stored in a Dataverse table for the individual who created the record in the Orders table. In your case:
With(
{wMonthlyStatement:Filter(
AddColumns(
[@MonthlyStatement];"UsersEmail";'Created By'.'Primary Email'
); UsersEmail = User().Email
)
};
SortByColumns(
Search(
wMonthlyStatement;
TextSearchBox1.Text;
"crb06_MonthlyStatementID"
);
"crb06_MonthlyStatementID";
If(SortDescending1; Descending; Ascending
)
)
)
Thanks for the reply. This worked.
Now I still have two questions though:
- Why did we set the global variable usermail and then didn't use it in the filter but used?
UsersEmail = User().Email
- Now if I want to see the monthly statements that I created, Power Apps displays an error that the formula
BrowseGallery1.Selected
is invalid because a "DataSource" compaitble values was expected. The name "BrowseGallery1" is correct.
Do you know how I can fix this?
Yep,
You need to use
Lookup([@monthlystatement],ID= Browsegallery1.Selected.ID)
Since you changed the datasource in the gallery it is no longer identical to the datasource in the form so you need a more specific item property that matches the datasource.
With regard to the usersEmail I pasted an older version but should have used the variable instead. The app will work much more efficiently if the variable userEmail replaces User().Email.
@Drrickryp this worked perfectly as well.
Nonetheless, if I actually try to use the global variable useremail then it doesn't work because it says that "This formula has side effects and cannot be evaluated" and it has created an internal error evaluating the expression.
Set( UserEmail; User().Email )
If I use the other version, i.e. no global variable and therefore
UsersEmail = User().Email
it works with no error.
Very odd. I wonder if you Set(xxx;User().Email) in the OnStart and in the formula
AddColumns(
[@MonthlyStatement];"UsersEmail";'Created By'.'Primary Email'
); UsersEmail = xxx
whether that would make a difference. Setting a global variable for a single value should reduce the communication between the device and the server every time the formula is used.
@Drrickryp
I did exactly that. I even tried to just create the variable
Set(varUser; User())
and then in the gallery view use
UsersEmail = varUser.Email
but that didn't work either.
For now my app is not very complex, so I assume it's not going to be a big deal but it's still very strange.
User | Count |
---|---|
197 | |
124 | |
86 | |
49 | |
42 |
User | Count |
---|---|
284 | |
160 | |
136 | |
75 | |
72 |