Use GetValueOrDefault in early bound plugins and say goodbye to catching nulls

When writing plugin code CRM has a very frustrating feature which allows it’s fields to be null.  Note I said this was a feature not a bug 🙂

I believe this was was one of the main reasons in CRM 4 it had it’s own set of variables CrmDateTime, CrmDecimal, CrmFloat, CrmMoney, CrmNumber.  These could hold a null as well as value (I think, it has been a while since I did any CRM 4 development).

Where this really niggles me is when I can’t pass anearly bound field  into a method because the method takes a bool but the early bound field is a ?bool, which basically means it’s a bool that can be null.

So how do we code around this

Late bound

So in previous blog post I have talked about using Entity.GetAttributeValue instead of Entity.Contains

using GetAttributeValue is great because it means I could take all the entity.contains code below

if (entity.Contains("salutation"))
{
     title = ( string)entity.Attributes["salutation"];
}

The GetAttributeValue returns either a null or a default value.  This is great for boolsints etc because instead of null I now get false or 0;

here is the list of default values
Type Return
Numerical (int, decimal, double) 0
Boolean false
DateTime DateTime.MinValue
Guid Guid.Empty

For other variables it will return a null.

Early bound

What if you are using early bound classes and don’t want to add the potential risk of putting in syntax errors in your plugin code by using late bound code.

What are the benefits of early bound code

  • Readability
  • No Syntax errors at runtime
  • casting errors are found at compile time
  • Easier to read, debug, maintain and extend

The main reason I like early bound classes is because it’s easier to understand and I don’t have to keep looking up and typing the name of the CRM fields.  It also moves most of the errors into compile time (syntax and casting errors), which means it’s less likely these errors will get through to the customers.

GetValueOrDefault to the rescue

If you want to convert the an early bound field into simple attribute then you should use GetValueOrDefault, which

The reason it’s a good idea to use this is because if you casted it to a bool or int you are in danger of creating an error because you cannot set a bool to be null!

The GetValueOrDefault will return a value (the default value for the variable type) even when the HasValue for the field is false (e.g. it’s null). Using the GetValueOrDefault means you don’t catch the error but you do have to be prepared for receiving a default value and code with that in mind.

So here it is in action

if (entity.Contains("salutation"))
bool credit = entity. CreditOnHold.GetValueOrDefault ();
entity. CreditOnHold = true ; credit = entity. CreditOnHold.GetValueOrDefault ();

 

 

The MSDN article if you want more, to be honest there isn’t much more to say about it.

Advertisement

FetchXML reports do not trigger RetrieveMultiple plugins in CRM 2011

 

It is by going down into the abyss that we recover the treasures of life. Where you stumble, there lies your treasure.
Joseph Campbell

The CRM forums are a great resource for learning interesting solutions and problems in Microsoft Dynamics CRM

I read this interesting gotcha/CRM Quirk this week about a CRM developer who had written a plugin which was triggered on the RetrieveMultiple event on his custom entity.  The user was wondering why his plugin was not being triggered when the user ran a report using FetchXML.

RetrieveMultiple Plugins, What, Why and How?

So you can have plugins which trigger on RetrieveMultiple for an entity.  Some readers are thinking why would you want to do that?

Reasons like

  • Translate data held in the CRM database
  • Exclude data from RetrieveMultiple queries
  • Log RetrieveMultiple requests
  • Calculate some fields in the retrieve from other entities
  • Decrypting encrypted fields

There are lots of other reasons, but they give you an idea.  The user wouldn’t even know a plugin had run because they would just receive the results as normal.

The retrieve multiple is triggered in CRM when the user

  • Runs an advanced find
  • User clicks on a view (e.g. active accounts)
  • Quick search
  • Associated views
  • Plugins doing retrieve multiples
  • Console apps doing retrieve multiples

 

What about reports and FetchXML

So why don’t reports and FetchXML trigger the RetrieveMultiple plugin

The answer was explained in this forum post answer by CRM legend David Jennaway who is a CRM MVP, when I was starting in CRM development David had a fantastic CRM blog which I followed avidly and still do, but but unfortunately for the CRM community David rarely writes any blog posts these days (2 post in 2014).

Reports do not trigger plugins. SQL reports bypass CRM completely, and Fetch reports also bypass the plugin pipeline
 He also add this answer
Just to clarify the last post, in CRM 2011 fetch queries are submitted via the RetrieveMultiple message, whereas in CRM 4 they were submitted via the Fetch message and (bizarrely) you could handle them in plugins on the Execute message
This logically makes sense because SQL reports definitely would hit the plugin pipeline and I’m guessing FetchXML when run from reports must be run using a slightly different mechanism (I have no knowledge of this)
I’m not sure if this is the same in CRM 2013 but I’m guessing it’s probably going to be the same because I don’t think there have been changes in those areas of functionality.