locked
Plugin to Fire on Quote Win RRS feed

  • Question

  • Hello everyone, I have having difficulty getting my plugin to fire properly. I am trying to build the plugin on entity quote message Win. However when I create Order and close Quote as Won nothing happens. The plugin is set to Post Operation Synchronous. I have also tired to run it Pre Operation Synchronous and receive the same results. No errors message are thrown.

    However, if I set the plugin up as Quote message Update, Post Operation Synchronous the plugin goes off without any errors and works perfectly. I have included my code in case that helps. I'm wondering if it has something to do with the Quote being Closed?? But then not sure why it wouldn't fire pre operation. Once the Quote is activated I can't change anything so the data should be there for the plugin to read on close as won? Is setting the message at Win not the correct message to use?

    I greatly appreciate any help. I've spent the entire day trying to troubleshoot this one issue, because it works so well on Quote Update just not on Quote Win.....

    namespace createLineItemsfromQuoteUpdate.QuoteUpdateLineItems
    {
        using System;
        using System.ServiceModel;
        using Microsoft.Xrm.Sdk;
        using Microsoft.Xrm.Sdk.Client;
        using System.Linq;
        using Microsoft.Xrm.Sdk.Query;
      
        public class PostQuoteUpdate: Plugin
        {
    
            public PostQuoteUpdate()
                : base(typeof(PostQuoteUpdate))
            {
                base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(40, "Update", "quote", new Action<LocalPluginContext>(ExecutePostQuoteUpdate)));
    
            }
    
            protected void ExecutePostQuoteUpdate(LocalPluginContext localContext)
            {
                if (localContext == null)
                {
                    throw new ArgumentNullException("localContext");
                }
    
                IPluginExecutionContext context = localContext.PluginExecutionContext;
                IOrganizationService service = localContext.OrganizationService;
    
                var ServiceContext = new OrganizationServiceContext(service);
                ITracingService tracingService = localContext.TracingService;
    
                if (context.InputParameters.Contains("Target") &&
                context.InputParameters["Target"] is Entity)
                {
    
                    Entity entity = (Entity)context.InputParameters["Target"];
                   
                        try
                        {
    
                            var res = from c in ServiceContext.CreateQuery("new_salescommissions")
                                      where c["new_quote"].Equals(entity.Id)
                                      select c;
    
                            var qp = from p in ServiceContext.CreateQuery("quotedetail")
                                     where p["quoteid"].Equals(entity.Id)
                                     select p;
    
                            foreach (var c in res)
                            {
                                Entity e = (Entity)c;
                                EntityReference ownerofsplit = (EntityReference)c.Attributes["ownerid"];
                                Entity ownervalue = service.Retrieve("systemuser", ownerofsplit.Id, new ColumnSet("new_commissionrate"));
                                decimal commRate = (decimal)ownervalue["new_commissionrate"];
    
                                Entity relatingQuoteValues = service.Retrieve("quote", entity.Id, new ColumnSet("totalamount", "new_totalongoing", "new_competitorconversion", "new_newclientsale", "new_routingorconsultingsale", "new_totalgpsunitssold"));
                                decimal salePerc = (decimal)2.00;
                                bool compConv = (bool)relatingQuoteValues["new_competitorconversion"];
                                bool newClient = (bool)relatingQuoteValues["new_newclientsale"];
                                bool rtcon = (bool)relatingQuoteValues["new_routingorconsultingsale"];
                                decimal gpsunits = (decimal)relatingQuoteValues["new_totalgpsunitssold"];
    
                                decimal points = (decimal)0.00;
                                Money total = (Money)relatingQuoteValues["totalamount"];
                                Money ongoing = (Money)relatingQuoteValues["new_totalongoing"];
                                decimal totalm = total.Value;
                                decimal ongoingm = ongoing.Value;
                                decimal split = (decimal)c["new_splitpercentage"];
                                Money totalSplit = new Money((split / 100) * totalm);
                                Money ongoingSplit = new Money((split / 100) * ongoingm);
    
                                if (compConv == true)
                                {
                                    salePerc = salePerc + 1;
                                    points = points + 1;
                                }
    
                                if (newClient == true)
                                {
                                    salePerc = salePerc + 1;
                                }
    
                                if (rtcon == true && newClient == true)
                                {
                                    points = points + 1;
                                }
    
                                if (ongoingm >= 10000)
                                {
                                    points = points + 1;
                                }
    
                                if (gpsunits >= 100)
                                {
                                    points = points + 1;
                                }
    
                                if (totalm >= 40000)
                                {
                                    e["new_pointsfromsale"] = (points * (split / 100));
                                }
                                else
                                {
                                    e["new_pointsfromsale"] = (decimal)0.00;
                                }
    
                                e["new_totalgpsunits"] = relatingQuoteValues["new_totalgpsunitssold"];
                                e["new_competitorconversion"] = relatingQuoteValues["new_competitorconversion"];
                                e["new_routingconsulting"] = relatingQuoteValues["new_routingorconsultingsale"];
                                e["new_newclientsale"] = relatingQuoteValues["new_newclientsale"];
                                e["new_totalsaleamount"] = relatingQuoteValues["totalamount"];
                                e["new_totalongoingamount"] = relatingQuoteValues["new_totalongoing"];
                                e["new_actualsalesfromsplit"] = totalSplit;
                                e["new_totalongoingsales"] = ongoingSplit;
                                e["new_newclientpoints"] = split / 100;
                                e["new_gpsunits"] = (gpsunits * (split / 100));
    
                                ServiceContext.UpdateObject(e);
    
                                foreach (var p in qp)
                                {
    
                                    EntityReference productname = (EntityReference)p.Attributes["productid"];
                                    Entity productnames = service.Retrieve("product", productname.Id, new ColumnSet("name", "new_commissionable", "producttypecode"));
                                    string name = (string)productnames["name"];
                                    bool commissionable = (bool)productnames["new_commissionable"];
                                    int gpsproduct = (int)productnames.GetAttributeValue<OptionSetValue>("producttypecode").Value;
    
                                    EntityReference unitname = (EntityReference)p.Attributes["uomid"];
                                    Entity unitnames = service.Retrieve("uom", unitname.Id, new ColumnSet("name"));
                                    string getuom = (string)unitnames["name"];
    
                                    if (commissionable == true)
                                    {
                                        Entity lineItem = new Entity("new_salescommissionslineitems");
    
                                        bool nc = (bool)c["new_northcarolinasale"];
                                        decimal splitPerc = (decimal)c["new_splitpercentage"];
                                        Money itemAmount = (Money)p["extendedamount"];
                                        decimal commAmount = itemAmount.Value;
                                        decimal commTotal = (commAmount * (splitPerc / 100));
                                        decimal commEarned = (decimal)0.00;
                                        decimal gpsunitspaid = (gpsunits * (splitPerc / 100));
                                        decimal gunitspaid = (decimal)2.5 * gpsunitspaid;
    
                                        if (nc == true)
                                        {
                                            decimal ncTax = (decimal)0.9825;
                                            commTotal = commTotal * ncTax;
                                        }
    
                                        if (newClient != true)
                                        {
                                            if (gpsproduct == 1 || gpsproduct == 100000005 || gpsproduct == 100000010)
                                            {
                                                decimal newsalePerc = (decimal)0.00;
                                                newsalePerc = salePerc + 1;
                                                decimal totalCommRate = ((commRate + newsalePerc) / 100);
                                                commEarned = (commTotal * totalCommRate);
                                                lineItem["new_commissionpercentage"] = totalCommRate;
                                            }
                                            else
                                            {
                                                decimal totalCommRate = ((commRate + salePerc) / 100);
                                                commEarned = (commTotal * totalCommRate);
                                                lineItem["new_commissionpercentage"] = totalCommRate;
                                            }
                                        }
                                        else
                                        {
                                            decimal totalCommRate = ((commRate + salePerc) / 100);
                                            commEarned = (commTotal * totalCommRate);
                                            lineItem["new_commissionpercentage"] = totalCommRate;
                                        }
    
                                        if (gpsproduct == 1 || gpsproduct == 100000005 || gpsproduct == 100000010)
                                        {
                                            if (gunitspaid > commEarned)
                                            {
                                                Money commissionearned = new Money(gunitspaid);
                                                lineItem["new_commissionearned"] = commissionearned;
                                            }
                                            else
                                            {
                                                Money commissionearned = new Money(commEarned);
                                                lineItem["new_commissionearned"] = commissionearned;
                                            }
                                        }
                                        else
                                        {
                                            Money commissionearned = new Money(commEarned);
                                            lineItem["new_commissionearned"] = commissionearned;
                                        }
    
                                        Money commissionableamount = new Money(commTotal);
                                        lineItem["new_name"] = name + " " + getuom;
                                        lineItem["new_salessplit"] = new EntityReference("new_salescommissions", new Guid(c.Id.ToString()));
                                        lineItem["ownerid"] = c["ownerid"];
                                        lineItem["new_splitpercentage"] = splitPerc;
                                        lineItem["new_commissonableamount"] = itemAmount;
                                        lineItem["new_commissionaftersplit"] = commissionableamount;
    
                                        service.Create(lineItem);
                                    }
                                }
    
                            }
    
                            ServiceContext.SaveChanges();
    
                        }
                        catch (FaultException ex)
                        {
                            throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
                        }
                    
                }
            }
        }
    }
    

    Thursday, October 31, 2013 9:27 PM

All replies

  • Hi

    You can fire plugin on create of "Quote Close" entity.

    When a quote is won a 'quote close' record is created. You can achieve your functionality by firing your plugin on create of quote close entity.


    Thanks, MS CRM Developer, Ranjan

    Friday, November 1, 2013 6:40 AM
  • Hello Ranjan, thanks for the response. I tried what you suggested with the same results. The child entity record are not updated and the child records to that entity are not created. I tried it on create of "Quote Close" for prevalidation, preoperation, and post operation and all returned the same results.

    I was thinking there could be some errors with preimage vs. postimage values? But I don't seem to run into any of these type of related issues when the plugin is set to Quote Update. Regardless of what fields I change or leave the same when I run the plugin it calculates everything correct every time.

    What if I left the plugin to Quote Update but added an if statement or catch to check the state of the Quote being Won. Would that work or would that not be practical to run the plugin every time a quote is updated when I only need it to fire on win?
    Friday, November 1, 2013 2:35 PM
  • Yes  you can approach the steps you have mentioned.

    "What if I left the plugin to Quote Update but added an if statement or catch to check the state of the Quote being Won."


    Thanks, MS CRM Developer, Ranjan

    Thursday, November 7, 2013 10:34 AM