1

My code does not seem to be updating the Lead entity. I have created a new Lead(); as you can see in the code and there is no update when I save the record. Is there something I am doing wrong? This way of updating is just as sufficient as updating by doing crmContext.UpdateObject(oLead); crmContext.SaveChanges();. Can someone please help me with why this is not updating the lead? There is a similar question that just refers to the basic plugin but instead of create I am updating.

        private List<Product> GetSectionAProducts()
    {

        List<Product> products = new List<Product>();
        var QEproduct = new QueryExpression("product");

        // Add all columns to QEproduct.ColumnSet
        QEproduct.ColumnSet.AllColumns = true;

        // Define filter QEproduct.Criteria
        QEproduct.Criteria.AddCondition("productnumber", ConditionOperator.BeginsWith, "MGIP");
        var QEproduct_Criteria_0 = new FilterExpression();
        QEproduct.Criteria.AddFilter(QEproduct_Criteria_0);

        // Define filter QEproduct_Criteria_0
        QEproduct_Criteria_0.AddCondition("capg_billingtimeframe", ConditionOperator.Equal, (int)capg_billingcycletype.Monthly);
        QEproduct_Criteria_0.AddCondition("capg_mincriteria", ConditionOperator.NotNull);
        QEproduct_Criteria_0.AddCondition("capg_maxcriteria", ConditionOperator.NotNull);
        QEproduct_Criteria_0.AddCondition("price", ConditionOperator.NotNull);

        EntityCollection results = this.OrganizationService.RetrieveMultiple(QEproduct);
        if (results.Entities != null)
        {
            // Retrieve all records from the result set.
            foreach (Entity product in results.Entities)
            {
                products.Add(new Product { Id = product.Id, capg_MinCriteria = (int?)product.Attributes["capg_mincriteria"], capg_MaxCriteria = (int?)product.Attributes["capg_maxcriteria"], Price =  (Money)product.Attributes["price"] });


            }
        }

        return products;
    }                    



                var duesproduct = sectionA.Where(o => o.capg_MinCriteria.Value <= dues).ToList().OrderByDescending(o => o.capg_MaxCriteria).FirstOrDefault();
                if (duesproduct != null)
                {
                    Xrm.Lead oLead = new Lead();
                    oLead.Id = this.InputTargetEntity.Id;
                    oLead.capg_CalculatedDuesBilling = new Money(duesproduct.Price == null ? 0 : duesproduct.Price.Value);
                    if (duesproduct.capg_MaxCriteria <= 100000)
                    {
                        oLead.capg_CalculatedDuesBilling = new Money(Math.Round((duesproduct.Price == null ? 0 : duesproduct.Price.Value) * new decimal(0.0290), 2));

                    }

                    if (duesproduct.capg_MaxCriteria <= 235000)
                    {
                        oLead.capg_CalculatedDuesBilling = new Money(Math.Round((duesproduct.Price == null ? 0 : duesproduct.Price.Value) * new decimal(0.0262), 2));
                    }

                    this.OrganizationService.Update(oLead);

                }
John Raesly
  • 308
  • 4
  • 11
  • Could you please check whether the oLead.Id is assigned with the right guid for the lead which you are expecting to get updated? – Renjith May 18 '16 at 04:21

1 Answers1

2

I highly recommend you updating records with the IOrganizationService.Update directly, not the context. Context is handy for queries, because you can use LINQ, but when you update entities via the context those might be sent to CRM as an update with all the entity attributes you selected, and that could cause weird behaviors because might trigger unexpected workflows. It also fills the Audit History really quickly.

It is much better to create a new instance of a lead, and populate it with the attributes and Id you want to update, only, and call service.Update.

Jordi
  • 1,460
  • 9
  • 9
  • I figured it out. Because I don't use IPlugin and my own classes it updates automatically and this.OrganizationService.Update(olead); just slowed down my plugin and caused it not to update. – John Raesly May 19 '16 at 15:39
  • Also, I first used linq but had trouble with the where clause that used contains. It is not a very viable choice, but thank you. – John Raesly May 19 '16 at 15:41
  • No worries, LINQ has some limitations indeed, once you know them, but it is handy because it uses strongly typed classes which are easier to maintain. Unfortunately LINQ can't do everything and sometimes you'll have to use QueryExpressions or FetchXml... unless you want to build your own Expression Trees :) – Jordi May 19 '16 at 21:28