create total amt, total discount, detailed amt (all are currency data type ) in your custom entity, write a plugin to calculate detailed amt = total amt - total discount. With preimage

Create total amt, total discount, detailed amt (all are currency data type ) in your custom entity, write a plugin to calculate detailed amt = total amt - total discount. With preimage

Step 1: First Open visual studio and create a Class library. 

https://www.codementor.io/@dewetvanthomas/tutorial-class-library-dll-for-c-129spithmr

Step 2: Get the Manage NuGet packages. For Manage NuGet packages Right click on the solution and we will find some options from those options we can able to see NuGet packages.

Installation and How to use NuGet packages : https://www.syncfusion.com/blogs/post/how-to-use-nuget-packages.aspx

Step 3: Click on Manage NuGet packages and we can see the tabs go to Browser tab and search for Xrm.Sdk , Select the Microsoft.CrmSdk.CoreAssemble and install it. Then we will get the System Library i.e, using Microsoft.Xrm.Sdk; 

Step 4: For Signing - Double click on properties then project page will open. In left side we have options in that Signing option will be available click on it and go for Sing the assembly. Select Sing the assembly CheckBox a drop down will be appear now click on new option .When you click on new option an popup will be displayed i.e, create strong key  in that uncheck the protect my key password name the file and click on Ok.

Note: If Signing is not creating then open the Visual studio as Run Admin and select the solution and do same process in step 4.

Project Properties -> Signing -> Sign the assembly


With out preimage: 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Microsoft.Xrm.Sdk;

 

namespace DetailedAmount

{

    public class DiscountPRO : IPlugin

    {

        public void Execute(IServiceProvider serviceProvider)

        {

            

            try

            {

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

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

                IOrganizationService service = factory.CreateOrganizationService(context.UserId);

                ITracingService tracing = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)

 

                {

                    Entity productDiscount = (Entity)context.InputParameters["Target"];

                    // decimal total = 1000;

                    //productDiscount["effizz_amount"] = total;

 

                    //decimal discount = (decimal)40.0 / 100 * total;

                    //productDiscount["effizz_discountamount"] = discount;

                    //tracing.Trace(discount + "total disc");

 

                    //decimal detailed = total - discount;

                    //productDiscount["effizz_totalamt"] = detailed;

                    //tracing.Trace(detailed + "total det");

 

                    Money preferAmount = productDiscount.Contains("effizz_amount") ? (Money)productDiscount["effizz_amount"] :null;

                    decimal total = 0;

                    if (preferAmount != null)

                    {

                        total = preferAmount.Value;

                    }

                    Money check = new Money(total);

                    productDiscount["effizz_amount"] = check;

                    //productDiscount["effizz_amount"] = total;

                    tracing.Trace("the amount is" + check);

                    //decimal total = 1000;

                    Money preferCount = productDiscount.Contains("effizz_discountamount") ? (Money)productDiscount["effizz_discountamount"] : null;

                    decimal discount = 0;

                    if (preferCount != null)

                    {

                        discount = preferCount.Value;

                    }

 

                    Money check1 = new Money(discount);

                    productDiscount["effizz_discountamount"] = check1;

                    tracing.Trace("total disc" + check1);

                    //productDiscount["effizz_discountamount"] = discount;

 

                    Money preferDetail = productDiscount.Contains("effizz_totalamt") ? (Money)productDiscount["effizz_totalamt"] : null;

                    decimal detailed = total - discount;

                    if (preferDetail != null)

                    {

                        detailed = preferDetail.Value;

                    }

                    Money check2 = new Money(detailed);

                    productDiscount["effizz_totalamt"] = check2;

                    tracing.Trace(check2 + "total det");

 

                    productDiscount["effizz_totalamt"] = null;

                    if (context.Depth == 1)

                    {

                        service.Update(productDiscount);

                    }

                }

            }

            catch (Exception ex)

           {

                throw new InvalidPluginExecutionException(ex.Message);

            }

        }

    }

}

 

 

With Preimage :

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Microsoft.Xrm.Sdk;

namespace DetailedAmount

{

    public class DiscountPRO : IPlugin

    {

        public void Execute(IServiceProvider serviceProvider)

        {

            try

            {

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

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

                IOrganizationService service = factory.CreateOrganizationService(context.UserId);

                ITracingService tracing = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)

                {

                    Entity productDiscount = (Entity)context.InputParameters["Target"];

                    Entity preImageAmount = (Entity)context.PreEntityImages["DetailedAmountCalculation"];

 

                    Money Amount = productDiscount.Contains("effizz_amount") ? (Money)productDiscount["effizz_amount"] : null;

                    Money preAmount = null;

                    decimal totalAmt = 0;

                    if (Amount != null)

                    {

                        totalAmt = Amount.Value;

                    }

                    if (Amount == null)

                    {

                        preAmount = preImageAmount.GetAttributeValue<Money>("effizz_amount");

                        totalAmt = preAmount.Value;

                    }

                    tracing.Trace("Amount:" + totalAmt);

                    Money totaldiscount = productDiscount.Contains("effizz_discountamount") ? (Money)productDiscount["effizz_discountamount"] : null;

                    Money preTotalDiscount = null;

                    decimal discountnew = 0;

                    if (totaldiscount != null)

                    {

                        discountnew = totaldiscount.Value;

                    }

                    if (totaldiscount == null)

                    {

                        preTotalDiscount = preImageAmount.GetAttributeValue<Money>("effizz_discountamount");

                        discountnew = preTotalDiscount.Value;

                       // tracing.Trace("totalDiscount PreImage:" + discountnew);

                    }

                    tracing.Trace("total discount:" + discountnew);

                    decimal detailedAmt = totalAmt - discountnew;

 

                    Money full = new Money(detailedAmt);

                    productDiscount["effizz_totalamt"] = full;

                    tracing.Trace("DetailedAmount:" + detailedAmt);

                    if (context.Depth == 1)

                    {

                        service.Update(productDiscount);

                    }

                }

            }

            catch (Exception ex)

            {

                throw new InvalidPluginExecutionException(ex.Message);

            }

        }

    }

}

 

Step 5: For pre-image registration go to plugin registration tool in XRM toolbox.

First register assemble with .dll file in plugin registration > next register a step as update > now register an image as pre-image.





Step 6: In register image for parameters we should take three fields

Amount, Discount amount and Total amount with schema name.

effizz_amount , effizz_discount , effizz_totalamount



Step 7: Go to Dynamics CRM 365 and check the functionality is working according to the scenario or not.





Comments

Popular posts from this blog

If any case created, then check for the same user how many cases are created with in 30 days, if more than 2 and less than 5 send a mail to team lead, if more than 5 and less than 9 then send a mail to manager using power automate.

Create approve & reject ribbon buttons, once click on approve it should change the status field to approve.If clicked on reject it should change to Reject. Based on status field change trigger work flow to send a email to stating request is approved/Rejected.

How to get and set values in plugins?