Dynamics CRM Tips: Get Latest Target Entity Value in Plugin Side

Please Register to this event for learn more!

If you are Dynamics CRM Developer, you will agree with me. When creating Dynamics CRM Plugin, you will be thinking there so much repetitive code that you need to apply when you want to do customization. Automatically what will you do is open the existing plugin code, and then you doing copy-paste to get all the objects that you need in the IServiceProvider object. Those objects such as IPluginExecutionContextIOrganizationServiceProvider, etc.

Then the most workflow that you will do, is getting the InputParameter[“Target”] to get all the attributes that are changed in the UI. Then combine with that workflow, sure you have a scenario whereby you need to check the combination of InputParameters[“Target”] + Previous data to get the latest value of the object to do something. The flow will be like the below image:

Diagram how the get latest works.

For getting the above result. Here is an idea to achieve that scenario:

using Microsoft.Xrm.Sdk;
using System;
using System.Text;
namespace Demo.Plugins
{
    public class StateManager
    {
        private readonly IServiceProvider _serviceProvider;
        public StateManager(IServiceProvider serviceProvider)
        {
            _serviceProvider = serviceProvider;
        }
        private IPluginExecutionContext PluginExecutionContext =>
            (IPluginExecutionContext)_serviceProvider.GetService(typeof(IPluginExecutionContext));
        public Entity Target => PluginExecutionContext.InputParameters["Target"] as Entity;
        public Entity Initial => PluginExecutionContext.PreEntityImages["EntityImage"];
        public Entity Latest
        {
            get
            {
                var temp = Initial;
                foreach (var targetAttribute in Target.Attributes)
                {
                    temp[targetAttribute.Key] = targetAttribute.Value;
                }
                return temp;
            }
        }
    }
    public class PreUpdateOperation : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            var stateManager = new StateManager(serviceProvider);
            stateManager.Target["new_orderdetail"] = "CHANGE FROM PLUGIN";
            stateManager.Target["new_qty"] = 9999;
            var sb = new StringBuilder();
            sb.AppendLine($"Init: \n{GetFormatValue(stateManager.Initial)}");
            sb.AppendLine($"Target \n{GetFormatValue(stateManager.Target)}");
            sb.AppendLine($"Latest \n{GetFormatValue(stateManager.Latest)}");
            throw new InvalidPluginExecutionException(sb.ToString());
        }
        private string GetFormatValue(Entity obj)
        {
            var orderDetail = obj.GetAttributeValue<string>("new_orderdetail");
            var customOrderRef = obj.GetAttributeValue<EntityReference>("new_orderid");
            var productRef = obj.GetAttributeValue<EntityReference>("new_productid");
            var qty = obj.GetAttributeValue<int>("new_qty");
            var pricePerUnit = obj.GetAttributeValue<Money>("new_price");
            return
                $"Name: {orderDetail}. Order: {customOrderRef?.Name}. Product: {productRef?.Name}. " +
                $"Qty: {qty}. Price: {pricePerUnit?.Value}";
        }
    }
}

In the above code, you will see that we need to rely on 1 object for reading purpose instead of need to do checking like the below code:

var qty = (input["new_qty"] ?? reference["new_qty"]) as int?;

But that code above, will not cover if we want to do updating in Plugin Message below Main Operation (PreValidate and PreOperation). We must manually set the stateManager.Input[“attribute”] to set the value.

Demonstration

Here is the result of the above plugin step:

Latest value retrieved succesfully

Summary

Niam.Xrm.Framework is a Dynamics CRM Plugin framework that I already using for almost 6 years. This framework has lots of functionality to helps us focus on the Business Logic + increase our productivity instead of doing lots of repetitive works. This 29th May 2021 at 3 PM (Malaysia Time), I will speak on Power Community – Power Apps Bootcamp about this framework. You can join to learn more about this framework.

For more info about the event, you can go here: https://events.powercommunity.com/powerapps-developer-bootcamp-2021/.

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.