Tuesday, March 7, 2017

Dynamics CRM 2016: Updating Lookup field value using CRM REST API

This article aims to guide you about the things to consider when making a REST API call to insert/ update a value in the lookup field in CRM.

Consider your CRM instance is having following two entities:

1) account >> This entity comes default with CRM instance
2) new_customer >> Custom entity having various fields, but we are typically interested in "new_account" which is a lookup to the account entity.



    public class Account
    {
        public string name { get; set; }

        public Guid accountid { get; set; }

    }

using Newtonsoft.Json;
public class Customer
    {
        [JsonProperty(PropertyName = "new_accountid@odata.bind")]
        public string new_accountid { get; set; }        

        public string new_customerRefNo { get; set; }        
    }

Notice, the JsonProperty in Customer class. This property needs to be set for all lookup fields in case if they are intended to be updated/ inserted using REST API. Name of this property should be set to - "[lookupfieldname]@odata.bind". In above case, [lookupfieldname] is "new_accountid"

Now you can populate values in Customer instance, and can send them in your REST API calls which will update lookup fields during PUT/ POST calls.