John Raesly
John Raesly

Reputation: 308

Cannot update lead record in CRM?

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);

                }

Upvotes: 1

Views: 278

Answers (1)

Jordi
Jordi

Reputation: 1470

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.

Upvotes: 2

Related Questions