Introducing Transactions with Dynamics CRM 2015

 

We always wanted a transactions capabilities when you are performing business logic in enterprise applications, given the complexity of the application, integration or business rules. Transactions gives full capability to manage application behavior.

In order to implement transactions through windows or web based applications we used to write our own logic to roll back the operations in case of any business logic failure or change in data status.

We can take the example of my project where I wanted to rollback all operations in case of payment failure while buying order through online portal. In case of payment failure, I wrote my own logic to rollback records.

In Dynamics CRM 2015 update 1, as transactions been introduced, I was able to use ExecuteTransactionRequest, this is  helpful to maintain integrity during the context of transactions.

We can execute multiple operations in single database transaction so that either all operations will be executed successfully or none (revert back).

You need to add the reference to Microsoft.Xrm.Sdk dll.. We can provide collection of Organization request as given in below example.  I have added 3 requests to the collection. ExecuteTransactionRequest takes request collections as a parameter

public static void PerformTransactionOperation(OrganizationService service)
{
var request = new ExecuteTransactionRequest()
{
Requests = new OrganizationRequestCollection()
};

var account = new Entity(“account”);
account[“name”] = “Account Test”;
var createRequest = new CreateRequest() { Target = account };
request.Requests.Add(createRequest);

RetrieveRequest retReq = new RetrieveRequest() { Target = new EntityReference(“account”, new Guid(“B6F79317-5E8E-E511-80E7-3863BB2E1390”)) };

retReq.ColumnSet = new ColumnSet(“name”);
request.Requests.Add(retReq);
Entity accToUpdate = new Entity(“account”);

accToUpdate.Id = new Guid(“B6F79317-5E8E-E511-80E7-3863BB2E1390”);
var updateRequest = new UpdateRequest() { Target = accToUpdate };
request.Requests.Add(updateRequest);
try
{
var respTrans = (ExecuteTransactionResponse)service.Execute(request);
foreach (var response in respTrans.Responses)
{
switch (response.ResponseName.ToUpper())
{
case “CREATE”:
var createResponse = (CreateResponse)response;
Console.WriteLine(“Account created: {0}”, createResponse.id);
break;

case “UPDATE”:
var updateResponse = (UpdateResponse)response;
break;

case “DELETE”:
var deleteResponse = (DeleteResponse)response;
break;

case “SETSTATE”:

var setstateResponse = (SetStateResponse)response;

break;

case “RETRIEVE”:
var retResponse = (RetrieveResponse)response;
break;
}}}
catch (FaultException<OrganizationServiceFault> ex)
{
ExecuteTransactionFault fault = (ExecuteTransactionFault)ex.Detail;
}}

It will be executed in the same order as it was added to collection.

In our case we got 3 responses for each request. In my code, I am checking the Response name and accordingly performing each operation.

If you want to know the exact request that was failed, you can check the FaultedRequestIndex as given below. It will tell you the index number of the request that was failed.

Few important points to Note:

  • An ExecuteMultipleRequest may have multiple ExecuteTransactionRequest instances.
  • An ExecuteTransactionRequest instance may not contain a ExecuteMultipleRequest or ExecuteTransactionRequest.
  • It has same limit as ExecuteMultipleRequest. Maximum batch size could be 1000. You can have maximum 1000 Requests in a batch.
  • CRM online can have maximum 2 Concurrent batch request.
  • If you have more than 1000 Requests you have to split the requests in batches.

Hope this Post is helpful. I would love to hear your suggestions.

Happy CRMing!!!!!!!!
Thank you,
Kalim Ansar
Adisys Corporation

Advertisements