Search code examples
c#dynamics-crm-4

CrmService.Update method does not work for certain attribute


This is my problem: all I want to do is update a "contact" entity in Crm 4 using the webservice.

This is my code:

CrmService eatupCrmService = CrmInteraction.InitializeCrmService();
contact updatedDelegate = new contact();
CeatupCrmService.Key contactPrimaryKey = new CeatupCrmService.Key();
contactPrimaryKey.Value = delegateId;
updatedDelegate.contactid = contactPrimaryKey;
updatedDelegate.address2_postalcode = delegateDetails.ContactDetailsPhysicalAddressCode;
eatupCrmService.Update(updatedDelegate);

I use the InitializeCrmService() to also retrieve and it works. When updating the address2_postalcode attribute, I get the following error:

"Server was unable to process request."

with the exception Detail\InnerText :

"0x80040216 An unexpected error occurred. Platform".

If I change the code to update another attribute, say I try to update the mobilephone attribute instead of address2_postalcode it works without any exceptions thrown.

As soon as I try to update address2_postalcode then I get that error. The address2_postalcode data type in Crm is nvarchar, and the value it is being assigned (delegateDetails.ContactDetailsPhysicalAddressCode) is of c#'s string type.

Anyone have any idea why this could be happening?


Solution

  • After many attempts at trying to understand why this error was occurring, I finally succeeded.

    When I started working on this project, the client instructed me to test using only a specific contact, because we were working directly on the production environment. (the client does not have a development environment yet)

    After doing some database queries to compare this contact to others (updates were failing only for the test contact) I noticed that my contact's address2_addressid attribute was NULL.

    I then went to CRM under Customization\Customize Entities and opened up the contact entity. Under attributes, I ordered by type and saw that the contact has 3 primarykey attributes: contactid, address1_addressid and address2_addressid.

    The test contact's address1_addressid and address2_addressid fields were NULL and this caused the CRM web service to throw the 0x80040216 An unexpected error occurred. Platform error upon me trying to update any of the address fields.

    I don't know why this contact had those IDs set to NULL, I asked and the person that created the contact had no explanation of how this could have happened. I guess this will remain a mystery, but at least I now have an answer to the error I was getting and I can cater for this in my code.