【问题标题】:Plugin Pre Operation Create - Update field error插件操作前创建 - 更新字段错误
【发布时间】:2014-03-08 13:55:47
【问题描述】:

我的插件在实体 X 上的 Pre Create 操作时触发。当尝试使用以下代码更新实体 X 上的字段时,我收到错误:

trEntity = (Entity)context.InputParameters["Target"];
trGuid = (Guid)trEntity.Id;

tr = (Entity)service.Retrieve("EntityX", trGuid,
                    new ColumnSet(new string[] { "field_a", "field_b" }));


tr["field_a"] = null;
service.Update(tr);

我得到的错误是: Id = 11505683-2292-b537-e311-143710e56fb7 的实体 X 不存在

【问题讨论】:

    标签: dynamics-crm dynamics-crm-2011 power-platform


    【解决方案1】:

    由于您位于Pre-Create,因此该实体在数据库中尚不存在。

    您无需在 Pre 事件中显式调用 Update。您只需更新Target 实体(在您的情况下为trEntity),您所做的更改将通过Create 操作保存。 Target 实体是即将创建的实际实体,因此请随时在 Pre 事件中直接更新 Target 上的字段。

    trEntity = (Entity)context.InputParameters["Target"];
    trEntity["field_a"] = null;
    

    【讨论】:

    • 谢谢乔希。所以你认为因为它是 Pre Create 并且我正在使用该服务来更新我收到错误?我会尝试改用trEntity["field_a"] = null;
    • 效果很好。我还有一个问题是我需要获取一个 EntityReference 属性的值。当我使用var tType = (EntityReference)trEntity.Attributes["trTypeId"]; var trType = tType.Name; 时,我没有得到值并且总是得到空值。请指教。
    • 我可以得到 EntityReference 的 id 但不能得到名称。所以下面的代码工作正常:var tType = (EntityReference)trEntity.Attributes["trTypeId"]; var trType = tType.Id;
    • 查找和实体引用总是很有趣。有时名字在那里,有时却不在。如果您在数据库中查找,查找的名称有时会直接与父记录(客户和有关查找字段)一起存储,但通常不是。如果需要名称,最安全的方法是使用服务检索相关实体的名称属性。
    • 在使用服务时使用预创建操作出现错误。所以下面的代码不起作用:tr = (Entity)service.Retrieve("EntityX", trGuid, new ColumnSet(new string[] { "trTypeId" })); tType = (EntityReference)tr.Attributes["trTypeId"]; trType = tType.Name;
    【解决方案2】:

    您是如何创建服务的? 当您尝试更新当前事务之外的记录时也会发生这种情况,即使用手动创建的 OrganizationServiceProxy 而不是使用 IOrganizationServiceFactory.CreateOrganizationService 提供的。

    【讨论】:

    • 我正在使用 Developer toolkit 并按如下方式创建服务:IPluginExecutionContext context = localContext.PluginExecutionContext; IOrganizationService service = localContext.OrganizationService;**
    猜你喜欢
    • 1970-01-01
    • 2015-04-01
    • 1970-01-01
    • 2012-03-01
    • 1970-01-01
    • 2015-12-31
    • 1970-01-01
    • 2019-04-22
    • 2019-04-09
    相关资源
    最近更新 更多