Search code examples
c#apidynamics-crmxrm

How to insert Country data to the MS Dynamics CRM Lead entity


How to insert country data (Address1_Country) to the MS Dynamics CRM Lead entity?

I am using the Xrm.cs and Microsoft.Xrm.Sdk NuGet package. I would like to add a country data along the new Lead entity that I am creating.

I have no problem creating a new Lead entity, like:

enter image description here

And I am adding a country data along other data to the Lead entity, like:

enter image description here

But the country data does not get passed further to the CRM:

enter image description here

I have NO idea what is wrong. The Address1_Country data is string type. So I have tried also data like "DE" and it does not work.

However it is true that country data here is a special data - lookup one. I am sure that this and my problem is somehow connected. Maybe I should fill the Id or Guid in? But Usually in Xrm.cs those data are also Guid data type .. So I am a bit lost here.

How can I fill the country data in CRM via C# API?


Solution

  • If Country is a lookup type, you need to set there an EntityReference.

    Address1_Country = new EntityReference("entityLogicalName", Guid);
    

    So you need to Retrieve the Guid of the country you want to set there (If you are going to filter by name you need a RetrieveMultiple) and set that Id there.

    This is the approach with Late Bound, I think you are using Early Bound, you only need to parse this to Early.