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
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...
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
This training provides practical hands-on experience in creating Power Apps solutions in a full-day of instructor-led App creation workshop.
Come together to explore latest innovations in code and application development—and gain insights from experts from around the world.
User | Count |
---|---|
12 | |
8 | |
5 | |
2 | |
2 |