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

if statement not executing the true value

Hi community,

i have save button in my app and i want it to save into two separate SPO lists,

NeedToSave and VarStatus are two variables that evaluate whether the user made changes that need to be saved and then save it.

it should save to either one of them or to both based on the varailables evaluated to true.

but only the first part of the formula is working and saving to the VP Weekly Status table, the other part is not saved although the VarStatus variable is true.

when taking the second part of the formula into a separate button (not withon an if statement) it works just fine.

any ideas?

my formula:

 

If(NeedToSave,
    ClearCollect(
        UpdatedItems,
        ItemsGallery.AllItems
    );
    ForAll(UpdatedItems, Patch(
      'VP weekly status',
        LookUp('VP weekly status',ID=UpdatedItems[@ID]),
        {Report_Items:TextInput3.Text},
        {Week:WeekValVP},
        {DataType:{
	        '@odata.type':"#Microsoft.Azure.Connectors.SharePoint.SPListExpandedReference",
            Value: ItemType,
            ID:ItemsGallery.Selected.ID
	}}
)),
  varStatus,
  ForAll( 
    Gallery2_1.AllItems,
    Patch(AccountsWeeklyStatus,ThisRecord,{Status:{ Value:LblUpdatedStatus_1.Text},'Status Description':TxtStatusDesc.Text}))
)

 

 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
WarrenBelz
Super User
Super User

Hi @Perez ,

That is because you have the second Patch as the "else" of the If() statement - try this (note you do not need the odata.type reference any more)

If(
   NeedToSave,
   With(
      {wItems:ItemsGallery.AllItems},
      ForAll(
         wItems As aPatch, 
         Patch(
            'VP weekly status',
            {ID=aPatch.ID},
            {
               Report_Items:TextInput3.Text,
               Week:aPatch.WeekValVP,
               DataType:
               {
                  Value: aPatch.ItemType,
                  ID:ItemsGallery.Selected.ID
               }       
            }
         )
      )
   )
);
If(
   varStatus,
   ForAll( 
      Gallery2_1.AllItems,
      Patch(
         AccountsWeeklyStatus,
         ThisRecord,
         { 
            Status:{Value:LblUpdatedStatus_1.Text},
            'Status Description':TxtStatusDesc.Text
         }
      )
   )
)

 

Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

View solution in original post

4 REPLIES 4
WarrenBelz
Super User
Super User

Hi @Perez ,

That is because you have the second Patch as the "else" of the If() statement - try this (note you do not need the odata.type reference any more)

If(
   NeedToSave,
   With(
      {wItems:ItemsGallery.AllItems},
      ForAll(
         wItems As aPatch, 
         Patch(
            'VP weekly status',
            {ID=aPatch.ID},
            {
               Report_Items:TextInput3.Text,
               Week:aPatch.WeekValVP,
               DataType:
               {
                  Value: aPatch.ItemType,
                  ID:ItemsGallery.Selected.ID
               }       
            }
         )
      )
   )
);
If(
   varStatus,
   ForAll( 
      Gallery2_1.AllItems,
      Patch(
         AccountsWeeklyStatus,
         ThisRecord,
         { 
            Status:{Value:LblUpdatedStatus_1.Text},
            'Status Description':TxtStatusDesc.Text
         }
      )
   )
)

 

Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

View solution in original post

@WarrenBelz , thanks for the quick reply.

i tried your formula, and it gives set of errors.

Perez_0-1614856644282.png

can you please elaborate about the use of the With function and As operator.

as for my original formula in the UI it looks like it is the true value for the second logical test

Perez_1-1614856842617.png

 

RandyHayes
Super User
Super User

@Perez 

Just chiming in here because I noticed in your formula that you were wasting the output of the ForAll.  ForAll is a function that returns a table - it is too overused as a programming For/Next and the results wasted.  This also impacts performance.

 

Consider the following formula for what you need:

If(NeedToSave,
    Patch(
      'VP weekly status'
      ForAll(ItemsGallery.AllItems As _item, 
        LookUp('VP weekly status',ID=UpdatedItems[@ID]),
        {ID: _item.ID,
         Report_Items: _item.TextInput3.Text,
         Week: WeekValVP,
         DataType:{Value: ItemType, ID:ItemsGallery.Selected.ID}
        }
      )
   )
);

If(varStatus,
  Patch(AccountsWeeklyStatus,
      ForAll(Gallery2_1.AllItems As _item,
         {ID: _item.ID,
          Status: {Value:_item.LblUpdatedStatus_1.Text},
          'Status Description': _item.TxtStatusDesc.Text
         }
      )
   )
)

 

 

HOWEVER... @WarrenBelz was spot on, and deserves the solution for this, that you had your second part in the else of the If.  So, this would never work to do both conditions.  It would have only done NeedToSave (if true) or varStatus (if NeedToSave was false and varStatus was true).

 

I hope this is helpful for you.

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

Really want to show your appreciation? Buy Me A Cup Of Coffee!

thanks, it worked!!

Helpful resources

Announcements
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

Welcome Super Users.jpg

Super User Season 2

Congratulations, the new Super User Season 2 for 2021 has started!

Carousel 2021 Release Wave 2 Plan 768x460.jpg

2021 Release Wave 2 Plan

Power Platform release plan for the 2021 release wave 2 describes all new features releasing from October 2021 through March 2022.

Users online (1,583)