I'm trying to use ForAll() to loop through a Gallery and update an "Approval Status" column based on the value from a Toggle (Toggle1). This is what I currently have:
ForAll( Gallery2.AllItems , If( Toggle1.Value, Patch('[dbo].[LINEITEM]', First(Filter('[dbo].[LINEITEM]',ID=ID)), {APPROVAL_STATUS: "Approved"}), Patch('[dbo].[LINEITEM]', First(Filter('[dbo].[LINEITEM]',ID=ID)), {APPROVAL_STATUS: "Rejected "}) ))
But when I push the button to run this code, the toggles are all switching back to `true`. This is dicated by the following Default Code:
If(ThisItem.APPROVAL_STATUS="Approved", true, false)
It seems that the `ID=ID` piece is what isn't working properly. What am I missing to make this work properly?
Solved! Go to Solution.
Hi @12vanblart,
Do you display the Approval Status column value using Toggle control within your app?
I think there is something wrong with the formula that you provided. I have made a test on my side (use a SP list as the data source of my app), please take a try with the following workaround:
The data structure of SP list as below:
App's configuration as below:
Set the OnSelect property of the "Submit" button to following formula:
ForAll(
RenameColumns(Gallery1.AllItems,"ID","ID1"),
If(
Toggle1.Value=true,
Patch('20181002_case6',LookUp('20181002_case6',ID=ID1),{ApprovalStatus:"Approved"}),
Patch('20181002_case6',LookUp('20181002_case6',ID=ID1),{ApprovalStatus:"Rejected"})
)
)
On your side, you should type the following formula:
ForAll(
RenameColumns(Gallery1.AllItems,"ID","ID1"),
If(
Toggle1.Value=true,
Patch('[dbo].[LINEITEM]',LookUp('[dbo].[LINEITEM]',ID=ID1),{APPROVAL_STATUS:"Approved"}),
Patch('[dbo].[LINEITEM]',LookUp('[dbo].[LINEITEM]',ID=ID1),{APPROVAL_STATUS:"Rejected"})
)
)
Or
ForAll(
RenameColumns(Gallery1.AllItems,"ID","ID1"),
If(
Toggle1.Value=true,
Patch('[dbo].[LINEITEM]',First(Filter('[dbo].[LINEITEM]',ID=ID1)),{APPROVAL_STATUS:"Approved"}),
Patch('[dbo].[LINEITEM]',First(Filter('[dbo].[LINEITEM]',ID=ID1)),{APPROVAL_STATUS:"Rejected"})
)
)
The GIF screenshot as below:
More details about the Patch function and RenameColumns function in PowerApps, please check the following workaround:
Patch function, RenameColumns function
In addition, you could also consider take a try to set the OnChange property of the Toggle control (within the Gallery control) to following formula:
If(
Toggle1.Value=true,
Patch('[dbo].[LINEITEM]',ThisItem,{APPROVAL_STATUS:"Approved"}),
Patch('[dbo].[LINEITEM]',ThisItem,{APPROVAL_STATUS:"Rejected"})
)
Best regards,
Kris
Hi @12vanblart,
If you try ID=ThisItem.ID does it work?
When I try this, It doesn't recognize `ThisItem` as valid.
Error:
Name isn't valid. This identifier isn't recognized. This error appears most commonly when a formula refers to something that no longer exists (for example, a control that you've deleted).
Ok, please try ForAll( Gallery2.AllItems, UpdateIf('[dbo].[LINEITEM]', Toggle1.Value, {APPROVAL_STATUS: "Approved"},!Toggle1.Value,{APPROVAL_STATUS: "Rejected "})). Let me know if that works.
This resolved the error message, but it doesn't seem to be setting correctly. i.e. I have a set of 3 items to loop over and I set the toggles to a status of {False, True, False}. Then after running the code their status is {False, False, False}.
I've run a couple of setups and it looks like it's only checking against the last toggle item.
i.e.)
{False, False, True} => {True, True, True}
{True, True, False} => {False, False, False}
Hi @12vanblart,
Do you display the Approval Status column value using Toggle control within your app?
I think there is something wrong with the formula that you provided. I have made a test on my side (use a SP list as the data source of my app), please take a try with the following workaround:
The data structure of SP list as below:
App's configuration as below:
Set the OnSelect property of the "Submit" button to following formula:
ForAll(
RenameColumns(Gallery1.AllItems,"ID","ID1"),
If(
Toggle1.Value=true,
Patch('20181002_case6',LookUp('20181002_case6',ID=ID1),{ApprovalStatus:"Approved"}),
Patch('20181002_case6',LookUp('20181002_case6',ID=ID1),{ApprovalStatus:"Rejected"})
)
)
On your side, you should type the following formula:
ForAll(
RenameColumns(Gallery1.AllItems,"ID","ID1"),
If(
Toggle1.Value=true,
Patch('[dbo].[LINEITEM]',LookUp('[dbo].[LINEITEM]',ID=ID1),{APPROVAL_STATUS:"Approved"}),
Patch('[dbo].[LINEITEM]',LookUp('[dbo].[LINEITEM]',ID=ID1),{APPROVAL_STATUS:"Rejected"})
)
)
Or
ForAll(
RenameColumns(Gallery1.AllItems,"ID","ID1"),
If(
Toggle1.Value=true,
Patch('[dbo].[LINEITEM]',First(Filter('[dbo].[LINEITEM]',ID=ID1)),{APPROVAL_STATUS:"Approved"}),
Patch('[dbo].[LINEITEM]',First(Filter('[dbo].[LINEITEM]',ID=ID1)),{APPROVAL_STATUS:"Rejected"})
)
)
The GIF screenshot as below:
More details about the Patch function and RenameColumns function in PowerApps, please check the following workaround:
Patch function, RenameColumns function
In addition, you could also consider take a try to set the OnChange property of the Toggle control (within the Gallery control) to following formula:
If(
Toggle1.Value=true,
Patch('[dbo].[LINEITEM]',ThisItem,{APPROVAL_STATUS:"Approved"}),
Patch('[dbo].[LINEITEM]',ThisItem,{APPROVAL_STATUS:"Rejected"})
)
Best regards,
Kris
Thank you so much! This is working as expected now! It looks like the Rename Columns function is what was missing to make it all work!
As for the OnChange property for the toggles themselves, I initially had it setup like this, but if you tried to toggle another item before the Patch was handled, it would undo the items toggled between first item toggled and Patch completing (idk if that sentence makes much sense?)
Thanks again for your assistance!
After working on the app some more, everything is working in the Edit Environment, but when I try to navigate to the page with the toggles in the published version of the App, I'm getting this error:
User | Count |
---|---|
138 | |
132 | |
75 | |
74 | |
72 |
User | Count |
---|---|
206 | |
195 | |
70 | |
59 | |
52 |