Plugins: context.InputParameters[“Target”] Entity – when are null values returned in the Attribute Collection?

I have been writing a fair few Plugins over the past week and came across an issue, whereby attributes which had “null” values, were being returned in some instances and not in others in the Entity attribute collection derived from the context.InputParameters.

To cover my bases, I started to using a combination of both;

if(entity.Attributes.Contains("sb_contactid")) {.....}
EntityReference contactid = (EntityReference)entity.Attributes["sb_contactid"];
and checking if contactid reference == null

However, following some JavaScript coding on an entity I was writing a plugin for, I noticed that a colleague of mine had used some v.4.0 javascript code to “set” and “unset” the required field level on both the contact and account lookups based on the information captured on the form.


Armed with this new knowledge, I realised that the plugin was returning “null” values for these two fields, because they were defined in CRM as “business required”, even though the “red asterisk” was not present on the Form due to the JavaScript code.

So to surmise, any fields that are defined as “business required” will always be returned in the Entity attribute collection, even if they contain “null” values.

UPDATE (11/08/11):

This does not seem to be the case if the plugin is triggered via the CRM web services i.e. an external application calling into CRM using the standard “Out of the box” service endpoints.

This warrants escalation to Microsoft Support, as to why the plugin behaves differently when triggered from the UI and Service endpoints. Will update this post once I have an answer (or not).