locked
CRM 2013 Plugin error while rolling up Quote Products total to Quote RRS feed

  • Question

  • Hi,

    I have written a CRM 2013 plugin which fires on creation on Quote Products that will roll up the total amount to parent Quote.

    I am receiving the error at the last line. Code and error are detailed below

    Could you please advise me?

    Many thanks for your help.

    CODE:

    decimal ProductsTotal = 0;

    EntityReference parentQuote;

    int b;
    //Retrieve related Quote's information
    parentQuote = (EntityReference)entity.Attributes["quoteid"];
    Guid parentQuoteID = parentQuote.Id;

    //Retrieve 'Product Type' field from Quote Record
    OptionSetValue ProductType = entity.GetAttributeValue<OptionSetValue>("out_producttype");
    b = ProductType.Value;

    //fetchxml to roll up the product totals to Quote. Option 973420001 = 'Product Cost'

    string allQuoteProductsTotal_sum = string.Format(@"<fetch distinct='false' mapping='logical' aggregate='true'>
                                                                  <entity name='quotedetail'>
                                                                  <attribute name='extendedamount' alias='products_sum' aggregate='sum' />
                                                                  <filter type='and'>                                                        
                                                                  <condition attribute='quoteid' operator='eq' uiname='' uitype='quote' value='{0}' />
                                                                  <condition attribute='out_producttype' operator='eq' value='{1}'/>                                                                                         </filter>
                                                                  </entity>
                                                                  </fetch>", parentQuoteID, b);

    EntityCollection allProductsTotal_sum_result = service.RetrieveMultiple(new FetchExpression(allQuoteProductsTotal_sum));

                        foreach (var c in allProductsTotal_sum_result.Entities)
                        {
                            if (c.Attributes.ContainsKey("products_sum"))
                            {
                                //ProductsTotal = ProductsTotal + ((Decimal)((AliasedValue)c["products_sum"]).Value);
                                decimal totalValue = ((Money)((AliasedValue)c["products_sum"]).Value).Value;

                                ProductsTotal = ProductsTotal + totalValue;
                            }
                        }


                        string oAnnualLeaveAllowanceId = parentQuote.Id.ToString();

                        Entity quote = new Entity("quote");
                        Guid abc = ((EntityReference)entity.Attributes["quoteid"]).Id;
                        quote.Id = abc;
                        quote.Attributes["new_producttotal"] = ProductsTotal;
                        service.Update(quote); //It fails at this point
                    

    ERROR:

    System.InvalidCastException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #3A82E374

    Regards,

    Welsey


    • Edited by Paul Wesley Tuesday, November 18, 2014 6:32 PM
    Tuesday, November 18, 2014 6:31 PM

Answers

  • Please make sure your new_producttotal field is of type money. Use the code below

    decimal ProductsTotal = 0;
    
    EntityReference parentQuote;
    int b;
    //Retrieve related Quote's information
    parentQuote = (EntityReference)entity.Attributes["quoteid"];
    Guid parentQuoteID = parentQuote.Id;
    
    //Retrieve 'Product Type' field from Quote Record
    OptionSetValue ProductType = entity.GetAttributeValue<OptionSetValue>("out_producttype");
    b = ProductType.Value;
    
    //fetchxml to roll up the product totals to Quote. Option 973420001 = 'Product Cost'
    
    string allQuoteProductsTotal_sum = string.Format(@"<fetch distinct='false' mapping='logical' aggregate='true'>
                                                                  <entity name='quotedetail'>
                                                                  <attribute name='extendedamount' alias='products_sum' aggregate='sum' />
                                                                  <filter type='and'>                                                        
                                                                  <condition attribute='quoteid' operator='eq' uiname='' uitype='quote' value='{0}' />
                                                                  <condition attribute='out_producttype' operator='eq' value='{1}'/>                                                                                         </filter>
                                                                  </entity>
                                                                  </fetch>", parentQuoteID, b);
    
    EntityCollection allProductsTotal_sum_result = service.RetrieveMultiple(new FetchExpression(allQuoteProductsTotal_sum));
    
                        foreach (var c in allProductsTotal_sum_result.Entities)
                        {
                            if (c.Attributes.ContainsKey("products_sum"))
                            {
                                //ProductsTotal = ProductsTotal + ((Decimal)((AliasedValue)c["products_sum"]).Value);
                                decimal totalValue = ((Money)((AliasedValue)c["products_sum"]).Value).Value;
    
                              //  ProductsTotal = ProductsTotal + totalValue;
                            }
                        }
    
    
                        string oAnnualLeaveAllowanceId = parentQuote.Id.ToString();
    
                        Entity quote = new Entity("quote");
                        Guid abc = ((EntityReference)entity.Attributes["quoteid"]).Id;
                        quote.Id = abc;
                        quote.Attributes.Add("new_producttotal",new Money(totalValue));
    
                        service.Update(quote); //It fails at this point


    Regards, Saad

    • Marked as answer by Paul Wesley Wednesday, November 19, 2014 10:41 AM
    Wednesday, November 19, 2014 5:54 AM

All replies

  • Please make sure your new_producttotal field is of type money. Use the code below

    decimal ProductsTotal = 0;
    
    EntityReference parentQuote;
    int b;
    //Retrieve related Quote's information
    parentQuote = (EntityReference)entity.Attributes["quoteid"];
    Guid parentQuoteID = parentQuote.Id;
    
    //Retrieve 'Product Type' field from Quote Record
    OptionSetValue ProductType = entity.GetAttributeValue<OptionSetValue>("out_producttype");
    b = ProductType.Value;
    
    //fetchxml to roll up the product totals to Quote. Option 973420001 = 'Product Cost'
    
    string allQuoteProductsTotal_sum = string.Format(@"<fetch distinct='false' mapping='logical' aggregate='true'>
                                                                  <entity name='quotedetail'>
                                                                  <attribute name='extendedamount' alias='products_sum' aggregate='sum' />
                                                                  <filter type='and'>                                                        
                                                                  <condition attribute='quoteid' operator='eq' uiname='' uitype='quote' value='{0}' />
                                                                  <condition attribute='out_producttype' operator='eq' value='{1}'/>                                                                                         </filter>
                                                                  </entity>
                                                                  </fetch>", parentQuoteID, b);
    
    EntityCollection allProductsTotal_sum_result = service.RetrieveMultiple(new FetchExpression(allQuoteProductsTotal_sum));
    
                        foreach (var c in allProductsTotal_sum_result.Entities)
                        {
                            if (c.Attributes.ContainsKey("products_sum"))
                            {
                                //ProductsTotal = ProductsTotal + ((Decimal)((AliasedValue)c["products_sum"]).Value);
                                decimal totalValue = ((Money)((AliasedValue)c["products_sum"]).Value).Value;
    
                              //  ProductsTotal = ProductsTotal + totalValue;
                            }
                        }
    
    
                        string oAnnualLeaveAllowanceId = parentQuote.Id.ToString();
    
                        Entity quote = new Entity("quote");
                        Guid abc = ((EntityReference)entity.Attributes["quoteid"]).Id;
                        quote.Id = abc;
                        quote.Attributes.Add("new_producttotal",new Money(totalValue));
    
                        service.Update(quote); //It fails at this point


    Regards, Saad

    • Marked as answer by Paul Wesley Wednesday, November 19, 2014 10:41 AM
    Wednesday, November 19, 2014 5:54 AM
  • Hello Saad,

    Many thanks for the help!

    This fixed my problem!!!!!!

    Have a great day!

    Wesley

    Wednesday, November 19, 2014 10:41 AM