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

Patch in If Statement Not Working

I have a Gallery of Employees that pulls from List1. Clicking on an Employee takes you to the ScnEmpDetails Scene while also setting VarEmpNew=false. If you instead click on the BtnNewEmp Button, it will set VarEmpNew=true and go to the same ScnEmpDetails.

 

The fields on ScnEmpDetails include:

 

TxtEmpNameDetails (Text field)

TxtEmpIDDetails (Text field)

DrpEmpLocationDetails (Drop down)

DrpEmpDepartmentDetails (Drop down)

 

and a BtnSubmitDetails Button has the following OnSelect=

 

If(
    VarEmpNew = true,
    Patch(
        Chip_Qty,
        First(
            Filter(
                Chip_Qty,
                ID = VarTargetID
            )
        ),
        {Title: TxtEmpNameDetails.Text},
        {EmployeeID: TxtEmpIDDetails.Text},
        {Location: Value(DrpEmpLocationDetails.SelectedText.Value)},
        {Department: Value(DrpEmpDepartmentDetails.SelectedText.Value)}
    ),
    Patch(
        Chip_Qty,
        Defaults(
            Chip_Qty
        ),
        {Title: TxtEmpNameDetails.Text},
        {EmployeeID: TxtEmpIDDetails.Text},
        {Location: Value(DrpEmpLocationDetails.SelectedText.Value)},
        {Department: Value(DrpEmpDepartmentDetails.SelectedText.Value)}
    )
);   

 

When VarEmpNew=true, it the DrpEmpLocationDetails and DrpEmpDepartmentDetails aren't populating in List1.

When VarEmpNew=false, nothing is populating in List1.

1 ACCEPTED SOLUTION

Accepted Solutions
iAm_ManCat
Super User
Super User

Hi,

 

When you use the Value() function, you are converting something to a number - are both of your dropdowns just numbers?

 

 

If(
    VarEmpNew = true,
    Patch(
        Chip_Qty,
        Defaults(
            Chip_Qty
        ),
        {
           Title: TxtEmpNameDetails.Text,
           EmployeeID: TxtEmpIDDetails.Text,
           Location: DrpEmpLocationDetails.Selected.Value,
           Department: DrpEmpDepartmentDetails.Selected.Value
        }
    ),
    Patch(
        Chip_Qty,
        First(
            Filter(
                Chip_Qty,
                ID = VarTargetID
            )
        ),
        {
           Title: TxtEmpNameDetails.Text,
           EmployeeID: TxtEmpIDDetails.Text,
           Location: DrpEmpLocationDetails.Selected.Value,
           Department: DrpEmpDepartmentDetails.Selected.Value
        }
    )
);   

 

 Also you could have all of your values passed through one record by encompassing them in a single {} instead of multiple as I've done above

 

You may need to modify location and department .Selected.Value to whatever intellisense provides you with after typing Selected.

 

Cheers,  

@iAm_ManCat


Please 'Mark as Solution' if someone's post answered your question and always 'Thumbs Up' the posts you like or that helped you!


Thanks!
You and everyone else in the community make it the awesome and welcoming place it is, keep your questions coming and make sure to 'like' anything that makes you 'Appy
Sancho Harker, MVP


View solution in original post

6 REPLIES 6
iAm_ManCat
Super User
Super User

Hi,

 

When you use the Value() function, you are converting something to a number - are both of your dropdowns just numbers?

 

 

If(
    VarEmpNew = true,
    Patch(
        Chip_Qty,
        Defaults(
            Chip_Qty
        ),
        {
           Title: TxtEmpNameDetails.Text,
           EmployeeID: TxtEmpIDDetails.Text,
           Location: DrpEmpLocationDetails.Selected.Value,
           Department: DrpEmpDepartmentDetails.Selected.Value
        }
    ),
    Patch(
        Chip_Qty,
        First(
            Filter(
                Chip_Qty,
                ID = VarTargetID
            )
        ),
        {
           Title: TxtEmpNameDetails.Text,
           EmployeeID: TxtEmpIDDetails.Text,
           Location: DrpEmpLocationDetails.Selected.Value,
           Department: DrpEmpDepartmentDetails.Selected.Value
        }
    )
);   

 

 Also you could have all of your values passed through one record by encompassing them in a single {} instead of multiple as I've done above

 

You may need to modify location and department .Selected.Value to whatever intellisense provides you with after typing Selected.

 

Cheers,  

@iAm_ManCat


Please 'Mark as Solution' if someone's post answered your question and always 'Thumbs Up' the posts you like or that helped you!


Thanks!
You and everyone else in the community make it the awesome and welcoming place it is, keep your questions coming and make sure to 'like' anything that makes you 'Appy
Sancho Harker, MVP


IPC_ahaas
Helper V
Helper V

Thank you for the clarification on Value(). I've struggled to find consistency in how to refer to the selected values of a drop down when working on this project. I also found that I had the patch scripts in the wrong order. Thank you for your help.

RandyHayes
Super User
Super User

@IPC_ahaas 

Just as a bonus...you can simplify your formula to:

Patch(Chip_Qty,
    {ID: VarTargetID,
     Title: TxtEmpNameDetails.Text,
     EmployeeID: TxtEmpIDDetails.Text,
     Location: DrpEmpLocationDetails.Selected.Value,
     Department: DrpEmpDepartmentDetails.Selected.Value
    } 
)

 

Patch is smart enough to know that if the primary Key (ID in this case) is blank, that it will create the record.  If it has a valid ID, then it will update the record.

Just make sure that VarTargetID is blank for new records and valid for existing.

 

Also, you should avoid using First(Filter as this is a performance hog.  With First(Filter PowerApps will firstly Filter and return ALL records that match your criteria.  THEN it will return the first of all those record.

Using LookUp(Chip_Qty, ID=VarTargetID) will return the exact same results, but PowerApps will only interact with records until the condition is met...much faster.

However, with the above formula, you don't even need to do that lookup.

 

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!
IPC_ahaas
Helper V
Helper V

Very interesting.

 

So just so I know I'm doing this right, I assumed "ID" was an actual primary key for all SharePoint lists. I can't actually see the column when I go to List settings:

 

IPC_ahaas_0-1660833983729.png

 

But I can show the column when viewing the list. Is this a correct assumption?

 

RandyHayes
Super User
Super User

@IPC_ahaas 

Yes, this is correct.  You cannot see it in that definition because you cannot define it or alter it.

You can display it in your list view in SharePoint and you can also display/use it in your app.

 

The key point is that Patch is smart enough to know that if you supply the ID (which you should already have if you are editing and existing record) will update the record.  And if you supply no ID, it will create the record.

_____________________________________________________________________________________
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!
IPC_ahaas
Helper V
Helper V

Awesome, thank you again @RandyHayes 

Helpful resources

Announcements
Ignite 2022

WHAT’S NEXT AT MICROSOFT IGNITE 2022

Explore the latest innovations, learn from product experts and partners, level up your skillset, and create connections from around the world.

Power Apps Africa Challenge 2022

Power Apps Africa Challenge

Your chance to join an engaging competition of Power Platform enthusiasts.

Super User 2 - 2022 Congratulations

Welcome Super Users

The Super User program for 2022 - Season 2 has kicked off!

September Events 2022

Check out all of these events

Attend in person or online, there are incredible conferences and events happening all throughout the month of September.

Users online (3,057)