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
Any help would be greatly appreciated. I have no idea what could be causing this.
Best Regards,
Anthony
Deer Rawish Kumar ,
I must have missed calculations because the code is actually working fine. However when replaying in debug mode you can't debug because cursists are already unrelated. I now develop my "delete" actions in a update message trigger so i can debug easy. When it works i change it to a delete. If you have suggestions on how i can do this better please let me know.
Best Regards,
Anthony
Hi AnthonyD,
Yes, you will be able to retrieve the records which are being deleted in pre-validation stage.
to make things a little simpler, have you tried executing a simple fetch xml rather in debug mode?
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,253 Super User 2024 Season 2
Martin Dráb 230,188 Most Valuable Professional
nmaenpaa 101,156