Skip to main content

Notifications

Customer experience | Sales, Customer Insights,...
Suggested answer

Pre Validation Plugin code executing to early

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

  • AnthonyD Profile Picture
    AnthonyD 40 on at
    RE: Pre Validation Plugin code executing to early

    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

  • Suggested answer
    Rawish Kumar Profile Picture
    Rawish Kumar 13,756 on at
    RE: Pre Validation Plugin code executing to early

    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?

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans