Patch Function in Canvas app

 

1. Create A New Record With Power Apps Patch Function



Syntax
Patch(Datasource, BaseRecord, NewRecord)


Input

Employees Table In SharePoint

IDFullNameEmployeeNumberHireDateActive
1Matthew Devaney105005/28/2010true
2Alice Lemon095811/03/2015true
3David Johnson056308/15/2013false


Code
Patch(
    Employees,
    Defaults(Employees),
    {
        FullName: "Sarah Green",
        EmployeeNumber: 1002,
        HireDate: Date(2018,3,14),
        Active: true
    }
)


Output

Employees Table In SharePoint

IDFullNameEmployeeNumberHireDateActive
1Matthew Devaney105005/28/2010true
2Alice Lemon095811/03/2015true
3David Johnson056308/15/2013false
4Sarah Green100203/14/2018true




2. Update An Existing Record Using Power Apps Patch Function



Syntax
Patch(Datasource, BaseRecord, ChangeRecord)


Input

Employees Table In SharePoint

IDFullNameEmployeeNumberHireDateActive
1Matthew Devaney105005/28/2010true
2Alice Lemon095811/03/2015true
3David Johnson056308/15/2013false
4Sarah Green100203/14/2018true


Code
Patch(
    Employees,
    LookUp(
        Employees,
        ID=4
    ),
    {
        FullName: "Sarah Brown",
        EmployeeNumber: 1003
    }
)


Output

Employees Table In SharePoint

IDFullNameEmployeeNumberHireDateActive
1Matthew Devaney105005/28/2010true
2Alice Lemon095811/03/2015true
3David Johnson056308/15/2013false
4Sarah Brown100303/14/2018true




3. Get The Result Of The Patch Function



Syntax
Set(VariableName, Patch(Datasource, BaseRecord, ChangeRecord))


Input

Employees Table In SharePoint

IDFullNameEmployeeNumberCreatedCreated By
1Matthew Devaney105005/08/2022Matthew Devaney
2Alice Lemon095805/10/2022Matthew Devaney
3David Johnson056305/13/2022Matthew Devaney
4Sarah Green100205/13/2022Matthew Devaney



Code
Set(
    varEmployeeCurrent,
    Patch(
        Employees,
        Default(Employees),
        {
            FullName: "Kelly Smith",
            EmployeeNumber: 1066
        }
    )
)


Output

Employees Table In SharePoint

IDFullNameEmployeeNumberCreatedCreated By
1Matthew Devaney105005/08/2022Matthew Devaney
2Alice Lemon095805/10/2022Matthew Devaney
3David Johnson056305/13/2022Matthew Devaney
4Sarah Green100205/13/2022Matthew Devaney
5Kelly Smith106605/29/2022Matthew Devaney



varEmployeeCurrent record in Power Apps

{
    ID: 5,
    FullName: "Kelly Smith",
    EmployeeNumber: 1066,
    'Created By': Date(2022, 05, 29),
    Created: Matthew Devaney,
    Modified: Date(2022, 05, 29),
    Modified By: Matthew Devaney
}



4. Create Multiple New Records With Power Apps Patch Function



Syntax
Patch(Datasource, BaseRecordsTable, NewRecordsTable)


Input

Employees Table In SharePoint

IDFullNameEmployeeNumberHireDateActive
1Matthew Devaney105005/28/2010true
2Alice Lemon095811/03/2015true
3David Johnson056308/15/2013false
4Sarah Green100203/14/2018true
5Kelly Smith106605/20/2022true




Code
ClearCollect(
    colNewEmployees,
    Table(
        Employees@{
            FullName: "Mary Baker",
            EmployeeNumber: 0798,
            HireDate: Date(2022, 06, 06),
            Active: true
        },
        Employees@{
            FullName: "John Miller",
            EmployeeNumber: 1203,
            HireDate: Date(2022, 06, 11),
            Active: true
        },
        Employees@{
            FullName: "Susan Wright",
            EmployeeNumber: 0590,
            HireDate: Date(2022, 06, 23),
            Active: true
        }
    )
);
Patch(
    Employees,
    ForAll(
        Sequence(CountRows(colNewEmployees)),
        Defaults(Employees)
    ),
    colNewEmployees
);



Output

Employees Table In SharePoint

