Revenue Round Off Plugin code for Microsoft dynamics CRM

 using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Microsoft.Xrm.Sdk;

using System.ServiceModel;


namespace MyPlugins

{

    public class RevenueRoundoff : IPlugin

    {

        public void Execute(IServiceProvider serviceProvider)

        {

            // Extract the tracing service for use in debugging sandboxed plug-ins.  

            // If you are not registering the plug-in in the sandbox, then you do  

            // not have to add any tracing service related code.  

            ITracingService tracingService =

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


            // Obtain the execution context from the service provider.  

            IPluginExecutionContext context = (IPluginExecutionContext)

                serviceProvider.GetService(typeof(IPluginExecutionContext));


            // Obtain the organization service reference which you will need for  

            // web service calls.  

            IOrganizationServiceFactory serviceFactory =

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

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




            // The InputParameters collection contains all the data passed in the message request.  

            if (context.InputParameters.Contains("Target") &&

                context.InputParameters["Target"] is Entity)

            {

                // Obtain the target entity from the input parameters.  

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



                try

                {

                    tracingService.Trace(context.Depth.ToString());


                    if (context.Depth > 1)

                        return;


                    // In case the user removes the value 

                    if (account.Attributes["revenue"] != null)

                    {

                        decimal revenue = ((Money)account.Attributes["revenue"]).Value;

                        revenue = Math.Round(revenue, 1);


                        account.Attributes["revenue"] = new Money(revenue);

                    }



                }


                catch (FaultException<OrganizationServiceFault> ex)

                {

                    throw new InvalidPluginExecutionException("An error occurred in MyPlug-in.", ex);

                }


                catch (Exception ex)

                {

                    tracingService.Trace("MyPlugin: {0}", ex.ToString());

                    throw;

                }

            }

        }

    }

}


Comments

Popular posts from this blog

Create Folder On Share Point by using Power Automate In Dynamics CRM

Upload Email Attachments To share point and Delete from Dynamics CRM Through Power Automate Flow - Power Automate

Duplicate Detection Plugin code for Microsoft dynamics CRM