Introduction:

ExecuteMultipleRequest – Message that allows you to club multiple CRM operations within a single transaction. You can add multiple messages to the collection and this will ensure all of the operations are executed within a transaction such that a single operation of them fails it would roll back all the other as well.

Plugin & Workflow Execution: Plugin and workflow assemblies are also executed within a transaction now. Any error within the plugin would roll back the original operation as well as any related operations you may have performed.

You may also like to see : Look Beyond “CRM” with QuickBooks Integration.

What happens if you use Execute Multiple within Plugin or Workflow assembly?

In the example below, we are performing multiple CRUD operations on records and each of these operations have been clubbed into the organization request collection and passed to the ExecuteMultiple request.

IPluginExecutionContext context = localContext.PluginExecutionContext;

            Entity preImageEntity = (context.PreEntityImages != null && context.PreEntityImages.Contains(this.preImageAlias)) ? context.PreEntityImages[this.preImageAlias] : null;
            Entity postImageEntity = (context.PostEntityImages != null && context.PostEntityImages.Contains(this.postImageAlias)) ? context.PostEntityImages[this.postImageAlias] : null;

            // TODO: Implement your custom Plug-in business logic.
            IOrganizationService _service = localContext.OrganizationService;
            try
            {

                ExecuteMultipleRequest executeMultipe = new ExecuteMultipleRequest()
                {
                    // Assign settings that define execution behavior: continue on error, return responses.
                    Settings = new ExecuteMultipleSettings()
                    {
                        ContinueOnError = true,
                        ReturnResponses = true
                    },
                    // Create an empty organization request collection.
                    Requests = new OrganizationRequestCollection()
                };

                
                Entity cnt = new Entity("contact");

                cnt["firstname"] = "dev";
                cnt["lastname"] = "smith";

                CreateRequest createReq1 = new CreateRequest() { Target = cnt };

                executeMultipe.Requests.Add(createReq1);

                Entity cnt1 = new Entity("contact");

                cnt1["firstname"] = "viky";
                cnt1["lastname"] = "border";

                CreateRequest createReq2 = new CreateRequest() { Target = cnt1 };

                executeMultipe.Requests.Add(createReq2);               

                

                Entity cnt2 = new Entity("contact");
                cnt2.Id = new Guid("2F7C6094-E86B-E511-80E0-3863BB3C0130");

                cnt2["firstname"] = "allen";

                UpdateRequest createReq3 = new UpdateRequest() { Target = cnt2 };

                executeMultipe.Requests.Add(createReq3);
                


                EntityReference cnt3 = new EntityReference() { LogicalName = "contact", Id = new Guid("2F7C6094-E86B-E511-80E0-3863BB3C0130") };

                DeleteRequest createRez = new DeleteRequest() { Target = cnt3 };

                ExecuteMultipleResponse responseWithResults = (ExecuteMultipleResponse)_service.Execute(executeMultipe);

                throw new InvalidPluginExecutionException("Manual exception");

Result:

Since the CRUD operations were part of the ExecuteMultiple request, these are actually committed to the database, even though there was an explicit exception thrown from the Plugin.

Conclusion:

When ExecuteMultiple is included within a Plugin or Workflow assembly, it is being executed within its own separate transaction that is different from the Plugin or workflow transaction and therefore any error within the code does not rollback the operations performed through the Execute Multiple request.

There's much more, for more Dynamics CRM Tips and Tricks head on to Inogic Blog. If you have any questions on Dynamics CRM or need any help in implementation, customization, upgradation of Dynamics CRM or are looking to get more out of your CRM, feel free to reach us at crm@inogic.com today!

The post ExecuteMultiple & Workflow or Plugin Assemblies appeared first on Inogic Blog.