Note: the content of this post is similar to this post from Dave Berry but I wanted to approach the same argument with some example code and tips.
Dynamics CRM allows to create fields with the following data types:
Except for "Single Line of Text" and "Multiple Lines of Text" (both use string), each one uses a different underline data type in .NET, some of these data types are nullable, some are not.
What means nullable and why we need to care about this?
Let's start with an example: In our CRM we have two records, the first record has all the fields filled with a value, in the second one all the fields are empty. When we use GetAttributeValue and there is a value, the method (fairly) returns the value. But what happens with our empty record? The response is "depends".
GetAttributeValue uses Generics, so we can choose to get a nullable type or not:
The next table is a summary:
For the types that can hold null, we use GetAttributeValue and after check if it's null or not:
If we are in a loop and we need to do a sum of an int field, the default value (0) is ok, so we can just do
DateTime is a particular case, it can't hold null but the default value (MinValue = 01/01/0001) can't be a valid CRM value (as happens with bool and numeric fields) so we can do the following check:
Now you are a Dynamics CRM True Survivor!
Dynamics CRM allows to create fields with the following data types:
Except for "Single Line of Text" and "Multiple Lines of Text" (both use string), each one uses a different underline data type in .NET, some of these data types are nullable, some are not.
What means nullable and why we need to care about this?
Let's start with an example: In our CRM we have two records, the first record has all the fields filled with a value, in the second one all the fields are empty. When we use GetAttributeValue and there is a value, the method (fairly) returns the value. But what happens with our empty record? The response is "depends".
GetAttributeValue uses Generics, so we can choose to get a nullable type or not:
In this case, if the value is null (for a Boolean/Two Options field means that no value is set) the first variable will contains false, the second will contains null.
bool boolean = entity.GetAttributeValue<bool>("new_boolean");
bool? booleanNullable = entity.GetAttributeValue<bool?>("new_boolean");
The next table is a summary:
CRM Type | .NET Type | can hold null? | default value |
---|---|---|---|
Single Line of Text | string | Yes | |
Option Set | OptionSetValue | Yes | |
Two Options | bool | No | false |
Image | byte[] | Yes | |
Whole Number | int | No | 0 |
Floating Point Number | double | No | 0.0 |
Decimal Number | decimal | No | 0 |
Currency | Money | Yes | |
Multiple Lines of Text | string | Yes | |
Date and Time | DateTime | No | DateTime.MinValue |
Lookup | EntityReference | Yes |
For the types that can't hold null we need to ask ourselves: "The default value is enough for the requirement?"
EntityReference lookupRef = entity.GetAttributeValue<EntityReference>("new_lookupid");
if (lookupRef == null) {
// no value set
} else {
// we have a value
}
If we are in a loop and we need to do a sum of an int field, the default value (0) is ok, so we can just do
but if we are doing a multiplication we need to skip the null values, so we use int?
int totalSum = 0;
foreach (Entity row in RowCollection.Entities) {
int number = entity.GetAttributeValue<int>("new_wholenumber");
totalSum+=number;
}
otherwise with a null value our totalMulty variable will be 0.
int totalMulty = 0;
foreach (Entity row in RowCollection.Entities) {
int? number = entity.GetAttributeValue<int?>("new_wholenumber");
if (number != null) {
totalMulty*=number;
}
}
DateTime is a particular case, it can't hold null but the default value (MinValue = 01/01/0001) can't be a valid CRM value (as happens with bool and numeric fields) so we can do the following check:
Practically when we don't use the nullable form, Dynamics CRM is doing the following:
DateTime dateTime = entity.GetAttributeValue<DateTime>("new_datetime");
if (dateTime == DateTime.MinValue) {
// no value set
} else {
// we have a value
}
The combination of the nullable form and the GetValueOrDefault can be useful in some scenarios. Let's say that we need to do a data migration to an external system, but if the source decimal Quantity is null, the target decimal Quantity must be -1.
double floating = entity.GetAttributeValue<double>("new_floating");
// equals to
double floating = entity.GetAttributeValue<double?>("new_floating").GetValueOrDefault();
In this way we deal automatically the null values and they are ready for the target system.
decimal quantity = entity.GetAttributeValue<decimal?>("new_quantity").GetValueOrDefault(-1);
Now you are a Dynamics CRM True Survivor!