Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Problem in plugin

Posted on by Microsoft Employee

Hi Expert

I created a plugin on entity "opportunityproduct"

Create and update of event "PostOpreation"

And the code is this

Create

protected override void ExecuteCrmPlugin(LocalPluginContext serviceProvider)

       {

           if (serviceProvider == null)

           {

               throw new InvalidPluginExecutionException("serviceProvider");

           }

           // TODO: Implement your custom Plug-in business logic.

           IPluginExecutionContext context = serviceProvider.PluginExecutionContext;

           //Check weather the "Traget" exists in the inpu parameter

           if (context.InputParameters.Contains("Target") == false) return;

           //Check weather the Input Parameter is a entity or not

           if (context.InputParameters["Target"] is Entity == false) return;

           //Get service and service factory from service provider            

           IOrganizationService service = serviceProvider.OrganizationService;

           //Extract the tracing service for use in debugging and to do tracing            

           ITracingService tracingService = serviceProvider.TracingService;

           //Check weather the Target entity have the Opportunity Line or not

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

           if (context.MessageName == "Create")

           {

               var ProductId = selectedEntity.GetAttributeValue<EntityReference>("productid").Id;

               var PostAmount = ((Money)context.PostEntityImages["PostImage"].Attributes["priceperunit_base"]).Value;

               //get Gst Rate

               ColumnSet new_rate = new ColumnSet("new_rate");

               Entity getRate = service.Retrieve("product", ProductId, new_rate);

               if ((Money)getRate.GetAttributeValue<Money>("new_rate") != null)

               {

                   decimal getGSTRate = (decimal)getRate.GetAttributeValue<Money>("new_rate").Value;

                   selectedEntity["tax"] =new Money(Math.Round(((PostAmount * getGSTRate) / 100), 2));

                   service.Update(selectedEntity);

               }

           }

       }

Update

protected override void ExecuteCrmPlugin(LocalPluginContext serviceProvider)

       {

           try

           {

               if (serviceProvider == null)

               {

                   throw new InvalidPluginExecutionException("serviceProvider");

               }

               // TODO: Implement your custom Plug-in business logic.

               IPluginExecutionContext context = serviceProvider.PluginExecutionContext;

               //Check weather the "Traget" exists in the inpu parameter

               if (context.InputParameters.Contains("Target") == false) return;

               //Check weather the Input Parameter is a entity or not

               if (context.InputParameters["Target"] is Entity == false) return;

               //Get service and service factory from service provider            

               IOrganizationService service = serviceProvider.OrganizationService;

               //Extract the tracing service for use in debugging and to do tracing            

               ITracingService tracingService = serviceProvider.TracingService;

               //Check weather the Target entity have the originating lead filed or not

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

               if (context.MessageName == "Update")

               {

                   var PostAmount = ((Money)context.PostEntityImages["PostImage"].Attributes["baseamount"]).Value;

                   var productID = ((EntityReference)context.PostEntityImages["PostImage"].Attributes["productid"]).Id;

                   ColumnSet new_rate = new ColumnSet("new_rate");

                   Entity getRate = service.Retrieve("product", productID, new_rate);

                   if ((Money)getRate.GetAttributeValue<Money>("new_rate") != null)

                   {

                       decimal getGSTRate = (decimal)getRate.GetAttributeValue<Money>("new_rate").Value;                        

                       selectedEntity["tax"] = new Money(Math.Round(((PostAmount * getGSTRate) / 100),2));

                       service.Update(selectedEntity);

                   }

               }

           }

           catch (Exception ex)

           {

               throw new InvalidPluginExecutionException(ex.Message);

           }

       }

When i am creating a product, if i am disable update plugin then product will be create otherwise they giving error!

Please give me suggestion.

Thanks in advance!

