0

我正在使用Microsoft Dynamics CRM Online。相关实体的Microsoft Dynamics CRM设置字段值

我有一个工作插件开始创建消息(后操作)的实体称为(让我们简化)“实体1”。这个插件的特点之一是它确定了一定的价值。我们称之为“重要价值”。插件还创建“entity1”和另一个实体(让我们再次简化)“entity2”之间的关系,并在“entity1”中填充相应的查找字段。

所有这一切都工作得很好。但是,我还希望插件将“entity2”(称为“samplefield”)字段设置为“importantValue”的值。我知道如何检索entity2的相关记录的GUID,但我无法让插件更新这个(已经存在的)记录。

这是代码制作问题的一部分。我已经检索了GUID“entity2Guid”并填充了重要值(它是一个字符串)。我的IOrganizationService被称为“服务”。

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

我在做什么错?提前致谢!

+0

你会得到什么例外? –

回答

-1

我想如果你正在更新自定义字段,你将不得不将字段名称添加到实体属性集合中。尝试更新的实体是这样的:

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

也许,如果你知道这个属性将永远不会被包含在属性集合中的if语句,并随时添加它,你可以跳过。

+1

'entity2 [“new_samplefield”] = importantValue'是分配值的完美有效方法。如果属性尚未包含在集合中,它将自动添加。 –

相关问题