Knowledge found and lost while working with Microsoft Dynamics CRM
RSS icon Home icon
  • Using Metadata to make intelligent programming decisions

    Posted on November 11th, 2007 mitch Print Print No comments

    This morning I released a small utility to update additional birthday information for CRM Contacts.

    During the process of creating this utility, I came upon an interesting design decision.

    The original solution proposed by Microsoft was to create an Integer Attribute to track the birthday month for a Contact.  I felt that it was a little more friendly to use a String Attribute to track the name of the birthday month, instead.

    But, since I wasn't sure which option the user would select, I wanted to make my CRMUpdateBirthdayList utility accommodate both possibilities.  To make this a reality, I turned to the CRM Metadata service to retrieve information about the Contact Entity using the following code:

       1: private EntityMetadata _selectedEntity = 
       2:          _metaDataService.RetrieveEntityMetadata(
       3:          EntityName.contact.ToString(),
       4:          EntityFlags.IncludeAttributes);
    This code retrieves all of the attribute information for the Contact Entity. 
    I then used the following loop to locate the Birthday Month Attribute
    then stored it's data type:
       1: for (int i=0; i<_selectedEntity.Attributes.Length; i++)
       2: {
       3:     if (_selectedEntity.Attributes[i].Name.ToString() == BirthdayMonthAttribute)
       4:     {
       5:         birthdayMonthType = _selectedEntity.Attributes[i].Type.ToString().ToLower();
       6:     }
       7: }

    Once I had obtained the data type, I used the following code to create the appropriate properties that would allow me to update the Contact record:

       1: switch (birthdayMonthType)
       2: {
       3:     case "integer":
       4:         CrmNumber n = new CrmNumber();
       5:         n.Value = d.Month;
       6:         CrmNumberProperty birthDateMonth2 = new CrmNumberProperty();
       7:         birthDateMonth2.Value = n;
       8:         birthDateMonth2.Name = BirthdayMonthAttribute;
       9:  
      10:         // Add properties to ArrayList.
      11:         arrProps.Add(birthDateMonth2);
      12:         break;
      13:     case "string":
      14:         StringProperty birthDateMonth = new StringProperty();
      15:         birthDateMonth.Value =
      16:            CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(d.Month);
      17:         birthDateMonth.Name = BirthdayMonthAttribute;
      18:  
      19:         // Add properties to ArrayList.
      20:         arrProps.Add(birthDateMonth);    
      21:         break;
      22:     default:
      23:         break;
      24: }

    In a nutshell, the above code segment will check the data type for the Birthday Month attribute and if it is an Integer, it create a property with the number of the birthday month.  Otherwise, if it is a String, it will create a property containing the name of the birthday month.

    While it would have been quicker to just have the user provide the data type inside the .config file, I think it makes much more sense to make your application make intelligent decisions based on the available data.  Not only does this cut down on potential errors introduced by the user, but it also reduces the maintenance of your application since it has taken into account the possibility that at some point in the future the administrator may change the way they record the birthday month.

    Customization, Dynamics CRM
    1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
    Loading ... Loading ...
    1,556 views
  • Birthdays in CRM: Revisited

    Posted on November 11th, 2007 mitch Print Print 4 comments

    Last year Phil Richardson and Ben Vollmer both posted solutions that would enable us to better track birthdays within CRM.  In May of 2007, the solution was added to the CRM How To series.

    To review the situation we find that while Microsoft provided us with a Birthday Attribute on the Contact Entity it's really hard to perform a search for birthdays because we need to know at what point the future will a contact's birthday occur again.

    Their solution is to create two custom fields that hold the birthday month and the birthday day.  The addition of these new Attributes allows us to perform Advanced Find operations such as: "Show me all birthdays in December", so we can get birthday cards into the mail.

    The How To article provides instructions on how to add the Attributes in question and provides the JavaScript code to populate those attributes whenever someone changes a Contact's birthdate.

    I would like to suggest a small change to that solution:  Instead of tracking the birthday month by number, I wanted to use the month's name.  You basically follow the instructions in the How To article but when you start to create the Birthday Month Attribute, make it a nvarchar(10) instead of an integer.

    Use the following JavaScript code in the Birthdate Attribute's OnChange event to populate the name of the month in the Birthday Month Attribute:

       1: if (crmForm.all.birthdate.DataValue != null)
       2: {
       3:  var monthList = new Array(
       4:  "January","February", "March",
       5:  "April", "May", "June",
       6:  "July", "August", "September",
       7:  "October", "November", "December");
       8:  
       9:  var bd = crmForm.all.birthdate.DataValue;
      10:  crmForm.all.new_birthdaymonth.DataValue = monthList[bd.getMonth()];
      11:  crmForm.all.new_birthdaymonth.ForceSubmit = true;
      12:  crmForm.all.new_birthdayday.DataValue = bd.getDate();
      13:  crmForm.all.new_birthdayday.ForceSubmit = true;
      14: }
      15: else
      16: {
      17:  crmForm.all.new_birthdaymonth.DataValue = null;
      18:  crmForm.all.new_birthdaymonth.ForceSubmit = true;
      19:  crmForm.all.new_birthdayday.DataValue = null;
      20:  crmForm.all.new_birthdayday.ForceSubmit = true;
      21: }

     

    So that takes care of all new birthdays added to the system.  But what about the birthdays that already exist?

    Well, that took a bit more work since we need to perform an update to the database for each Contact record containing a birthday.

    Attached to this article you will find a small utility that will update the new birthday month and birthday day Attributes you added based on the Contact's birthdate. This utility can be run at any time to update any CRM Contact that contains a Birthdate.

    Download: CRMUpdateBirthdayList

    Review the enclosed CRMUpdateBirthdayList-Readme.doc file for configuration and usage instructions.

    This utility can be run from either the CRM Server or from a workstation on which the CRM Outlook client has been installed.

    Misc
    1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
    Loading ... Loading ...
    1,584 views