Dataverse: Testing Database Transactions On Custom API

After I examined the potentials of Custom API to redefine how we design the system, I was curious if Custom API will also apply database transactions following the plugin event execution pipeline (Pre Operation – Post Operation will be in the transaction. Meaning if during the process got some errors, it will able to rollback). For example, if we call the Custom API to do some creation data in Dataverse, then in the next step got some error. Will it be rollback or not?

Just a meme will not hurt you right?

For my example, this is the code for the custom API that I created:

using Microsoft.Xrm.Sdk;
using Niam.XRM.Framework;
using Niam.XRM.Framework.Interfaces.Plugin;
using Niam.XRM.Framework.Plugin;

namespace Insurgo.Custom.Api
{
    public class PluginCustomApiDemo : PluginBase, IPlugin
    {
        public PluginCustomApiDemo(string unsecure, string secure) : base(unsecure, secure)
        {
        }

        protected override void ExecuteCrmPlugin(IPluginContext<Entity> context)
        {
            var param = context.PluginExecutionContext.InputParameters["name"].ToString();
            var document = new Entity("new_document");
            document.Set("new_documentnumber", "Create from Custom API: " + param);

            context.Service.Create(document);
        }
    }
}

Below picture is the definition of the Custom API (Custom API Manager by David Rivard):

Custom API Definition

To prove the Custom API working, here is the result if I call it from Custom API Tester by Jonas Rapp:

Testing the Custom API

Here is the code for the plugin that I create for this demo purpose:

using Microsoft.Xrm.Sdk;
using System;

namespace Demo.Plugin
{
    [System.Runtime.Serialization.DataContractAttribute(Namespace = "http://schemas.microsoft.com/xrm/2011/new/")]
    [Microsoft.Xrm.Sdk.Client.RequestProxyAttribute("ins_testapi")]
    public partial class Ins_TestApiRequest : Microsoft.Xrm.Sdk.OrganizationRequest
    {

        public string Name
        {
            get
            {
                if (this.Parameters.Contains("name"))
                {
                    return ((string)(this.Parameters["name"]));
                }
                else
                {
                    return default(string);
                }
            }
            set
            {
                this.Parameters["name"] = value;
            }
        }

        public Ins_TestApiRequest()
        {
            this.RequestName = "ins_testapi";
            this.Name = default(string);
        }
    }

    [System.Runtime.Serialization.DataContractAttribute(Namespace = "http://schemas.microsoft.com/xrm/2011/new/")]
    [Microsoft.Xrm.Sdk.Client.ResponseProxyAttribute("ins_testapi")]
    public partial class ins_testapiResponse : Microsoft.Xrm.Sdk.OrganizationResponse
    {

        public ins_testapiResponse()
        {
        }
    }


    public class PreTestOperation : PluginBase
    {

        public PreTestOperation()
            : base(typeof(PreTestOperation))
        {
        }

        protected override void ExecuteCdsPlugin(ILocalPluginContext localPluginContext)
        {
            if (localPluginContext == null)
            {
                throw new ArgumentNullException(nameof(localPluginContext));
            }

            var action = new Ins_TestApiRequest
            {
                Name = "PreTestOperation"
            };
            localPluginContext.CurrentUserService.Execute(action);


            throw new InvalidPluginExecutionException("testing rollback");
        }
    }
}

As you can see, the plugin is very simple. We call the Custom API that I already prepare (ins_testapi) (the definition of the response and request API classes was generated by Early Bound Generator by Darryl LaBar). Then to simulate error, I throw an error and inspect the result on the screen:

No new data created if got error

Summary

So the summary of the testing above is to proving will the Custom API will support rollback if we use it in the Plugin. And the answer for this testing is YES IT CAN (within The Dataverse tables)!

One thought on “Dataverse: Testing Database Transactions On Custom API

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.