cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Anthony_Dob
Responsive Resident
Responsive Resident

Pre Validation Plugin | Delete operation | Retrieve related data before delete

Hello guys,

 

I am facing something quite odd. I have a plugin for a delete of a record. This plugin step is on Pre Validation (it also has a pre image). However when I try to fetch the related records of the parent record (which I am deleting), I retrieve 0 records. On Pre Validation the code should run before executing deletion?? I am sure the parent record has related records.

 

This is the code I am running:

 

 

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()
                {
                    //Set this to the entity you wish to retrieve data from
                    EntityName = "cref8_cursist",

                    //This will return us all columns for each record returned
                    ColumnSet = new ColumnSet(true),
                };

                //AddLink(RELATIONSHIP ENTITY NAME, ENTITY UNIQUE ID FROM THE ENTITIE YOU WISH TO RETRIEVE DATA FROM, ENTITY UNIQUE ID FROM THE ENTITIE YOU WISH TO RETRIEVE DATA FROM
                var link = query.AddLink("cref8_extrakost_cref8_cursist", "cref8_cursistid", "cref8_cursistid");
                link.LinkCriteria = new FilterExpression()
                {
                    //Add this to filter the Trainee records for a specified Course
                    Conditions =
                                    {
                                        new ConditionExpression("cref8_extrakostid", ConditionOperator.Equal, extraKostEntity.Id)
                                    }
                };

                //Invoke the service with our query
                var Cursists = service.RetrieveMultiple(query);

                //Loop through the result
                foreach (var cursist in Cursists.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);
                }
            }

 

 

This is the bit of code that returns related records:

 

var query = new QueryExpression()
                {
                    //Set this to the entity you wish to retrieve data from
                    EntityName = "cref8_cursist",

                    //This will return us all columns for each record returned
                    ColumnSet = new ColumnSet(true),
                };

                //AddLink(RELATIONSHIP ENTITY NAME, ENTITY UNIQUE ID FROM THE ENTITIE YOU WISH TO RETRIEVE DATA FROM, ENTITY UNIQUE ID FROM THE ENTITIE YOU WISH TO RETRIEVE DATA FROM
                var link = query.AddLink("cref8_extrakost_cref8_cursist", "cref8_cursistid", "cref8_cursistid");
                link.LinkCriteria = new FilterExpression()
                {
                    //Add this to filter the Trainee records for a specified Course
                    Conditions =
                                    {
                                        new ConditionExpression("cref8_extrakostid", ConditionOperator.Equal, extraKostEntity.Id)
                                    }
                };

                //Invoke the service with our query
                var Cursists = service.RetrieveMultiple(query);

 

 

Normally Cursists should contain 2 entities but now it's returning 0 entities.


What could be the problem here?

 

Best Regards,

Anthony

2 REPLIES 2
MayankP
Responsive Resident
Responsive Resident

Hi @Anthony_Dob 

 

I suggest debug your code using plug in profiler as per this video 

https://www.youtube.com/watch?v=ZwQ8ergVVBw

 

This should help you figure out which line got issue, I suspect may be issue with the query (or link or filter criteria is wrong), 

 

another thing you could do is to check is run this query (or code) out side plug in sample app to see if this is returning valid data... 

Anthony_Dob
Responsive Resident
Responsive Resident

hi @MayankP ,

 

looks like the code was actually working. The debugger threw me off because I was not getting any related records anymore (because they were already deleted). I would advice people having trouble debugging "Delete" messages to trigger them as a "Update" messages while developing your plugin. This way you can debug with all records still in your code. Once the plugin works, switch it back to the "Delete" message.

 

Best Regards,

Anthony

Helpful resources

Announcements
Ignite 2022

WHAT’S NEXT AT MICROSOFT IGNITE 2022

Explore the latest innovations, learn from product experts and partners, level up your skillset, and create connections from around the world.

Power Apps Africa Challenge 2022

Power Apps Africa Challenge

Your chance to join an engaging competition of Power Platform enthusiasts.

Super User 2 - 2022 Congratulations

Welcome Super Users

The Super User program for 2022 - Season 2 has kicked off!

September Events 2022

Check out all of these events

Attend in person or online, there are incredible conferences and events happening all throughout the month of September.

Users online (3,187)