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

If Statement not working correctly inside a Patch statement

I am trying to conditionally change the value of fields in  a record when doing a Patch.

 

Patch(DB, Defaults(DB),

{

Approval_1: If(var1, 1),
Approval_2: If(var2, 1),
Approval_3: If(var3, 1)

})

 

Only one of the var1, var2, var3 variables is true at any one time, and therefore it is to update that fields value to a 1 (true). The field is a SQL bit field that is set to 0 (zero) as a default.

 

Every time I issue the Patch statement, and confirm that only 1 of the variable (var1, var2, var3) are set true, ALL the db fields (Approval_1, Approval_2, Approval_3 end up getting set to a 1 (true). It should only set that one field. 

 

Any ideas? I must be missing something obvious (at least obvious to others, but not me). Been stuck for hours. Thanks for any help!

 

1 ACCEPTED SOLUTION

Accepted Solutions
RandyHayes
Super User III
Super User III

@dBrand 

Sure, so you can go with something like this instead:

With(_item: LookUp(DB, Pkey=PkeyIn)},
    Patch(DB, _item, 
       {
          Approval_1: Coalesce(_item,Approval_1, If(var1, 1, 0)),
          Approval_2: Coalesce(_item.Approval_2, If(var2, 1, 0)),
          Approval_3: Coalesce(_item.Approval_3, If(var3, 1, 0))
       }
    )
)

This will either use the existing value if not null or the new value if null.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up. Solved your problem? - Click on Accept as Solution. Others seeking the same answers will be happy you did.
Check out my PowerApps Videos too!

View solution in original post

7 REPLIES 7
RandyHayes
Super User III
Super User III

@dBrand 

Please consider changing your Formula to include the false scenario :

Patch(DB, Defaults(DB),
   {
     Approval_1: If(var1, 1, 0),
     Approval_2: If(var2, 1, 0),
     Approval_3: If(var3, 1, 0)
   }
)

 

I hope this is helpful for you.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up. Solved your problem? - Click on Accept as Solution. Others seeking the same answers will be happy you did.
Check out my PowerApps Videos too!

Only concern is that I do not want to change the value in the field once it is set to 1 when updating it when one of the other variables get set (using an update Patch statement).

RandyHayes
Super User III
Super User III

@dBrand 

Um...I'm confused...what would you be changing?  Your patch is using Defaults as the record to patch.  This means you are creating a new record.  Since it is a new record, there is nothing that has already been set!

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up. Solved your problem? - Click on Accept as Solution. Others seeking the same answers will be happy you did.
Check out my PowerApps Videos too!

Sorry for not being clear.

I use that statement when first creating that record. A user will then fill in a screen and save it and it updates the record (one of the three fields, depending on which of the variables are set true (onoly one at a time can be set true). So, the first time through, if it sees no record for that particular client (which is another field on that record, I just didn't show it in my example), it creates the record, as per my original example (and your suggestion helped thought). But when the record gets updated using the below example, it ends up setting all three fields to 1. I want it to only update the one field that the variable is set to true (Var1,...). This way I leave the other fields what they were (which could be 1 or 0). 

 

Patch(DB, (DB), {Pkey: PKeyIn},

{

Approval_1: If(var1, 1),
Approval_2: If(var2, 1),
Approval_3: If(var3, 1)

})

 

Hope that's a little clearer. Thanks!

RandyHayes
Super User III
Super User III

@dBrand 

Sure, so you can go with something like this instead:

With(_item: LookUp(DB, Pkey=PkeyIn)},
    Patch(DB, _item, 
       {
          Approval_1: Coalesce(_item,Approval_1, If(var1, 1, 0)),
          Approval_2: Coalesce(_item.Approval_2, If(var2, 1, 0)),
          Approval_3: Coalesce(_item.Approval_3, If(var3, 1, 0))
       }
    )
)

This will either use the existing value if not null or the new value if null.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up. Solved your problem? - Click on Accept as Solution. Others seeking the same answers will be happy you did.
Check out my PowerApps Videos too!

View solution in original post

The only issue I face is that the field has a default of 0 (zero), not null, as I allow that field to change between 0 and 1 at certain points of the program. Any way to do this with 0 instead of null? I think the coalesce only works with null.

While this didn't exactly fix my situation, it is a solution that would work if I used Nulls, so I am marking this as a solution, as it may help others. Thanks for the solution!

 

I ended up using the first solution from @RandyHayes for my initial record creation. But for the update of the record (since I did not have Nulls in my fields), I went with below:

 

Switch(true,

   Var1, Patch(DB, {Pkey: PkeyIn}, {Approval_1: 1}),
   Var2, Patch(DB, {Pkey: PkeyIn}, {Approval_2: 1}),
   Var3, Patch(DB, {Pkey: PkeyIn}, {Approval_3: 1})

)

 

Still don't know why the way I wrote it intially would not work right, but that's how it goes. Thanks again @RandyHayes .

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

MBAS Attendee Badge

Claim Your Badge & Digital Swag!

Check out how to claim yours today!

secondImage

Are Your Ready?

Test your skills now with the Cloud Skill Challenge.

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

Top Solution Authors
Top Kudoed Authors
Users online (71,051)