gpt4 book ai didi

c# - Microsoft Dynamics CRM - 错误消息 : Entity Id must be the same as the value set in property bag

转载 作者:行者123 更新时间:2023-11-30 14:25:46 29 4
gpt4 key购买 nike

您好 StackOverflow 社区,

我只是尝试从插件或自定义工作流事件中复制“联系人”- 实体的记录。相关代码是

            QueryExpression qe = new QueryExpression("contact")
{
ColumnSet = new ColumnSet("firstname", "lastname")
};
EntityCollection entityCollection = _organizationService.RetrieveMultiple(qe);
foreach (Entity entity in entityCollection.Entities)
{
entity.Id = Guid.NewGuid();

if (!entity.Attributes.Contains("firstname"))
{
entity.Attributes.Add("firstname", "");
}
entity["firstname"] = (entity.GetAttributeValue<string>("firstname") ?? "") + "(Copy)";

_organizationService.Create(entity);
}

不幸的是,我总是收到错误消息

“实体Id必须与属性包中设置的值相同”。

如果我省略了这一行

Entity.Id = Guid.NewGuid();

然后我得到了错误

“无法插入重复键。”

我还尝试了各种其他方法来构造一个新的 Guid,包括

byte [] bytes = new byte[16];
random.NextBytes(bytes);
entity.Id = new Guid(bytes);

entity.Id = Guid.Empty;

结果也是

“实体Id必须与属性包中设置的值相同”。

另一方面,我有一个桌面应用程序,我可以在本文的帮助下连接到我的 Microsoft CRM 2016 Office 365 系统 https://msdn.microsoft.com/en-us/library/jj602970.aspx并且可以复制记录。

非常感谢任何帮助。

最佳答案

QueryExpression 始终返回 id,既作为实际的 Entity.Id,又作为属性名称 contactid。这就是您收到错误的原因。您可以只删除此属性,但最佳做法是始终创建一个新的 C# 联系人对象,而不是更新从 CRM 检索到的对象。此外,您不希望设置自己的 GUID,因为 CRM 使用顺序 GUID,这些 GUID 针对索引和排序进行了更好的优化。

QueryExpression qe = new QueryExpression("contact")
{
ColumnSet = new ColumnSet("firstname", "lastname")
};

foreach (Entity entity in _organizationService.RetrieveMultiple(qe).Entities)
{
var copy = new Entity();
copy["firstname'] = (entity.GetAttributeValue<string>("firstname") ?? "") + "(Copy)";
_organizationService.Create(copy);
}

关于c# - Microsoft Dynamics CRM - 错误消息 : Entity Id must be the same as the value set in property bag,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37272585/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com