Reputation: 3
I have the following custom entities with the following fields:
Student:
Course:
Participant (Holds a particular Student's score in a particular Course):
I need to create a plugin which upon adding/updating a Participant's score, it updates the Participant's Student's Average accordingly.
So my logic is as follows:
There are a few things I'm having trouble with:
Anyone who could give me some sample code to help with the aforementioned problems would really be helping me out. Thanks in advance.
Upvotes: 0
Views: 1393
Reputation: 11
Using Query Expression :
QueryExpression qe = new QueryExpression();
qe.EntityName = "new_particpiant";
ColumnSet columns = new ColumnSet(
new string[1]
{
"new_average",
});
ConditionExpression ce = new ConditionExpression
{
AttributeName = "new_particpiant",
Operator = ConditionOperator.Equal,
Values = { 'Your Guid' }
};
FilterExpression filterQuery = new FilterExpression();
filterQuery.FilterOperator = LogicalOperator.And;
filterQuery.AddCondition(ce);
qe.ColumnSet = columns;
EntityCollection ec = service.RetrieveMultiple(qe);
Entity data = new Entity();
if (ec.Entities.Count > 0)
{
data = ec.Entities[0];
string average = Convert.ToString(data.Attributes["new_average"]);
}
Upvotes: 0
Reputation: 22846
You can do query the records in a plugin using Query Expression or FetchXML using service.RetrieveMultiple
. For example, you can build the fetchxml using XrmToolBox FetchXML builder or simply download the fetchxml from CRM Advanced find builder and use it in below code sample. Read more
var fetchXml = $@"
<fetch>
<entity name='new_particpiant'>
<attribute name='new_average'/>
<filter type='and'>
<condition attribute='new_particpiant' operator='eq' value='{GUID}'/>
</filter>
</entity>
</fetch>";
EntityCollection entities = service.RetrieveMultiple(new FetchExpression(fetchXml));
Upvotes: 1