Monday, July 4, 2011

How to update the full-name format in CRM

There are three special entities (contact, lead, user) which auto-generate the full name attribute based on a combination of first, middle and last name attributes. Other entities can also have a full-name attribute as explained in this post.  The format for the full-name is configurable under Settings –> Administration –> System Settings:
selectfullname
However, updating the format will not retro-actively update the full-name field for existing records so if your organization has the need to update the full-time format, you will need some manual intervention to make sure existing records abide to the new format. There are some posts suggesting to execute SQL scripts to update the existing records; however, SQL scripts are unsupported and also not possible in CRM Online. You just need to execute the following code which will take care of updating existing records to conform to the current full-name format in your organization:

private static void UpdateFullNameFormat(IOrganizationService service)
  1. {
  2.     UpdateFullName(Contact.EntityLogicalName, service);
  3.     UpdateFullName(SystemUser.EntityLogicalName, service, true);
  4.     UpdateFullName(Lead.EntityLogicalName, service);
  5. }
  6.  
  7. private static void UpdateFullName(string entityName, IOrganizationService service, bool checkSystemUsers = false)
  8. {
  9.     QueryExpression query = new QueryExpression(entityName);
  10.     EntityCollection coll;
  11.     query.PageInfo.PageNumber = 1;
  12.     if (checkSystemUsers)
  13.     {
  14.         // Skip the SYSTEM and INTEGRATION built-in users which cannot be modified
  15.         query.Criteria.AddCondition(new ConditionExpression("calendarid", ConditionOperator.NotNull));
  16.     }
  17.     query.ColumnSet = new ColumnSet("lastname", "middlename", "firstname");
  18.     do
  19.     {
  20.         coll = service.RetrieveMultiple(query);
  21.         foreach (Entity e in coll.Entities)
  22.         {
  23.             service.Update(e);
  24.         }
  25.         query.PageInfo.PageNumber++;
  26.     }
  27.     while (coll.MoreRecords);
  28. }

Note that the only thing that I am doing is executing an Update request for each record, without modifying any attribute but it will force the full-name field to be re-calculated based on the new format. When it is so simple to correct it in a supported manner I wonder why complicate life with SQL scripts!

Note that I am using PageInfo because there might be more that 5000 records of each entity so the query results are paged. Thanks Andriy for pointing it out!

3 comments:

  1. Hello Gonzalo, It seems that you forgot about paging. In the case you have more then 5000 contacts or leads - only first 5000 would be updated.

    ReplyDelete
  2. Thanks Andriy, I totally forgot that! I updated the code accordingly!

    ReplyDelete
  3. Hi i’m quit new in CRM 2011 (online). I have the same ‘problem’ and want to update the full names to (last, first middle). This script seems to be the solution. But how do I run it?

    ReplyDelete