Jul 04 2011
CRM 4.0 0×80040216 An unexpected error occurred
A client was unable to update a few contacts – it was producing an error over and over. Attempting to reproduce the issue for the affected records, we came across this particular error:
When trying to set an address as a ‘Primary Mailing Address’ In the More Address section of a CRM contact, the above error takes place. It wasn’t an issue for most of any other contact.
A trace revealed the following information:
Error: Exception has been thrown by the target of an invocation.
Error Message: Exception has been thrown by the target of an invocation.
Source File: Not available
Line Number: Not available
Request URL: http://crm
Stack Trace Info: [InvalidCastException: Specified cast is not valid.]
at Microsoft.Crm.BusinessEntities.AddressTrigger.Update(GUID)
Further, the event log was showing errors related to this. It seemed to be attempting to call a step in one of our plugins that clearly was non-existent.
We decided to investigate in SQL, and found that there were contacts with a missing AddressNumber=1 field in the CustomerAddress table. In our case, AddressNumber fields were stamped for our records; however, they were stamped as AddressNumber=2. If you compare a broken record with one that works, you’ll see something similar with the one described below:
It was time to resolve the issue. We ran a few queries to do this.
The first query returns a count of how many records have the missing AddressNumber=1 field:
select COUNT(*)
from ContactBase c left outer join
CustomerAddressBase a on c.ContactId=a.ParentId and a.AddressNumber=1
where a.CustomerAddressId is null
The second query returns a column listing of all those records (you can add more columns if you wish):
select contactid, fullname, c.createdon, c.createdbyname
from Contact c left outer join
CustomerAddressBase a on c.ContactId=a.ParentId and a.AddressNumber=1
where a.CustomerAddressId is null
There are two ways to resolve this issue – one of which is unsupported by Microsoft. Create a backup of your MSCRM databases before attempting this.
Supported method:
Recreate the record, including the addresses, and merging the broken one with the new one.
Unsupported method:
begin tran
update CustomerAddress
set AddressNumber=1
where AddressNumber = 2 and ObjectTypeCode=2 and ParentId not in (
select ParentId from CustomerAddress where AddressNumber =1 and ObjectTypeCode = 2)
commit tran
I just want to reiterate that this method is completely UNSUPPORTED. However, this is an easier method if you have a number of records you’d rather not recreate/merge as it can be extremely time consuming.
Both methods will end up stamping the AddressNumber field correctly, and as a result, will be able to be updated with no issue from then on.
Comments Off