Pre Validation Plugin code executing to early

0
43
Role Image


Hi guys,

I have a Delete Message on a Pre Validation Plugin. In the plugin I try to retrieve all related records from the record which I am deleting (for calculation purposes). Normally when I query these related entities I should get 2 entities returned. I get 0 returned so I think the Database is deleting the record before the Plugin code can finish doings its work. This is weird because on “Pre Validation” the plugin code should finish before database actions happen. I know for a fact that this record has 2 related records so this can not be the issue. Below is the full code of the plugin. 

if (context.MessageName == "Delete" && context.Stage == 10)
            {
                var extraKostEntity = context.PreEntityImages.FirstOrDefault(q => q.Key == "cref8_extrakost").Value;
                EntityReference extraKostERef = ((EntityReference)extraKostEntity.Attributes["cref8_extrakosttraining"]);
                Guid trainingID = new Guid(extraKostERef.Id.ToString());
                double extraKost = Convert.ToDouble(extraKostEntity.Attributes.FirstOrDefault(q => q.Key == "cref8_prijs").Value);

                var training = service.Retrieve("cref8_opleiding", trainingID, new ColumnSet(true));

                var query = new QueryExpression("cref8_cursist");
                query.ColumnSet = new ColumnSet(true);
                var intersection = new LinkEntity
                {
                    LinkFromEntityName = "cref8_cursist",
                    LinkToEntityName = "cref8_extrakost_cref8_cursist",
                    LinkFromAttributeName = "cref8_cursistid",
                    LinkToAttributeName = "cref8_cursistid",
                    JoinOperator = JoinOperator.Inner
                };
                intersection.LinkCriteria.AddCondition("cref8_extrakostid", ConditionOperator.Equal, extraKostEntity.Id);
                query.LinkEntities.Add(intersection);

                var cursistCollection = service.RetrieveMultiple(query);

                //Loop through the result
                foreach (var cursist in cursistCollection.Entities)
                {
                    //YEARLY BUDGET CURSIST DATA 
                    ConditionExpression ceYearlyBudgetFromTrainee = new ConditionExpression("cref8_cursist", ConditionOperator.Equal, cursist.Id);
                    ConditionExpression ceYearlyBudgetYear = new ConditionExpression("cref8_jaar", ConditionOperator.Equal, training.Attributes.FirstOrDefault(q => q.Key == "cref8_jaarstartopleiding").Value);

                    FilterExpression filter = new FilterExpression();
                    filter.Conditions.Add(ceYearlyBudgetFromTrainee);
                    filter.Conditions.Add(ceYearlyBudgetYear);

                    QueryExpression qeYearlyBudget = new QueryExpression("cref8_jaarlijkbudget");
                    qeYearlyBudget.ColumnSet = new ColumnSet(true);
                    qeYearlyBudget.Criteria.AddFilter(filter);
                    EntityCollection yearlyBudgetResult = service.RetrieveMultiple(qeYearlyBudget);

                    //BUDGETTEN 
                    double budgetOver = Convert.ToDouble(yearlyBudgetResult.Entities.First().Attributes.FirstOrDefault(q => q.Key == "cref8_overigebudget").Value);
                    //CALCULATIES
                    double nieuwBudget = budgetOver + extraKost;

                    Entity budget = new Entity("cref8_jaarlijkbudget");
                    budget["cref8_jaarlijkbudgetid"] = yearlyBudgetResult.Entities.First().Attributes.FirstOrDefault(q => q.Key == "cref8_jaarlijkbudgetid").Value;
                    budget["cref8_overigebudget"] = nieuwBudget;
                    service.Update(budget);
                }
            }

More info

  • Delete Message
  • Pre Validation Plugin
  • Synchronous
  • This is the only step the assembly has
  • The step has a Pre Image which is working fine

Any help would be greatly appreciated. I have no idea what could be causing this.

Best Regards,

Anthony



Source link

Leave a reply

Please enter your comment!
Please enter your name here