serving the solutions day and night

Pages

Monday, March 18, 2013

Dynamics CRM Import Solution Failure

Failure Error:- An item with the same key has already been added.

Because of "duplicate field names", there are 2 possible causes.
1)Duplication has occurred between a custom field, and a virtual field

CRM creates an additional attribute in the metadata for the text of the field for certain field types (boolean, picklist and lookup). This attrbiute is not physically stored, and is considered to be a 'virtual' field. The virtaul attribute has the name of the base field + a suffix of 'name'.
For example,
the addresstypecode picklist field  => addresstypecodename virtual field
new_parentcustomerid custom lookup field  => new_parentcustomeridname virtual field.

CRM will let you do this, but the import could fail.

The following script will list of any duplicates, run in the METABASE database

select
    e.name as entity, a.name as attribute, count(*)
from
    attribute a join entity e on a.entityid = e.entityid
group by
    e.name, a.name
having count(*) > 1
2)Duplicate entry in FieldXml.

CRM stores metadata in several places in CRM. One of them is the OrganizationUIBase table in the MSCRM database. Within this table, some data is stored in the FieldXml field. This is an XML document that looks something like this:
<entity name="contact" objecttypecode="10003"><fields>
    <field name="gab_govermentid" requiredlevel="na">
        <displaynames><displayname description="Goverment ID" languagecode="1033" /></displaynames>
    </field>
    <field name="createdon" requiredlevel="na" format="date">
        <displaynames><displayname description="Created On" languagecode="1033" /></displaynames>
    </field>
</fields></entity>
There should only be one field element for each CRM attribute, but the problem I had was that a field element was duplicated. To determine any duplicate fields, run the following SQL in the MSCRM database:
declare @fieldXml nvarchar(max), @otc int, @idoc int
declare curEntity cursor fast_forward for
select ObjectTypeCode, FieldXml from OrganizationUIBase --where InProduction = 1
open curEntity
fetch next from curEntity into @otc, @fieldXml
while @@fetch_status = 0
begin
EXEC sp_xml_preparedocument @idoc OUTPUT, @fieldXml
if exists ( select name as field from OpenXML(@idoc, '/entity/fields/field') WITH (name nvarchar(64)) group by name having count(*) > 1 )
select @otc as otc, name as field, count(*) as occurences
from OpenXML(@idoc, '/entity/fields/field') WITH (name nvarchar(64))
group by name
having count(*) > 1
EXEC sp_xml_removedocument @idoc
fetch next from curEntity into @otc, @fieldXml
end
close curEntity
deallocate curEntity

This will list the objecttypecode and attribute name of any duplicates. If you have duplicates, the resolution is to export the customisations for the relevant entities, delete any duplicate field elements from the customisation xml, then reimport the xml and publish the entities.

No comments: