cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Helper V
Helper V

Using a conditional data source in Patch based on dropdown selection

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}
)

 

2 REPLIES 2
Super User II
Super User II

@Digital 

 

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:

vm78934_2.png

 

 

Helper V
Helper V

@poweractivate ,

 

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. 

Helpful resources

Announcements
New Badges

New Solution Badges!

Check out our new profile badges recognizing authored solutions!

New Power Super Users

Congratulations!

We are excited to announce the Power Apps Super Users!

Power Apps Community Call

Power Apps Community Call: February

Did you miss the call? Check out the Power Apps Community Call here.

Microsoft Ignite

Microsoft Ignite

Join digitally, March 2–4, 2021 to explore new tech that's ready to implement. Experience the keynote in mixed reality through AltspaceVR!

Top Solution Authors
Top Kudoed Authors
Users online (54,855)