Is it possible to conditionally supply datasource to a patch statement? I have tried by inserting a switch and also using With but Patch returns an error.
I managed to do this with Remove so not sure why it isn't working with Path. I think this would be useful rather than needing to write out multiple patch statements in an IF statement.
I even tried putting the record to Patch inside a With and hoping to Patch the ThisRecord value as a way of not repeating the same record to be patched over and over, but that didn't work either.
Patch(
Switch(
dcLocation.Selected.Value,
"Loc1",List1,
"Loc2",List2,
"Loc3",List3,
"Loc4",List4,
List5,
),
gblSelectedRecord,
{Column1:"new value"}
)
This does not display an error...
Remove(
Switch(
dcLocation.Selected.Value,
"Loc1",List1,
"Loc2",List2,
"Loc3",List3,
"Loc4",List4,
List5,
),
{ID:gblSelectedRecord.ID}
)
We can reproduce what you are facing.
Power Apps is confused by this manner of expression because the Switch is being implicitly presumed as a Collection. It really hates the Switch to be specified inline as the first parameter of Patch - even if it is abstracted into a Variable. Perhaps it must be abstracted to a Collection explicitly instead in order not to get an error.
So instead try something like this:
Set(_SwitchVar,Switch(
Dropdown1.Selected.Value
,
"1",JSONLIST
,
"2",JSONLIST2
)
);
ClearCollect(_ovTmp,_SwitchVar);
Patch(
_ovTmp
,
Defaults(_SwitchVar ),
{Title:Text(Dropdown1.Selected.Value)}
)
----------------Below is for additional info:
This answer presumes the following simple test case only, but is probably applicable to your scenario too:
(presumes this is on Screen1 OnVisible)
Set(_SwitchVar,JSONLIST);
ClearCollect(_ovTmp,_SwitchVar);
(also presumes DropDownSample a single column table with "1" "2" and "3" as items).
(also presumes one Dropdown called DropDown1 and one button called Button3. Presumes the code at very top of this post is placed in OnChange of Button3).
It was tested against SharePoint List. However the above was only tested for not giving an error. It is up to you to check on the rest if it really works.
Evidence it does not cause any error is shown below:
Thanks I'll give that a try. I tested the Remove with the switch and although there was no warning in the editor I got a runtime error so that doesn't work.
No drama, I was just trying to avoid a large block of repeating code. Thanks again.
User | Count |
---|---|
176 | |
116 | |
85 | |
44 | |
41 |
User | Count |
---|---|
239 | |
153 | |
131 | |
77 | |
72 |