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.

@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.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
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
Power Platform Call June 2022 768x460.png

Power Platform Community Call

Join us for the next call on August 17, 2022 at 8am PDT.

Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

365 EduCon 768x460.png

Microsoft 365 EduCon

Join us for two optional days of workshops and a 3-day conference, you can choose from over 130 sessions in multiple tracks and 25 workshops.

Users online (1,483)