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.
Public Function Test() As String
Try
‘ Set up the CRM Service.
Dim token As CrmAuthenticationToken = New CrmAuthenticationToken()
token.AuthenticationType = 0
token.OrganizationName = “E4685-Dev”
Dim service As CrmService = New CrmService()
service.Url = “http://172.20.23.36:5555/mscrmservices/2007/crmservice.asmx”
service.CrmAuthenticationTokenValue = token
service.Credentials = New NetworkCredential(“Administrator”, “CrmAdmin123”, “MSCRMPUNE”)
Dim objDynamicEntity As DynamicEntity = New DynamicEntity()
objDynamicEntity.Name = “customeraddress”
Dim arrlProp As ArrayList = New ArrayList()
Dim addressName As String = “Street 1″ + ” ” + “Street 2″ + ” ” + “SAngola”
Dim Nameprop As StringProperty = New StringProperty()
Nameprop.Name = “name”
Nameprop.Value = addressName
arrlProp.Add(Nameprop)
Dim line1prop As StringProperty = New StringProperty()
Nameprop.Name = “line1”
Nameprop.Value = “street1”
arrlProp.Add(line1prop)
Dim line2prop As StringProperty = New StringProperty()
Nameprop.Name = “line2”
Nameprop.Value = “street2”
arrlProp.Add(line2prop)
Dim cityProp As StringProperty = New StringProperty()
Nameprop.Name = “city”
Nameprop.Value = “city”
arrlProp.Add(cityProp)
‘…. add other details as necessary ….
Dim FaxProp As StringProperty = New StringProperty()
Nameprop.Name = “fax”
Nameprop.Value = “020-12345”
arrlProp.Add(FaxProp)
Dim ContactProp As StringProperty = New StringProperty()
Nameprop.Name = “primarycontactname”
Nameprop.Value = “contactName”
arrlProp.Add(ContactProp)
‘ …. this is the important property that you should add …
Dim objEntityNameReference As EntityNameReference = New EntityNameReference()
objEntityNameReference.Value = “account”
Dim objEntityNameReferenceProperty As EntityNameReferenceProperty = New EntityNameReferenceProperty()
objEntityNameReferenceProperty.Name = “ObjectTypeCodeId”
objEntityNameReferenceProperty.Value = objEntityNameReference
arrlProp.Add(objEntityNameReferenceProperty)
Dim accountLookup As Lookup = New Lookup
Dim accountlookupprop As LookupProperty = New LookupProperty
accountLookup.name = “accountid”
accountLookup.Value = New Guid(“1045C1DE-4E9E-DF11-987A-001AA0C32354”)
accountLookup.type = “account”
accountlookupprop.Name = “ParentId”
accountlookupprop.Value = accountLookup
arrlProp.Add(accountlookupprop)
objDynamicEntity.Properties = CType(arrlProp.ToArray(GetType([Property])), [Property]())
Dim objtargetCreate As TargetCreateDynamic = New TargetCreateDynamic()
objtargetCreate.Entity = objDynamicEntity
Dim objrequest As CreateRequest = New CreateRequest()
Dim objresponse As CreateResponse = New CreateResponse()
objrequest.Target = objtargetCreate
objresponse = CType(service.Execute(objrequest), CreateResponse)
Return objresponse.id.ToString()
Catch ex As SoapException
Catch ex As Exception
End Try
End Function
Still i m getting error.
Please upload the whole code.
Add an Addresses record to the entity and save. This should resolve it.