Archive for March, 2010

CRM 4.0 Create customeraddress 0x80040216 An unexpected error occurred

Thursday, March 4th, 2010

When I tried to add more address into an account in CRM, I keep getting this error with error message that doesn’t really help you nail down the problem. If you face the same error or you want to know how to create customeraddress using CRM 4.0 SDK DynamicEntity, please read through. The following simple code highlight how to add address into an account (You can also do on contact by changing the entity name “account” to “contact”. Please note that the values (i.e. street1, street2, city, …) is already set.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
var newAddress = new DynamicEntity
  {
     Name = NameOfEntity,
     Properties = new PropertyCollection()
  };
 
var prop = newAddress.Properties;
string addressName = street1 + " " + street2 + " " + city;
prop.Add(new StringProperty("name", addressName));
 
prop.Add(new StringProperty("line1", street1));
prop.Add(new StringProperty("line2", street2));
prop.Add(new StringProperty("city", city));
// .... add other details as necessary ....
prop.Add(new StringProperty("fax", fax));
prop.Add(new StringProperty("primarycontactname", contactName));
 
// .... this is the important property that you should add ...                       
prop.Add(new EntityNameReferenceProperty(AttrObjectType, new EntityNameReference("account")));
prop.Add(new LookupProperty(AttrParent, new Lookup("account", parentId)));
 
var targetCreate = new TargetCreateDynamic {Entity = newAddress};
var request = new CreateRequest {Target = targetCreate};
var response = (CreateResponse)CrmServiceUtil.GetCrmService().Execute(request);
return response.id;

Rule of thumb, do not forget to set the ParentId attribute, as well as ObjectTypeCodeId. That should do the trick. The above code uses .NET 3.0.