简体   繁体   English

如何访问Dynamics 365插件中的自定义实体字段

[英]How to access Custom Entity fields in Dynamics 365 plugin

I have the following custom entities with the following fields: 我具有以下字段的自定义实体:

Student: 学生:

  • Name 名称
  • ID ID
  • Average 平均

Course: 课程:

  • Name 名称
  • ID ID

Participant (Holds a particular Student's score in a particular Course): 参与者(在特定课程中获得特定学生的分数):

  • Student (above) 学生(以上)
  • Course (above) 课程(以上)
  • Score 得分

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: 我遇到了一些麻烦:

  • Accessing all saved Participants. 访问所有已保存的参与者。
  • Accessing the Participants Student to first check against all Participants and to update their average. 访问参与者学生首先检查所有参与者并更新他们的平均值。
  • Accessing the Student's average (which is essentially the same issue as the previous one). 获取学生的平均值(与上一个平均值基本相同)。

Anyone who could give me some sample code to help with the aforementioned problems would really be helping me out. 任何可以给我一些示例代码来解决上述问题的人,都将真正帮助我。 Thanks in advance. 提前致谢。

You can do query the records in a plugin using Query Expression or FetchXML using service.RetrieveMultiple . 您可以使用Query Expression或FetchXML使用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. 例如,您可以使用XrmToolBox FetchXML构建器构建 fetchxml,或者直接从CRM Advanced查找构建器下载fetchxml并在下面的代码示例中使用它。 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));

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"]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM