如何使用C#在CRM中的新实体中设置查找字段的值 [英] How to set value of lookup field in a new entity in CRM using C#

查看:76
本文介绍了如何使用C#在CRM中的新实体中设置查找字段的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过以下代码更新CRM中的查找"字段.

I am trying to update a Lookup field in CRM via the following code.

  • 创建一个新实体 Person
  • 人员包含来自另一个实体医院
  • 的查找字段 HospitalOfBirth
  • 要插入的值:hospitalName

我参考了在线文章,并在下面起草了代码.

I referenced from online articles and drafted my code below.

Entity Person = new Entity("Person");

//do I set the value of the field by the following?
Person.Attributes[Person.HospitalOfBirth] = hospitalName;

Person.Attributes[Person.HospitalOfBirth] = new EntityReference("Hospital", Person.Id);

Person.Id = helper.GetOrganizationService().Create(Person);

我可以知道为 Person 中的查询字段 HospitalOfBirth 分配一个值,并使用医院名称对其进行更新吗?

May I know to assign a value to the lookup field HospitalOfBirth in Person, and update it with hospitalName?

推荐答案

您无法通过显示名称文本设置查找值,需要使用查找ID(外键)来实现.

You cannot set a lookup value by display name text, you need lookup Id (Foreign key) to achieve it.

如果您只具有 hospitalName ,而没有 hospitalId ,则必须通过执行 HospitalId 实体查询GUID.使用 FetchXml QueryExpression ,通过传递 hospitalName 过滤器> RetrieveMultiple .

If you just have the hospitalName and not the hospitalId, then you have to query the Hospital entity for the GUID by doing RetrieveMultiple using FetchXml or QueryExpression by passing hospitalName filter.

Person.Attributes[Person.HospitalOfBirth] = new EntityReference("Hospital", Hospitalid);

这篇关于如何使用C#在CRM中的新实体中设置查找字段的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