Skip to content

Commit b889c76

Browse files
committed
integrate upsertmultiple
1 parent a0dfc28 commit b889c76

File tree

4 files changed

+153
-128
lines changed

4 files changed

+153
-128
lines changed

.openpublishing.redirection.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
{
22
"redirections": [
3+
{
4+
"source_path": "powerapps-docs/developer/data-platform/upsertmultiple.md",
5+
"redirect_url": "bulk-operations#upsertmultiple",
6+
"redirect_document_id": "false"
7+
},
38
{
49
"source_path": "powerapps-docs/developer/data-platform/org-service/extend-code-generation-tool.md",
510
"redirect_url": "/dynamics365/customerengagement/on-premises/developer/org-service/extend-code-generation-tool",

powerapps-docs/developer/data-platform/TOC.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@
4949
- name: Bulk Operation messages
5050
href: bulk-operations.md
5151
items:
52-
- name: Use UpsertMultiple (preview)
53-
href: upsertmultiple.md
5452
- name: Use DeleteMultiple (preview)
5553
href: deletemultiple.md
5654
- name: Create your own messages

powerapps-docs/developer/data-platform/bulk-operations.md

Lines changed: 148 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Use bulk operation messages
33
description: Learn how to use special APIs to perform operations on multiple rows of data in a Microsoft Dataverse table.
4-
ms.date: 11/13/2023
4+
ms.date: 02/08/2024
55
author: divkamath
66
ms.author: dikamath
77
ms.reviewer: jdaly
@@ -20,7 +20,7 @@ To get the best performance when you run operations on multiple rows of a Micros
2020

2121
- [`CreateMultiple`](#createmultiple): Creates multiple records of the same type in a single request.
2222
- [`UpdateMultiple`](#updatemultiple): Updates multiple records of the same type in a single request.
23-
- [`UpsertMultiple` (preview)](upsertmultiple.md): Creates or updates multiple records of the same type in a single request.
23+
- [`UpsertMultiple`](upsertmultiple.md): Creates or updates multiple records of the same type in a single request.
2424
- [`DeleteMultiple` (preview)](deletemultiple.md): For elastic tables only. Deletes multiple records of the same type in a single request.
2525

2626
> [!NOTE]
@@ -137,6 +137,7 @@ Updates multiple records of the same type in a single request.
137137

138138
Just like when you update individual records, the data you send with `UpdateMultiple` must contain only the values you're changing. Learn how to [update records with SDK for .NET](org-service/entity-operations-update-delete.md) and [update records with the Web API](webapi/update-delete-entities-using-web-api.md#basic-update).
139139

140+
140141
##### [SDK for .NET](#tab/sdk)
141142

142143
Uses the [UpdateMultipleRequest class](xref:Microsoft.Xrm.Sdk.Messages.UpdateMultipleRequest).
@@ -221,6 +222,151 @@ OData-Version: 4.0
221222

222223
Multiple records with the same primary key or alternate key values in the payload are not supported with `UpdateMultiple`. When more than one record in the `Targets` parameter is uniquely identified by a primary or alternate key, the operation is performed on the first record only. Any subsequent records with the same key value(s) in the payload are ignored.
223224

225+
### UpsertMultiple
226+
227+
Use `Upsert` to integrate data with external sources when you don't know whether the table exists in Dataverse or not.`Upsert` operations frequently depend on alternate keys to identify records. Use `UpsertMultiple` to perform `Upsert` operations in bulk.
228+
229+
##### [SDK for .NET](#tab/sdk)
230+
231+
Uses the [UpsertMultipleRequest class](xref:Microsoft.Xrm.Sdk.Messages.UpsertMultipleRequest).
232+
233+
This static `UpsertMultipleExample` method depends on a `samples_bankaccount`table that has a string column named
234+
`samples_accountname` configured as an alternate key. It also has a string column named `samples_description`. This code uses the [Entity constructor that sets the keyName and keyValue](use-alternate-key-reference-record.md#using-the-entity-class) to specify the alternate key value.
235+
236+
```csharp
237+
/// <summary>
238+
/// Demonstrates using UpsertMultiple with alternate key values
239+
/// </summary>
240+
/// <param name="service">The authenticated IOrganizationService instance</param>
241+
static void UpsertMultipleExample(IOrganizationService service)
242+
{
243+
var tableLogicalName = "samples_bankaccount";
244+
// samples_accountname string column is configued as an alternate key
245+
// for the samples_bankaccount table
246+
var altKeyColumnLogicalName = "samples_accountname";
247+
248+
// Create one record to update with upsert
249+
service.Create(new Entity(tableLogicalName)
250+
{
251+
Attributes =
252+
{
253+
{altKeyColumnLogicalName, "Record For Update"},
254+
{"samples_description","A record to update using Upsert" }
255+
}
256+
});
257+
258+
// Using the Entity constructor to specify alternate key
259+
Entity toUpdate = new(
260+
entityName: tableLogicalName,
261+
keyName: altKeyColumnLogicalName,
262+
// Same alternate key value as created record.
263+
keyValue: "Record For Update");
264+
toUpdate["samples_description"] = "Updated using Upsert";
265+
266+
Entity toCreate = new(
267+
entityName: tableLogicalName,
268+
keyName: altKeyColumnLogicalName,
269+
keyValue: "Record For Create");
270+
toCreate["samples_description"] = "A record to create using Upsert";
271+
272+
// Add the records to a collection
273+
EntityCollection records = new()
274+
{
275+
EntityName = tableLogicalName,
276+
Entities = { toUpdate, toCreate }
277+
};
278+
279+
// Send the request
280+
UpsertMultipleRequest request = new()
281+
{
282+
Targets = records
283+
};
284+
285+
var response = (UpsertMultipleResponse)service.Execute(request);
286+
287+
// Process the responses:
288+
foreach (UpsertResponse item in response.Results)
289+
{
290+
Console.WriteLine($"Record {(item.RecordCreated ? "Created" : "Updated")}");
291+
}
292+
}
293+
```
294+
295+
**Output:**
296+
297+
```
298+
Record Updated
299+
Record Created
300+
```
301+
302+
#### SDK examples
303+
304+
Within [Sample: SDK for .NET Use bulk operations](org-service/samples/create-update-multiple.md), look for the [UpsertMultiple project](https://github.com/microsoft/PowerApps-Samples/blob/master/dataverse/orgsvc/C%23-NETCore/BulkOperations/UpsertMultiple/README.md)
305+
306+
##### [Web API](#tab/webapi)
307+
308+
Uses the [UpsertMultiple action](xref:Microsoft.Dynamics.CRM.UpsertMultiple).
309+
310+
> [!IMPORTANT]
311+
>
312+
> - You must set the `@odata.type` property for each item in the `Targets` parameter.
313+
> - The `UpsertMultiple` action returns `204 NoContent`. The `UpsertMultipleResponse` complex type is not returned.
314+
315+
The following example shows using the `UpsertMultiple` action with a standard table named `sample_example`.
316+
These requests identify the records using an alternate key defined using a column named `sample_keyattribute`.
317+
The `@odata.id` annotation identifies the record with a relative URL as described in [Use an alternate key to reference a record](use-alternate-key-reference-record.md)
318+
319+
**Request**
320+
321+
```http
322+
POST [Organization Uri]/api/data/v9.2/sample_examples/Microsoft.Dynamics.CRM.UpsertMultiple
323+
OData-MaxVersion: 4.0
324+
OData-Version: 4.0
325+
If-None-Match: null
326+
Accept: application/json
327+
Authorization: Bearer <access token>
328+
Content-Type: application/json; charset=utf-8
329+
Content-Length: 850
330+
331+
{
332+
"Targets": [
333+
{
334+
"@odata.type": "Microsoft.Dynamics.CRM.sample_example",
335+
"sample_name": "sample record 0000001",
336+
"@odata.id": "sample_examples(sample_keyattribute='0000001')"
337+
},
338+
{
339+
"@odata.type": "Microsoft.Dynamics.CRM.sample_example",
340+
"sample_name": "sample record 0000002",
341+
"@odata.id": "sample_examples(sample_keyattribute='0000002')"
342+
},
343+
{
344+
"@odata.type": "Microsoft.Dynamics.CRM.sample_example",
345+
"sample_name": "sample record 0000003",
346+
"@odata.id": "sample_examples(sample_keyattribute='0000003')"
347+
},
348+
{
349+
"@odata.type": "Microsoft.Dynamics.CRM.sample_example",
350+
"sample_name": "sample record 0000004",
351+
"@odata.id": "sample_examples(sample_keyattribute='0000004')"
352+
353+
}
354+
]
355+
}
356+
```
357+
358+
**Response**
359+
360+
```http
361+
HTTP/1.1 204 NoContent
362+
OData-Version: 4.0
363+
```
364+
---
365+
366+
#### Availability
367+
368+
`UpsertMultiple` is available for tables that support `CreateMultiple` and `UpdateMultiple`. This includes all elastic tables. The queries found in [Availability with standard tables](bulk-operations.md#availability-with-standard-tables) will not return results for `UpsertMultiple`, but you can use them to detect whether a table supports both `CreateMultiple` and `UpdateMultiple`.
369+
224370

225371
## Standard and elastic table usage
226372

@@ -293,8 +439,6 @@ When you use the Web API to perform a bulk operation on an elastic table, you ne
293439
Bulk operation message availability depends on whether you're using standard tables or elastic tables. All elastic tables support the `CreateMultiple`, `UpdateMultiple`, `UpsertMultiple`, and `DeleteMultiple` messages.
294440

295441
See also:
296-
297-
- [UpsertMultiple Availability](upsertmultiple.md#availability)
298442
- [DeleteMultiple Availability](deletemultiple.md#availability)
299443

300444
#### Availability with standard tables

powerapps-docs/developer/data-platform/upsertmultiple.md

Lines changed: 0 additions & 122 deletions
This file was deleted.

0 commit comments

Comments
 (0)