Quantcast
Channel: Sam's Activities
Viewing all articles
Browse latest Browse all 3363

Problem in plugin

$
0
0

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!


Viewing all articles
Browse latest Browse all 3363

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>