Search code examples
c#mappingdynamics-crm-2011dynamics-crmdynamics-crm-4

Programmatically create an attribute map for custom fields on the lead/contact entities


So I have a CRM integration that adds two fields to the lead and contact. An integer and a Boolean. When we convert a lead into a contact I want those custom fields from the lead to carry over to the new contact. We have over 700 instances using our product so this needs to be a programmatic solution. And thats where the problem lies. I haven't been able to wrap my head around the CreateAttributeMappings class and was hoping someone here could enlighten me and show me where I'm being dumb...

Right now I've got something like this:

var parentEntityMapId = new v4.Sdk.Lookup();
parentEntityMapId.name = "lead";
parentEntityMapId.Value = Guid.NewGuid();

var entityMapId = new v4.Sdk.Lookup();
entityMapId.name = "contact";
//entityMapId.Value = new Guid("608861bc-50a4-4c5f-a02c-21fe1943e2cf");
entityMapId.Value = Guid.NewGuid();

var attributeMapId = new v4.Sdk.Key();
attributeMapId.Value = Guid.NewGuid();

var attributeMap = new v4.SdkTypeProxy.attributemap();
attributeMap.attributemapid = attributeMapId;
attributeMap.entitymapid = entityMapId;
attributeMap.sourceattributename = fieldNameFrom;
attributeMap.targetattributename = fieldNameTo;
//parentEntityMapId.Value = new Guid("DC6574CB-92CE-446C-A5D6-885A75107D52");
attributeMap.parentattributemapid = parentEntityMapId;

var targetAttributeMap = new v4.SdkTypeProxy.TargetCreateAttributeMap();

targetAttributeMap.AttributeMap = attributeMap;

var attributeMapCreateRequest = new v4.SdkTypeProxy.CreateRequest();
attributeMapCreateRequest.Target = targetAttributeMap;

var response = this.CrmService.Execute(attributeMapCreateRequest);

However this gives me this error message:

0x80046203 Invalid mapping. Either an attribute is not mappable, or attributes are of different types, or the size of the target attribute is smaller than the size of the source attribute. Platform

Any help or insight you could give me would be greatly appreciated.


Solution

  • Finally figured it out. Need to pull the existing entity map and then just add the two fields and send the request. Something so simple was so frustrating...

        public void CreateAttributeMapping(string fieldNameFrom, string entityNameFrom, string fieldNameTo, string entityNameTo)
        {
            var entityMap = Retrieve("entitymap", new List<FilterCriteria> { new FilterCriteria("targetentityname", entityNameTo), new FilterCriteria("sourceentityname", entityNameFrom) }, null);
    
            var entityMapId = new v4.Sdk.Lookup();
            entityMapId.name = entityMap.GetKeyName();
            entityMapId.Value = entityMap.GetKeyValue();
    
            var attributeMap = new v4.SdkTypeProxy.attributemap();
            attributeMap.entitymapid = entityMapId;
            attributeMap.sourceattributename = fieldNameTo;
            attributeMap.targetattributename = fieldNameFrom;
    
            var targetAttributeMap = new v4.SdkTypeProxy.TargetCreateAttributeMap();
    
            targetAttributeMap.AttributeMap = attributeMap;
    
            var attributeMapCreateRequest = new v4.SdkTypeProxy.CreateRequest();
            attributeMapCreateRequest.Target = targetAttributeMap;
    
            var response = this.CrmService.Execute(attributeMapCreateRequest);