I'm getting a strange NullReference when executing RetrieveMultiple in MS CRM 365 (online).
Unfortunately, the exception doesn't happen always, so it's hard to reproduce. The logs I was able to get show the following:
System.NullReferenceException: Object reference not set to an instance of an object. Server stack trace: at Microsoft.Crm.Sandbox.SandboxCallbackService.ProcessException(Exception e, SandboxSdkClient client) at Microsoft.Crm.Sandbox.SandboxOrganizationService.Execute(String operation, Byte[] serializedRequest, Object sandboxTraceSettingsObj) at System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Object[]& outArgs) at System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg) Exception rethrown at [0]: at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) at Microsoft.Crm.Sandbox.ISandboxOrganizationService.Execute(String operation, Byte[] serializedRequest, Object traceSettings) at Microsoft.Crm.Sandbox.SandboxOrganizationServiceWrapper.ExecuteInternal(OrganizationRequest request) at Microsoft.Crm.Sandbox.SandboxOrganizationServiceWrapper.RetrieveMultipleInternal(QueryBase query) at CRM.Plugins.Messages_Entry_Points.Report12EntryPlugin.ValidateReportInput(LocalPluginContext obj) at CRM.Plugins.Base_Files.Plugin.Execute(IServiceProvider serviceProvider)
The stack trace suggests the exception is thrown inside Microsoft CRM code (Microsoft.Crm.Sandbox.SandboxOrganizationService), not something a typical CRM developer has access to.
Here's the relevant code fragment (the ValidateReportInput method):
var groupSelector = preImage.GetAttributeValue<string>("str_customergroupcode"); var activityId = preImage.GetAttributeValue<string>("str_activityid"); if (!String.IsNullOrEmpty(groupSelector)) { var existingEntriesQuery = new QueryByAttribute("str_report12element") { ColumnSet = new ColumnSet("str_leadid", "str_contactid") }; existingEntriesQuery.AddAttributeValue("str_customergroupcode", groupSelector); existingEntriesQuery.AddAttributeValue("str_activityid", activityId); var results = obj.OrganizationService.RetrieveMultiple(existingEntriesQuery).Entities.Select(r => r.ToEntity<str_report12element>()); //some validation checks here; error seems to occurs in RetrieveMultiple above }
str_report12element is a model of a custom entity created mostly for technical reasons (i.e. it's not viewed by the users directly). The fields referenced in the query are regular string fields ("single line of text").
I know QueryByAttribute permits the use of null as query values (I've checked), so the query should work fine even if the two parameters (groupSelector and activityId in this case) are null. The OrganizationService object is clearly defined, as the exception seems to happen somewhere inside the RetrieveMultiple method. I also don't think Entities is null as the stack trace is, again, pointing towards the RetrieveMultiple method.
I've also considered if some other plugin isn't being triggered by this CRM message that could throw such an error, but the entity, being a custom one, is very basic. Additionally there are no plugins registered on the Retrieve or RetrieveMultiple messages for this entity.
A very similar error has been popping up in a different section, this time related to the Retrieve method.
System.NullReferenceException: Object reference not set to an instance of an object. Server stack trace: at Microsoft.Crm.Sandbox.SandboxCallbackService.ProcessException(Exception e, SandboxSdkClient client) at Microsoft.Crm.Sandbox.SandboxOrganizationService.Execute(String operation, Byte[] serializedRequest, Object sandboxTraceSettingsObj) at System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Object[]& outArgs) at System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg) Exception rethrown at [0]: at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) at Microsoft.Crm.Sandbox.ISandboxOrganizationService.Execute(String operation, Byte[] serializedRequest, Object traceSettings) at Microsoft.Crm.Sandbox.SandboxOrganizationServiceWrapper.ExecuteInternal(OrganizationRequest request) at Microsoft.Crm.Sandbox.SandboxOrganizationServiceWrapper.RetrieveInternal(String entityName, Guid id, ColumnSet columnSet) at Microsoft.Crm.Sandbox.SandboxOrganizationServiceWrapper.Retrieve(String entityName, Guid entityId, ColumnSet columnSet) at CRM.Plugins.Messages_Entry_Points.ContactEntityDMSSyncPlugin.CreateContactTransportModel(LocalPluginContext localContext, Contact contact) at CRM.Plugins.Messages_Entry_Points.ContactEntityDMSSyncPlugin.CreateSBusSync(LocalPluginContext obj) at CRM.Plugins.Base_Files.Plugin.Execute(IServiceProvider serviceProvider)
There are 3 calls to `Retrieve` in the `CreateContactTransportModel` method, all of which are fairly simple:
if (contact.str_address1_countyid != null) { var county = localContext.OrganizationService.Retrieve("str_county", contact.str_address1_countyid.Id, new ColumnSet("str_name")).ToEntity<str_county>(); result.County = county.str_name; } if (contact.str_City != null) { var city = localContext.OrganizationService.Retrieve("str_city", contact.str_City.Id, new ColumnSet("str_name")).ToEntity<str_city>(); result.City = city.str_name; } if (contact.str_titleid != null) { var title = localContext.OrganizationService.Retrieve("str_title", contact.str_titleid.Id, new ColumnSet("str_name")).ToEntity<str_title>(); result.Title = title.str_name; }
The only conclusion I can come up with is that something wrong is happening inside MS CRMs internal code and that's a very scary thought.
I'd like to know how I can work around this, or if this is something that I should get MS support to have a look at. Or perhaps, given that MS CRM does have a few "secrets" here or there, this is one of them and someone could shed some light on what's going on?
*This post is locked for comments