1

I am trying to map using AutoMapper.
Source object is C# object

public class Source
    {
        public string comment { get; set; }
        public string Subject { get; set; }
        public Account Account{ get; set; }        
    }

public class Account
        {
            public string FirstName{ get; set; }
            public string LastName{ get; set; }
        }

My destination is a CRM entity named crm_destination which has fields like comment , Subject but it also has a LookUp field account of type Account.

But I don't know how to map LookUp field.

Following is my Automapper

AutoMapper.Mapper.CreateMap<Source, Destinaetion>()
               .ForMember(dest => dest.comment, opt => opt.MapFrom(src => src.comment))
               .ForMember(dest => dest.account, opt => opt.MapFrom(src => src.account));

.ForMember(dest => dest.account, opt => opt.MapFrom(src => src.account)) is throwing error of type mismatch..

basically my problem is I don't know how to map LookUp field where CRM entity is destination.

Daryl
  • 18,592
  • 9
  • 78
  • 145
user2739679
  • 827
  • 4
  • 14
  • 24

3 Answers3

1

Lookups are stored as EntityReferences in CRM. So you'll need to convert your account to an EntityReference.

I have never used AutOMapper, but what Nicknow suggests makes sense. Just call the ToEntityReference() method on Entity.

Daryl
  • 18,592
  • 9
  • 78
  • 145
1

You need to use a custom resolver in AutoMapper. In your custom resolver you'll need to query CRM to get the ID of the account record in that matches your Account object and return an EntityReference to this object.

Nicknow
  • 7,154
  • 3
  • 22
  • 38
-2

I solved it. Lookup creates a relationship. my lookp created a relationship name cxrm_account_cxrm_source_account
so instead of .ForMember(dest => dest.account, opt => opt.MapFrom(src => src.account))
I did

.ForMember(dest => dest.cxrm_account_cxrm_source_account, opt => opt.MapFrom(src => src.Account))

now it is working

user2739679
  • 827
  • 4
  • 14
  • 24
  • How do you get the `AccountID` for Dynamics CRM? Your `Account` object doesn't have a `Guid` but you need a `Guid`. The change above doesn't match to the code in your question, you need to edit something here. – Nicknow Mar 26 '14 at 23:49
  • I don't need to get Account ID. It generates a new record (Account) with new GUID. I need to get GUID only if I need to check whether record exists or not. – user2739679 Mar 27 '14 at 11:14
  • I don't know why you gave -1 to the answer but the above solution is working for me.. – user2739679 Mar 27 '14 at 11:15