CRM 2011 Pre-Operation Update 相关实体查找字段
我正在尝试更新CRM 2011预操作更新插件中的相关记录。一个简单的查询根据几个参数检索一组相关实体,然后结果查询中的每个实体都会查找相关实体集。但是,当引发异常时,会抛出提示 InvalidCastException 的异常。单步执行代码后,每个值看起来都是正确的,Guids 符合预期等。我已经禁用了可能涉及目标和相关实体的其他插件,但问题仍然存在。 targetEntity 和 relatedEntity 之间的关系是 1:N。我已经确认这个问题不是因为该属性在单步执行时需要 EntityReference 而不是 Guid。
该插件的代码如下,我用真实的实体/属性名称替换了更明显的代理(它使用开发人员工具包插件类文件):-
Entity targetEntity = (Entity)localContext.PluginExecutionContext.InputParameters["Target"];
QueryExpression query = new QueryExpression
{
EntityName = "relatedEntity",
Criteria =
{
Conditions =
{
new ConditionExpression("testOptionSetAttribute", ConditionOperator.Equal, 100000004),
new ConditionExpression("parentEntityId", ConditionOperator.Null)
}
}
};
EntityCollection resultsCollection = localContext.OrganizationService.RetrieveMultiple(query);
foreach (Entity result in resultsCollection.Entities)
{
result.Attributes["parentEntityId"] = targetEntity.Id;
localContext.OrganizationService.Update(result);
}
任何帮助将不胜感激。
谢谢。
I am attempting to update a related record in a CRM 2011 pre-operation update plug-in. A simple query retrieves a set of related entities based upon a couple of parameters and then each entity from the resultant query has it's lookup to the related entity set. However, when firing an exception is thrown suggesting an InvalidCastException. Having stepped through the code each of the values looks to be correct, Guids where expected etc. I have disabled other plug-ins which may involve the target and related entities, but still the problem persists. The relationship between the targetEntity and the relatedEntity is 1:N. I have confirmed that this issue is not because the attribute is expecting an EntityReference rather than a Guid while stepping through.
The code for the plug in is below, where I have substituted real entity/attribute names for more obvious proxies (it uses the developer toolkit Plugin class file):-
Entity targetEntity = (Entity)localContext.PluginExecutionContext.InputParameters["Target"];
QueryExpression query = new QueryExpression
{
EntityName = "relatedEntity",
Criteria =
{
Conditions =
{
new ConditionExpression("testOptionSetAttribute", ConditionOperator.Equal, 100000004),
new ConditionExpression("parentEntityId", ConditionOperator.Null)
}
}
};
EntityCollection resultsCollection = localContext.OrganizationService.RetrieveMultiple(query);
foreach (Entity result in resultsCollection.Entities)
{
result.Attributes["parentEntityId"] = targetEntity.Id;
localContext.OrganizationService.Update(result);
}
Any help would be appreciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我同意彼得的观点。
它必须是一个
EntityReference
,而且它必须是一个new EntityReference
。试试这个:
I agree with Peter.
It must be an
EntityReference
, moreover it must be anew EntityReference
.Try this: