Sunday, 16 August 2015

ExecuteTransactionRequest in CRM 2015 Update 1

We got one more wonderful and powerful feature from CRM 2015 Update 1. i.e ExecuteTransactionRequest
 
Normally, when we do some web application with SQL or some database as a backend we always make use of powerful feature of database transaction. So that we can get the choice of rollback in case an operation fails.

CRM 2015 Update 1 introduced a new feature ExecuteTransactionRequest.
It takes input as a collection of Organization request like Create, Update, Delete, SetState, Assign, any organization request and have CRM execute them in a transaction. And the response of this request also a collection of responses corresponding to every request.

We can read the response for each of the individual request considering the matching index.
 
OrganizationRequestCollection orgRequestCol = new OrganizationRequestCollection();
Entity entContact = null;
var createRequest = null;
for (int intCount= 0; intCount < 3; intCount++)
{
    entContact = new Entity("contact");
    entContact["name"] = string.Format("Test Contact {0}", intCount);
    createRequest = new CreateRequest() { Target = entContact };
    orgRequestCol.Add(createRequest);
}
ExecuteTransactionRequest execTrans = new ExecuteTransactionRequest()
{
   ReturnResponses = true,
   Requests = orgRequestCol
};
ExecuteTransactionResponse response = (ExecuteTransactionResponse)iService.Execute(execTrans);
 
Note :
  • The maximum allowed batch size is 1000.
  • You put a batch of multiple individual requests into one ExecuteTransaction request, which contain a collection of CRM records that you want CRM server to process as a batch
  • If all records have been successfully processed, the whole batch is committed; otherwise if any of the record fails, the whole batch is rolled back (all or none)
Hope this helps.

--
Happy CRM'ing
Gopinath

No comments:

Post a Comment