如何访问 Dynamics 365 插件中的自定义实体字段
How to access Custom Entity fields in Dynamics 365 plugin
我有以下具有以下字段的自定义实体:
学生:
- 姓名
- ID
- 平均
课程:
- 姓名
- ID
参与者(持有特定学生在特定课程中的分数):
- 学生(以上)
- 课程(以上)
- 得分
我需要创建一个插件,根据 adding/updating 参与者的分数,相应地更新参与者的学生平均分。
所以我的逻辑是这样的:
- 参与者是 created/updated
- 遍历所有参与者以获取给定参与者的学生所修课程的数量并计算他们的分数。
- 相应地更新参与者的学生平均水平。
我遇到了一些问题:
- 正在访问所有已保存的参与者。
- 访问参与者学生以首先检查所有参与者并更新他们的平均值。
- 获取学生的平均分(本质上与上一个问题相同)。
任何能给我一些示例代码来帮助解决上述问题的人都会帮助我。
提前致谢。
您可以使用 service.RetrieveMultiple
使用查询表达式或 FetchXML 查询插件中的记录。例如,您可以使用 XrmToolBox FetchXML builder or simply download the fetchxml from CRM Advanced find builder and use it in below code sample. Read more
构建 fetchxml
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));
使用查询表达式:
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"]);
}
我有以下具有以下字段的自定义实体:
学生:
- 姓名
- ID
- 平均
课程:
- 姓名
- ID
参与者(持有特定学生在特定课程中的分数):
- 学生(以上)
- 课程(以上)
- 得分
我需要创建一个插件,根据 adding/updating 参与者的分数,相应地更新参与者的学生平均分。
所以我的逻辑是这样的:
- 参与者是 created/updated
- 遍历所有参与者以获取给定参与者的学生所修课程的数量并计算他们的分数。
- 相应地更新参与者的学生平均水平。
我遇到了一些问题:
- 正在访问所有已保存的参与者。
- 访问参与者学生以首先检查所有参与者并更新他们的平均值。
- 获取学生的平均分(本质上与上一个问题相同)。
任何能给我一些示例代码来帮助解决上述问题的人都会帮助我。 提前致谢。
您可以使用 service.RetrieveMultiple
使用查询表达式或 FetchXML 查询插件中的记录。例如,您可以使用 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));
使用查询表达式:
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"]);
}