簡體   English   中英

如何使用 C# 在 CRM 中的新實體中設置查找字段的值

[英]How to set value of lookup field in a new entity in CRM using C#

我正在嘗試通過以下代碼更新 CRM 中的查找字段。

  • 創建一個新實體Person
  • Person 包含來自另一個實體Hospital的查找字段HospitalOfBirth
  • 要插入的值:hospitalName

我從在線文章中引用並在下面起草了我的代碼。

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分配一個值,並使用 hospitalName 更新它嗎?

您不能通過顯示名稱文本設置查找值,您需要查找 Id(外鍵)來實現它。

如果您只有hospitalName而不是hospitalId ,那么您必須通過使用FetchXmlQueryExpression通過傳遞hospitalName過濾來查詢Hospital實體的 GUID。

Person.Attributes[Person.HospitalOfBirth] = new EntityReference("Hospital", Hospitalid);
Entity Person = new Entity("Person");
Person.Attributes[Person.HospitalOfBirth] = new EntityReference("Hospital", Person.Id);
Person.Id = **<<Guid of the Person record you need to update>>**
helper.GetOrganizationService().Update(Person);

您需要在實體 object 中提供要更新的記錄的 guid。

希望這可以幫助 !!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM