【问题标题】:Microsoft Dynamics CRM - Error message: Entity Id must be the same as the value set in property bagMicrosoft Dynamics CRM - 错误消息:实体 ID 必须与属性包中设置的值相同
【发布时间】:2016-05-17 09:43:39
【问题描述】:

您好 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);
            }

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

"Entity Id 必须与属性包中设置的值相同"。

如果我省略了这条线

Entity.Id = Guid.NewGuid();

然后我得到错误

“无法插入重复键。”

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

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

entity.Id = Guid.Empty;

导致

"Entity Id 必须与属性包中设置的值相同"。

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

非常感谢任何帮助。

【问题讨论】:

  • 我认为你在microsoft dynamic crm中创建一个插件你需要注册一个插件来创建,更新等,而不需要_organizationService.Create(entity); .

标签: c# dynamics-crm dynamics-crm-2013 microsoft-dynamics


【解决方案1】:

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

【讨论】:

  • 非常感谢。该解决方案对我有用。我也先尝试了 entity.Attributes.Remove(entity.LogicalName + "id") ,但没有成功。
猜你喜欢
  • 1970-01-01
  • 2011-11-17
  • 1970-01-01
  • 2021-07-17
  • 1970-01-01
  • 2018-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多