yonSon
yonSon

Reputation: 3

How to access Custom Entity fields in Dynamics 365 plugin

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:

  1. Participant is created/updated
  2. Loop through all Participants to get the number of courses being taken by the given Participant's Student and sum their scores.
  3. Update Participant's Student's Average accordingly.

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

Answers (2)

Agasti
Agasti

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

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

enter image description here

    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

Related Questions