Compartilhar via


ExecuteTransactionRequest Class

Definition

Contains the data that is needed to execute one or more message requests in a single database transaction, and optionally return a collection of results.

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

This example method shows how to create two account records in a transaction:

/// <summary>
/// Demonstrates ExecuteTransaction
/// </summary>
/// <param name="service">Authenticated IOrganizationService instance</param>
static void ExecuteTransactionExample(IOrganizationService service)
{

      // A collection of two create requests
      OrganizationRequestCollection requests = new() {
         {
            new CreateRequest(){
                  Target = new Entity("account"){
                     Attributes = {
                           { "name","Test Account One"},
                           // Preferred Customer
                           { "accountcategorycode", new OptionSetValue(1)}

                     }
                  }
            }
         },
         {
            new CreateRequest(){
                  Target = new Entity("account"){
                     Attributes = {
                           { "name","Test Account Two"},
                           // Standard
                           { "accountcategorycode", new OptionSetValue(2)}
                     }
                  }
            }
         }
      };

      // Prepare the request
      ExecuteTransactionRequest request = new() { 
         Requests = requests,
         ReturnResponses = true
      };

      try
      {
         // Send the request
         var response = (ExecuteTransactionResponse)service.Execute(request);

         // Process the successful responses
         response.Responses.ToList().ForEach(x => { 
            var createResponse = (CreateResponse)x;
            Console.WriteLine($"Created account with ID:{createResponse.id}");
         });

      }
      catch (FaultException<OrganizationServiceFault> ex)
      {
         int index = ((ExecuteTransactionFault)ex.Detail).FaultedRequestIndex + 1;
         string message = ex.Detail.Message;

         Console.WriteLine($"Create request failed for the account {index} because: {message}");
      }
}

Output

Created account with ID:8662fc13-fd71-ee11-9ae7-000d3a993550
Created account with ID:8b62fc13-fd71-ee11-9ae7-000d3a993550

If you change the assignment of accountcategorycode in the second entity to use an invalid value (3):

// Invalid value
{ "accountcategorycode", new OptionSetValue(3)}

The output becomes:

Create request failed for the account 2 because: A validation error occurred. 
The value 3 of 'accountcategorycode' on record of type 'account' is outside the valid range. 
Accepted Values: 2,1

Sample code on GitHub

Execute multiple requests in transaction

Remarks

Learn how to execute messages in a single database transaction

Usage

Pass an instance of this class to the Execute(OrganizationRequest) method, which returns an instance of the ExecuteTransactionResponse 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

ExecuteTransactionRequest()

Initializes a new instance of the ExecuteTransactionRequest class.

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 message requests to execute.

ReturnResponses

Gets or sets a value indicating if responses are to be returned for each message request processed.

Applies to