Search code examples
c#.netdynamics-crmcrmdynamics-crm-webapi

CRMWebAPI - How to work with Entity reference?


I use CRMWebAPI for CRUD operations with CRM. How can I work there with a entity reference?

For example when I want to update parent account attribute on the Account entity, where the parent account attribute is Lookup to another account.

Something like this:

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

Solution

  • Referred this code sample and able to compose this. Please test 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);