简体   繁体   English

Microsoft Dynamics CRM设置相关实体​​的字段值

[英]Microsoft Dynamics CRM set field value of related entity

I am using Microsoft Dynamics CRM Online. 我正在使用Microsoft Dynamics CRM Online。

I have a working plug-in starting on the Create Message (post-operate) of an entity called (let's simplify) "entity1". 我有一个工作的插件,该插件始于名为(让我们简化)“ entity1”的实体的创建消息(后操作)。 One of the features of this plug-in is that it determines a certain value. 该插件的功能之一是确定某个值。 Let's call this one "importantValue". 我们将此称为“ importantValue”。 The plug-in also creates a relation between "entity1" and another entity (let's simplify again) "entity2", and populates the corresponding lookup field in "entity1". 该插件还在“ entity1”和另一个实体(再次简化)“ entity2”之间创建一个关系,并在“ entity1”中填充相应的查找字段。

All of this is working just fine. 所有这些都很好。 However, I also want the plug-in to set a field of "entity2" (called "samplefield") to the value of "importantValue". 但是,我还希望该插件将“ entity2”字段(称为“ samplefield”)设置为“ importantValue”的值。 I know how to retrieve the GUID of the related record of entity2, but I can't get the plug-in to update this (already existing) record. 我知道如何检索Entity2相关记录的GUID,但是我无法获取该插件来更新此(已存在)记录。

This is the part of the code making problems. 这是代码制作问题的一部分。 I already retrieved the GUID "entity2Guid" and filled importantValue (it's a string). 我已经检索了GUID“ entity2Guid”并填充了重要值(它是一个字符串)。 My IOrganizationService is called "service". 我的IOrganizationService称为“服务”。

 Entity entity2 = new Entity("new_entity2");
 entity2.Id = new Guid (entity2Guid);
 entity2["new_samplefield"] = importantValue;
 service.Update(entity2);

What am I doing wrong? 我究竟做错了什么? Thanks in advance! 提前致谢!

I think if you are updating a custom field you will have to add the field name to the entities attribute collection. 我认为,如果您要更新自定义字段,则必须将字段名称添加到实体属性集合中。 Try updating the entity this way: 尝试通过以下方式更新实体:

if (entity2.Attributes.ContainsKey("new_samplefield"))
    entity2["new_samplefield"] = importantValue;
else
    entity2.Attributes.Add("new_samplefield", importantValue);

Maybe if you know this attribute will never be included in the attribute collection you can skip the if statement and always add it. 也许如果您知道此属性将永远不会包含在属性集合中,则可以跳过if语句并始终添加它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM