Sometimes there is a long gap between Part 1 and Part 2 of my blog series.
But its just because I want to use the concepts in my projects or POC’s before sharing them.
The Part 1 of this series is here: Click here
Part 2 Goes here, the new features which can benefit you!
The new null conditional operator {?.}:
You can do multiple layer null check in one condition, the below snippet checks EntityCollection is not null
and then checks Entities is not null:
EntityCollection entityCollection = service.RetrieveMultiple(query);
if (entityCollection?.Entities != null)
{
// Processing logic
}
What’s more you can also do null check and also match conditions , the below snippet checks EntityCollection is not null,
then checks Entities is not null and also matches entity count condition:
EntityCollection entityCollection = service.RetrieveMultiple(query);
if (entityCollection?.Entities?.Count > 0)
{
// Processing logic
}
Nameof Operator:
This operator helps you to get the name of a particular type and can be used for handling in exceptions and tracing:
For example:
var entity = service.Retrieve(“account “, new Guid(“B18DFC86-4776-E511-80FC-3863BB343C90”), new ColumnSet(true));
Console.WriteLine(“Var in entity is of type: “ + nameof(entity));
Console.WriteLine(“Attribute collection in entity is of type: “ + nameof(entity.Attributes));
Gives the output:
Exception filters:
In your CRM exception handling you can filter by messages now:
try
{
throw new Exception(“CRM Exception”);
}
catch (InvalidPluginExecutionException ex) when (ex.Message == “CRM Exception”)
{
//control will not come in this catch block
}
catch (Exception ex) when (ex.Message == “CRM Exception”)
{
//control will come in this catch block
}
Hope it helps and Happy CRMing!