Quantcast
Channel: Microsoft Dynamics 365 Community
Viewing all articles
Browse latest Browse all 64797

GetAttributeValue demystified

$
0
0
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:

bool boolean = entity.GetAttributeValue<bool>("new_boolean");
bool? booleanNullable = entity.GetAttributeValue<bool?>("new_boolean");
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.
The next table is a summary:

CRM Type.NET Typecan hold null?default value
Single Line of TextstringYes
Option SetOptionSetValueYes
Two OptionsboolNofalse
Imagebyte[]Yes
Whole NumberintNo0
Floating Point NumberdoubleNo0.0
Decimal NumberdecimalNo0
CurrencyMoneyYes
Multiple Lines of TextstringYes
Date and TimeDateTimeNoDateTime.MinValue
LookupEntityReferenceYes
For the types that can hold null, we use GetAttributeValue and after check if it's null or not:

EntityReference lookupRef = entity.GetAttributeValue<EntityReference>("new_lookupid");
if (lookupRef == null) {
// no value set
} else {
// we have a value
}
For the types that can't hold null we need to ask ourselves: "The default value is enough for the requirement?"

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

int totalSum = 0;
foreach (Entity row in RowCollection.Entities) {
int number = entity.GetAttributeValue<int>("new_wholenumber");
totalSum+=number;
}
but if we are doing a multiplication we need to skip the null values, so we use int?

int totalMulty = 0;
foreach (Entity row in RowCollection.Entities) {
int? number = entity.GetAttributeValue<int?>("new_wholenumber");
if (number != null) {
totalMulty*=number;
}
}
otherwise with a null value our totalMulty variable will be 0.

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:

DateTime dateTime = entity.GetAttributeValue<DateTime>("new_datetime");
if (dateTime == DateTime.MinValue) {
// no value set
} else {
// we have a value
}
Practically when we don't use the nullable form, Dynamics CRM is doing the following:

double floating = entity.GetAttributeValue<double>("new_floating");
// equals to
double floating = entity.GetAttributeValue<double?>("new_floating").GetValueOrDefault();
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.

decimal quantity = entity.GetAttributeValue<decimal?>("new_quantity").GetValueOrDefault(-1);
In this way we deal automatically the null values and they are ready for the target system.

Now you are a Dynamics CRM True Survivor!

Viewing all articles
Browse latest Browse all 64797

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>