11 Nov
Posted by: mitch in: Customization, Dynamics CRM
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.
Leave a reply