IDFullNameEmployeeNumberHireDateActive
1Matthew Devaney105005/28/2010true
2Alice Lemon095811/03/2015true
3David Johnson056308/15/2013false
4Sarah Green100203/14/2018true
5Kelly Smith106605/20/2022true
6Mary Baker079806/06/2022true
7John Miller120306/11/2022true
8Susan Wright059006/23/2022true




5. Edit Multiple Existing Records Using Power Apps Patch Function



Syntax
Patch(Datasource, BaseRecordsTable, UpdateRecordsTable)


Input

Employees Table In SharePoint

IDFullNameEmployeeNumberHireDateActive
1Matthew Devaney105005/28/2010true
2Alice Lemon095811/03/2015true
3David Johnson056308/15/2013false
4Sarah Green100203/14/2018true
5Kelly Smith106605/20/2022true


Code
ClearCollect(
    colUpdateEmployees,
    Table(
        Employees@{
            ID: 2,
            FullName: "Alice Henderson",
            EmployeeNumber: 1001
        },
        Employees@{
            ID: 4,
            Active: false
        },
        Employees@{
            ID: 5,
            HireDate: Date(2022, 08, 01)
        }
    )
);
Patch(
    Employees,
    ShowColumns(
        colUpdateEmployees,
        "ID"
    ),
    colUpdateEmployees
);


Output

Employees Table In SharePoint

IDFullNameEmployeeNumberHireDateActive
1Matthew Devaney105005/28/2010true
2Alice Henderson100111/03/2015true
3David Johnson056308/15/2013false
4Sarah Green100203/14/2018false
5Kelly Smith106608/01/2022true




6. Upsert Multiple Records With Power Apps Patch Function



Syntax
Patch(Datasource, BaseRecordsTable, UpsertRecordsTable)


Input

Employees Table In SharePoint

IDFullNameEmployeeNumberHireDateActive
1Matthew Devaney105005/28/2010true
2Alice Lemon095811/03/2015true
3David Johnson056308/15/2013false


Code
ClearCollect(
    colUpsertEmployees,
    Table(
        Employees@{
            ID: 2,
            FullName: "Alice Henderson",
            EmployeeNumber: 1001
        },
        Employees@{ 
            ID: Blank(),
            FullName: "Sarah Green",
            EmployeeNumber: 1002,
            HireDate: Date(2018, 03, 14),
            Active: false
        },
        Employees@{ 
            ID: Blank(),
            FullName: "Kelly Smith",
            EmployeeNumber: 1066,
            HireDate: Date(2022, 05, 20),
            Active: true
        }
    )
);
Patch(
    Employees,
    colUpsertEmployees
);


Output

Employees Table In SharePoint

IDFullNameEmployeeNumberHireDateActive
1Matthew Devaney105005/28/2010true
2Alice Henderson100111/03/2015true
3David Johnson056308/15/2013false
4Sarah Green100203/14/2018false
5Kelly Smith106608/01/2022true


7. Change Values In A Record Variable Using Power Apps Patch Function



Syntax
Patch(Record1, Record2)


Input

Record stored in a global variable named gblEmployee

IDFullNameEmployeeNumberHireDateActive
1Matthew Devaney105005/28/2010true


Code
Patch(
    gblEmployee,
    {EmployeeNumber: 1063}
);


Output

Employees Table In SharePoint

IDFullNameEmployeeNumberHireDateActive
1Matthew Devaney106305/28/2010true




More Power Apps Patch Function Tips And Tricks

Want to learn more about the Patch function? Check out these awesome otherarticles I’ve written:


Everything You Need To Know About Power Apps Patch Forms

A full tutorial on how to build a Power Apps patch form including the topics: form submissions, data validation, error-handling and updating a previously submitted record.

Patch Multiple Records In Power Apps 10X Faster

A nifty trick I discovered to submit multiple records at once really really quickly.

Power Apps Patch Function Examples For Every SharePoint Column Type

Example of how to patch every SharePoint column type in an easy to read format.

Patch Function Error-Handling

Learn how to check a form for errors on submission and eliminate the possibility to losing entered data.

Power Apps Excel-Style Editable Table

Make an excel-style table in Power Apps you users will love by using the Patch function

Comments

Popular posts from this blog

If any case created, then check for the same user how many cases are created with in 30 days, if more than 2 and less than 5 send a mail to team lead, if more than 5 and less than 9 then send a mail to manager using power automate.

Create approve & reject ribbon buttons, once click on approve it should change the status field to approve.If clicked on reject it should change to Reject. Based on status field change trigger work flow to send a email to stating request is approved/Rejected.

How to get and set values in plugins?