PowerApps Canvas Patch Upsert (Insert or Update) Records in Dynamics 365

In PowerApps Canvas apps, you can use the `Patch` function to upsert (insert or update) records in Dynamics 365. The `Patch` function updates an existing record if it exists or creates a new record if it doesn’t exist. Here’s how you can use the `Patch` function to upsert records in Dynamics 365:

Patch(
DynamicsEntityName, // Name of the Dynamics 365 entity
Defaults(DynamicsEntityName), // Create a new record if it doesn't exist
{
// Define the fields to update or create
Field1: Value1,
Field2: Value2,
// Add more fields as needed
}
)

Replace `DynamicsEntityName` with the name of the entity in Dynamics 365 that you want to update or create. Replace `Field1`, `Field2`, etc. with the field names of the entity, and `Value1`, `Value2`, etc. with the corresponding values you want to set.

Here’s a breakdown of the parameters:

1. `DynamicsEntityName`: The name of the Dynamics 365 entity you want to update or create.
2. `Defaults(DynamicsEntityName)`: Creates a new record with default values if a matching record doesn’t exist.
3. `{}`: The record to update or create. Specify the field names and their corresponding values.

For example, if you have a Dynamics 365 entity named “Account” with fields “Name” and “Address”, you can use the `Patch` function like this:

Patch(
Accounts,
Defaults(Accounts),
{
Name: "New Account Name",
Address: "123 Main St"
}
)

This will upsert a record in the “Accounts” entity with the specified name and address. If a record with the same name already exists, it will be updated with the new address. If no matching record exists, a new record will be created with the specified name and address.

Make sure to replace “Accounts” with the actual name of your Dynamics 365 entity. Additionally, ensure that the field names and values are accurate and match the schema of your Dynamics 365 entity.