Yesterday, when I was looking into some failed tests, I discovered a quirk of action. If you invoke an action and don’t pass in the arguments that are optional, it doesn’t mean this is going to come up as null in the execution context.
Below is the action I used to replicate the behaviour.
When I execute the action and don’t set any arguments, here is what comes through in the execution context.
Argument Type | Value |
---|---|
Boolean | false |
DateTime | 0001-01-01T00:00:00 |
Decimal | 0 |
Entity | null |
EntityReference | null |
Float | 0 |
Int | 0 |
Money | null |
Picklist | null |
String | null |
EntityCollection | null |
The interesting things to note are:
- Action arguments, don’t behave the same as AttributeCollection. When you retrieve an entity using the SDK, get the attribute just using the key (not GetAttributeValue), the key won’t exist in the collection, if the attribute value is null. This is not the same behaviour with the action arguments. The key will always be there in the context, even if was not set during the action invocation.
- The default value of action arguments = default value of the primitive type
This essentially means that if you are going to do some processing based on the action argument that is a primitive type (Boolean, DateTime, Decimal, Float, Int, String), don’t make it optional. For e.g. if you make a boolean argument optional, how would you differentiate between an action invocation with the argument set to false, and another one which did not set the argument at all?
tl;dr; For action arguments (both input and output) of type Boolean, DateTime, Decimal, Float, Int and String avoid optional.
Thanks for the handy article. I have a question that follows on from this with regards to using arguments in condition statements and how to tell if they are “null”.
By biggest headache being how to tell if an entity reference input argument was not set as you are only able to use the entity itself not check the argument?
This appears to happen only in old version of CRM. I checked this v9 and it does not throw null reference exception.