简体   繁体   中英

How to pass an EntityReference to add attribute value on a lookup field in Microsoft Dynamics 365 CRM

Entity contact = new Entity("contact");
contact.Attributes.Add("fullname", "h api test");
contact.Attributes.Add("emailaddress1", "hh@devenv1.local");
contact.Attributes.Add("telephone1", "1");

contact.Attributes["parentcusotmerid"] = new EntityReference("Organization", );

Guid contactId = m_OrgServ.Create(contact);
Console.WriteLine(contactId);

The lookup field I want to set

The logicalname of the lookup field is parentcusotmerid , and

m_OrgSerc.create 

is basically

Service.create

I am setting attribute values for the fields, it works fine for normal text boxes where I am entering values, however for lookup values it doesn't work. I know lookup fields are of type EntityReference , so I need to know the LogicalName of the entity the lookup is pointing and the Id of the record.

I have tried it but its asking for the GUID of the Organization field now, so I'm not sure if I am going about it the right way?

You cannot set "parentcustomerid" to organization. It's special reference field that takes either Account or Contact entity reference as parameter.

If you want to set it you go like this

contact.Attributes["parentcusotmerid"] = new EntityReference("account", Guid.NewGuid());

or

contact.Attributes["parentcusotmerid"] = new EntityReference("contact", Guid.NewGuid());

where Guid.NewGuid() is Guid of your Account or Contact that you want to reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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