davidyack / Xrm.Tools.CRMWebAPI

This is an API helper for working with the Common Data Service (CDS) Web API
MIT License
143 stars 73 forks source link

How to work with entity reference (lookup)? #91

Closed msymer closed 4 years ago

msymer commented 4 years ago

I would like to update attribute of the account entity. The attribute is lookup to another entity. How can I do it? Something like this:

dynamic updateObject = new ExpandoObject();
updateObject.lookupattributeid= the_entity_reference;
dynamic updateResult = await api.Update("accounts", new Guid("1111111-2222-3333-4444-55555555"), updateObject, false);
msymer commented 4 years ago

Ok i got it.

dynamic updateObject = new ExpandoObject();

Guid accountID = new Guid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx");    
var parAcctIndexer = updateObject as IDictionary<string, Object>;
parAcctIndexer["parentaccountid@odata.bind"] = "/accounts(" + accountID.ToString() + ")";

dynamic updateResult = await api.Update("accounts", new Guid("1111111-2222-3333-4444-55555555"), updateObject, false);