Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Apps - Building Power Apps
Answered

if statement not executing the true value

(0) ShareShare
ReportReport
Posted on by

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

 

 

 

 

 

 

  • EP-29071004-0 Profile Picture
    on at
    Re: if statement not executing the true value

    thanks, it worked!!

  • RandyHayes Profile Picture
    76,287 Super User 2024 Season 1 on at
    Re: if statement not executing the true value

    @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.

  • EP-29071004-0 Profile Picture
    on at
    Re: if statement not executing the true value

    @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

     

  • Verified answer
    WarrenBelz Profile Picture
    146,524 Most Valuable Professional on at
    Re: if statement not executing the true value

    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.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Markus Franz – Community Spotlight

We are honored to recognize Markus Franz as our April 2025 Community…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 146,524 Most Valuable Professional

#2
RandyHayes Profile Picture

RandyHayes 76,287 Super User 2024 Season 1

#3
Pstork1 Profile Picture

Pstork1 65,906 Most Valuable Professional

Leaderboard