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

4.5k Views Asked by At
Entity contact = new Entity("contact");
contact.Attributes.Add("fullname", "h api test");
contact.Attributes.Add("emailaddress1", "[email protected]");
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?

1

There are 1 best solutions below

3
minohimself On BEST ANSWER

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