web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

Pre Validation Plugin code executing to early

(0) ShareShare
ReportReport
Posted on by 40

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

I have the same question (0)
  • Suggested answer
    Rawish Kumar Profile Picture
    13,758 on at

    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?

  • AnthonyD Profile Picture
    40 on at

    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

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 69 Most Valuable Professional

#2
ManoVerse Profile Picture

ManoVerse 62 Super User 2026 Season 1

#3
11manish Profile Picture

11manish 43

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans