Tuesday, 13 May 2014

CRM 2013 Custom Action as Validation Gate

In My Previous Post I was talking about Custom Action in Microsoft Dynamics CRM 2013 as Custom Message and also I give example about utilizing Custom Action as Calculation Formula.

Now, I will still using the same Custom Action as Example, but I will add some logic to make it as Validation Gate.

Imagine that you have several application that need connect to your CRM and do the same calculation, then you have to copy paste your custom code to all of the apps and do the validation.

Let’s go to the example with still using same custom action but adding some sauce : Discount

image

Then, I amend my code to give some validation :
public class Action_SimpleCalculation : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            #region must to have

            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

            // Create service with context of current user
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

            //create linq context
            KonicaBaseContext baseContext = new KonicaBaseContext(service);

            //create tracing service
            ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            #endregion

            //To get access to the image of the Quote record
            EntityReference entityRef = context.InputParameters["Target"] as EntityReference;
            
            //To read the input parameter
            Money money1 = context.InputParameters["Money1"] as Money;
            Money money2 = context.InputParameters["Money2"] as Money;

            //Capture Discount Rate
            decimal discount = 0;
            discount = (decimal) context.InputParameters["Discount"];

            //Now, I am talking about Custom Action as Validation Gate
            if(discount >= 100)
            {
                throw new InvalidPluginExecutionException("Hi, are you crazy to give such discount? We will not gain any profit, do you realize that?!");
            }

            else{
                //This is how the calculation works and custom action as a Calculation Formula
                Money sum = new Money(money1.Value + money2.Value);
                Money sumafterDiscount = new Money();
                sumafterDiscount = new Money(sum.Value - ((sum.Value) * (discount / 100)));

                //using this as a response output
                context.OutputParameters["MoneySum"] = sumafterDiscount;
            }
        }
    }

Then, I also add parameter to call this action :
#region custom action execution

        //EARLY BOUND
        public Money ExecuteCustomAction_SimpleCalculation(IOrganizationService service, Money money1Param, Money money2Param, Guid quoteId)
        {
            tfp_Action_SimpleCalculationRequest request = new tfp_Action_SimpleCalculationRequest()
            {
                Money1 = money1Param,
                Money2 = money2Param,
                Target = new EntityReference("quote", quoteId),
                //new parameter
                Discount = 120
            };

            tfp_Action_SimpleCalculationResponse response = service.Execute(request) as tfp_Action_SimpleCalculationResponse;
            //Processing of response
            return response.MoneySum;
        }
        #endregion



Update your Plugin Assembly then see the result.

And yeah, this is the result

image

Now, change the discount again to less than 100.

image

Then Update your plugin.

Here is the result :

image

Hope it helps!
Thank you.

No comments:

Post a Comment

My Name is..