Compartilhar via


ExecuteMultipleRequest Class

Definition

Contains the data that is needed to execute one or more message requests as a single batch operation, and optionally return a collection of results.

public ref class ExecuteMultipleRequest sealed : Microsoft::Xrm::Sdk::OrganizationRequest
[System.Runtime.Serialization.DataContract(Namespace="http://schemas.microsoft.com/xrm/2011/Contracts")]
public sealed class ExecuteMultipleRequest : Microsoft.Xrm.Sdk.OrganizationRequest
[<System.Runtime.Serialization.DataContract(Namespace="http://schemas.microsoft.com/xrm/2011/Contracts")>]
type ExecuteMultipleRequest = class
    inherit OrganizationRequest
Public NotInheritable Class ExecuteMultipleRequest
Inherits OrganizationRequest
Inheritance
ExecuteMultipleRequest
Attributes

Examples

The following example shows how to use this message. For this sample to work correctly, you must be connected to the server to get an IOrganizationService interface instance.

/// <summary>
/// Demonstrate ExecuteMultiple
/// </summary>
/// <param name="service">Authenticated IOrganizationService instance</param>
static void ExecuteMultipleExample(IOrganizationService service)
{
    // List of records to create
    List<Entity> recordsToCreate = new() {

        {
            new Entity("account"){
            Attributes = {
                    {"name", "test account 1" }
                }

            }
        },
        {
            new Entity("account"){
            Attributes = {
                    {"name", "test account 2" }
                }
            }
        }

    };

    // List of records to update
    List<Entity> recordsToUpdate = new() {

        {
            new Entity("account", new Guid("7a914942-34cb-ed11-b596-0022481d68cd")){
            Attributes = {
                    {"numberofemployees", 1000 }
                }

            }
        },
        {
            new Entity("account", new Guid("7c914942-34cb-ed11-b596-0022481d68cd")){
            Attributes = {
                    {"numberofemployees", 2000 }
                }
            }
        }

    };

    // List of records to delete
    List<EntityReference> recordsToDelete = new() {

        {
            new EntityReference("account", new Guid("6ee65bf7-2c71-ee11-9ae7-000d3a993550"))
        },
        {
            new EntityReference("account", new Guid("7825a9fe-2c71-ee11-9ae7-000d3a993550"))
        }

    };

    //Initialize collection of requests
    OrganizationRequestCollection requests = new();

    recordsToCreate.ForEach(x =>
    {
        requests.Add(new CreateRequest() { Target = x });
    });

    recordsToUpdate.ForEach(x =>
    {
        requests.Add(new UpdateRequest() { Target = x });
    });

    recordsToDelete.ForEach(x =>
    {
        requests.Add(new DeleteRequest() { Target = x });
    });

    // Prepare the request
    ExecuteMultipleRequest request = new()
    {
        Settings = new ExecuteMultipleSettings
        {
            ContinueOnError = true,
            ReturnResponses = true
        },
        Requests = requests
    };

    // Send the request
    var response = (ExecuteMultipleResponse)service.Execute(request);

    // Process the responses
    response.Responses.ToList().ForEach(x =>
    {
        if (x.Fault != null) {

            Console.WriteLine($"Request {x.RequestIndex} failed with error: {x.Fault.Message}");
        }
        else {
            switch (x.Response.ResponseName)
            {
                case "Create":
                    var createResponse = (CreateResponse)x.Response;
                        Console.WriteLine($"Created record with ID:{createResponse.id}");
                    break;
                case "Update":
                        Console.WriteLine("Record updated.");
                    break;
                case "Delete":
                        Console.WriteLine("Record deleted.");
                    break;
            }
        }
    });
}

Because the settings specify that responses should be returned, if all the data in the example is present in the environment for these operations to succeed, the output will look like this:

Created record with ID:4779f97b-2a71-ee11-9ae7-000d3a993550
Created record with ID:4b79f97b-2a71-ee11-9ae7-000d3a993550
Record updated.
Record updated.
Record deleted.
Record deleted.

Because the settings specify that processing should continue when errors occur, if the data for records to update or delete in the example is not present, the output will look like this:

Created record with ID:ba91e8b4-2c71-ee11-9ae7-000d3a993550
Created record with ID:be91e8b4-2c71-ee11-9ae7-000d3a993550
Request 2 failed with error: Entity 'account' With Id = 7a914942-34cb-ed11-b596-0022481d68cd Does Not Exist
Request 3 failed with error: Entity 'account' With Id = 7c914942-34cb-ed11-b596-0022481d68cd Does Not Exist
Request 4 failed with error: Entity 'account' With Id = 6ee65bf7-2c71-ee11-9ae7-000d3a993550 Does Not Exist
Request 5 failed with error: Entity 'account' With Id = 7825a9fe-2c71-ee11-9ae7-000d3a993550 Does Not Exist

Sample code on GitHub

Execute multiple requests

Remarks

Usage

Pass an instance of this class to the Execute(OrganizationRequest) method, which returns an instance of the ExecuteMultipleResponse class.

Privileges and Access Rights

There are no specific privileges required for this request. Refer to the required privileges and access rights of each message request you add to the Requests collection.

Notes for Callers

PowerPlatform.Dataverse.Client.ServiceClient.CallerId property and Xrm.Tooling.Connector.CrmServiceClient.CallerId property is honored for each message request. Learn to impersonate another user using the SDK for .NET

Constructors

ExecuteMultipleRequest()

Initializes a new instance of the ExecuteMultipleRequest.

Properties

ExtensionData

Gets or sets the structure that contains extra data. Optional.

(Inherited from OrganizationRequest)
Item[String]

Gets or sets the indexer for the Parameters collection.

(Inherited from OrganizationRequest)
Parameters

Gets or sets the collection of parameters for the request. Required, but is supplied by derived classes.

(Inherited from OrganizationRequest)
RequestId

Gets or sets the ID of the request. Optional.

(Inherited from OrganizationRequest)
RequestName

Gets or sets the name of the request. Required, but is supplied by derived classes.

(Inherited from OrganizationRequest)
Requests

Gets or sets the collection of requests to execute.

Settings

Gets or sets the settings that define whether execution should continue if an error occurs and if responses for each message request processed are to be returned.

Applies to

See also