0

I have a Delete Message on a Pre Validation Plugin. In the plugin I try to retrieve all related records (cursists) from the record(extraKost) which I am deleting (for calculation purposes). Normally when I query these related entities I should get 2 entities (cursists) 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);
            }
        }



All 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

  • 1
    I can't say if this is related and would help with your issue, but judging from what you are doing in your code, it's more suitable to fire this plug-in PreOperation (Stage = 20) rather than PreValidation. This is because PreOperation fires inside the transaction. So in case something goes wrong, the changes your plugin does would be rolled back. If you keep using PreValidation there might be a situation then your plug-in will update records, but the related record itself will not be deleted – J-M Dec 23 '21 at 15:22
  • Can you confirm whether `cursists` is in a cascade-delete relationship with `extraKost`? – avolkmann Feb 26 '22 at 12:20

0 Answers0