*This post is locked for comments

  • Inogic Profile Picture
    Inogic 24,094 on at
    RE: Problem in plugin

    Please try with below Code it works for me:

     

        protected override void ExecuteCrmPlugin (IServiceProvider serviceProvider)

            {

               

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

     

              

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

     

               

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

     

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

          

                           

     

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

     

                    if (context.MessageName == "Update")

                    {

     

                        var PostAmount = ((Money)context.PostEntityImages["PostImage"].Attributes["baseamount"]).Value;

     

                        var productID = ((EntityReference)context.PostEntityImages["PostImage"].Attributes["productid"]).Id;

     

                        ColumnSet new_rate = new ColumnSet("new_rate");

     

                        Entity getRate = service.Retrieve("product", productID, new_rate);

     

                        if ((Money)getRate.GetAttributeValue<Money>("new_rate") != null)

                        {

     

                            decimal getGSTRate = (decimal)getRate.GetAttributeValue<Money>("new_rate").Value;

     

                            selectedEntity["tax"] = new Money(Math.Round(((PostAmount * getGSTRate) / 100), 2));

     

                            service.Update(selectedEntity);

     

                        }

     

                    }

            }

     

    Hope this helps.

     

    Thanks!

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Problem in plugin

    Hi,

      protected override void ExecuteCrmPlugin(LocalPluginContext serviceProvider)
    
            {
                ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
                IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
    
                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
    
                //Check weather the Target entity have the Opportunity Line or not
    
                if (context.Depth > 1) { return; }
                Entity selectedEntity = (Entity)context.InputParameters["Target"];
    
                if (context.MessageName == "Create")
    
                {
    
                    var ProductId = selectedEntity.GetAttributeValue<EntityReference>("productid").Id;
    
                    var PostAmount = ((Money)context.PostEntityImages["PostImage"].Attributes["priceperunit_base"]).Value;
    
                    //get Gst Rate
    
                    ColumnSet new_rate = new ColumnSet("new_rate");
    
                    Entity getRate = service.Retrieve("product", ProductId, new_rate);
    
                    if ((Money)getRate.GetAttributeValue<Money>("new_rate") != null)
    
                    {
    
                        decimal getGSTRate = (decimal)getRate.GetAttributeValue<Money>("new_rate").Value;
    
                        selectedEntity["tax"] = new Money(Math.Round(((PostAmount * getGSTRate) / 100), 2));
    
                        service.Update(selectedEntity);
    
                    }
    
                }
    
            }


    Try with this  -

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Problem in plugin

    Yes for that you can check depth like below after getting context .

    if (context.Depth > 1) { return; }

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Problem in plugin

    Hi Goutam,

    When I am updataing "service.update" at the time of create plugin  and also in Update plugin i think both plugin call at the same time ,they conflict.

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Problem in plugin

    Hi,

    Could you please try to check this  - the highlighted are  modified -

           protected override void ExecuteCrmPlugin(IServiceProvider serviceProvider)
            {
                try
    
                {
                    ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
                    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
    
                    Entity selectedEntity = (Entity)context.InputParameters["Target"];
    
                    if (context.MessageName == "Update")
    
                    {
                        Entity _postImageEntity = (context.PostEntityImages != null && context.PostEntityImages.Contains("PostImage")) ? context.PostEntityImages["PostImage"] : null;
    
                        decimal PostAmount= 0;
                        Guid productID = new Guid();
    
                        if (_postImageEntity.Attributes.Contains("baseamount"))
                        {
                             PostAmount = ((Money)context.PostEntityImages["PostImage"].Attributes["baseamount"]).Value;
                        }
    
                        if (_postImageEntity.Attributes.Contains("productid"))
                        {
                            productID = ((EntityReference)context.PostEntityImages["PostImage"].Attributes["productid"]).Id;
                        }
    
                        ColumnSet new_rate = new ColumnSet("new_rate");
    
                        Entity getRate = service.Retrieve("product", productID, new_rate);
    
                        if ((Money)getRate.GetAttributeValue<Money>("new_rate") != null)
    
                        {
    
                            decimal getGSTRate = (decimal)getRate.GetAttributeValue<Money>("new_rate").Value;
    
                            selectedEntity["tax"] = new Money(Math.Round(((PostAmount * getGSTRate) / 100), 2));
    
                            service.Update(selectedEntity);
    
                        }
    
                    }
    
                }
    
                catch (Exception ex)
    
                {
    
                    throw new InvalidPluginExecutionException(ex.Message);
    
                }
    
            }


Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans