cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Digital
Responsive Resident
Responsive Resident

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
poweractivate
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

 

 

Digital
Responsive Resident
Responsive Resident

@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
PA User Group

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group

secondImage

Demo Extravaganza is Back!

We are excited to announce that Demo Extravaganza for 2021 has started!

MBAS on Demand

Microsoft Business Applications Summit sessions

On-demand access to all the great content presented by the product teams and community members! #MSBizAppsSummit #CommunityRocks

Power Apps June 2021

June Power Apps Community Call

Did you miss the call? Check out the recording here!

Top Solution Authors
Top Kudoed Authors
Users online (12,445)