Friday, April 4, 2008

Increasing the length of a CRM 3.0 custom entity nvarchar field

Recently, our CRM users asked that the length of a custom field be increased. This seemed like a simple task, however, after a nvarchar field has been created, CRM gives you the ability to decrease the size, but not increase it. To add to the problem, the field was the entity’s primary attribute so it could not be deleted. So, we were left with a few options.
A. Create a new field with the new length, transfer all existing data to the new field, and hide the existing field from users.
B. Back up all data, delete the entity, create a new one, and import all of the archived data.
C. Hack the system by going around CRM’s user interface and modifying the field length.

Option C would give us what we were really looking for (and it sounded like the most fun), so I decided to give it a try.

Here are the steps that were attempted (Warning this is un-supported by Microsoft, and is listed here for educational purposes only! It could seriously harm your CRM installation.):

1. Export the entity’s customizations as an XML file.
2. Open the file in your favorite editor and search for the field’s name.
3. Assuming you only work in one language, there are two values that will need to be modified. They are Length and maxlength. Change maxlength to the new value and Length to two times the new value.

<attribute PhysicalName="Ffs_fieldtomodify">

<Type>nvarchar</Type>

<Length>150</Length>

<ValidForCreateApi>1</ValidForCreateApi>

<ValidForUpdateApi>1</ValidForUpdateApi>

<ValidForReadApi>1</ValidForReadApi>

<IsCustomField>1</IsCustomField>

<DisplayMask>PrimaryNameValidForAdvancedFindValidForFormValidForGrid</DisplayMask>

<Description>The name of the custom entity.</Description>

</attribute>


<field name="ffs_fieldtomodify" requiredlevel="required" maxlength="75" format="text">

<displaynames>

<displayname description="Field To Modify" languagecode="1033" />

</displaynames>

</field>


4. Save the XML, import it into CRM, and publish customizations. (At this point, CRM will show the new length but the SQL tables have not been updated. So, if you try to save a value that is too long you will get a SQL exception message. To solve this, continue on.)

5. Modify the field’s length in the ExtensionBase table of the _MSCRM database. Two views will also need to be updated. The views are and Filtered. You can script the view as “Alter to a New Query Editor window”, and then run the script un-modified. This will not change the definition of the view, but will cause it to be re-compiled and pick up the new field length.

6. To get the field length to display correctly in the CRM metadata browser, update the Attribute table in the _METABASE. You can find the correct row by searching for the field’s name. The length needs to be set to two times the new length (the same value as was entered in the XML file).

select * from attribute where [name] = 'ffs_fieldtomodify'



After all of these steps, everything appeared to be working perfectly. Until, I tried to take my CRM client offline. It gave the error message, “The XML passed to the platform is not well-formed XML”. A CRM client trace revealed this:

>Crm Exception Message: Updating Ffs_temp.ffs_fieldtomodify.Length but it's not valid for update. Original value:150. New value:250., ErrorCode: -2147220991

After thinking about this for awhile, I decided to cancel plan C and go back to plan B. It was an interesting challenge; but in the end it was safer and easier to just delete and re-create the entity. However, if you do not use the CRM offline client, you MIGHT be able modify the field lengths without any problems.

No comments: