BusinessRules bug?: “The given key was not present in the dictionary” when creating record in Dynamics 365 v.8.2

I got the following error when creating a Contact in a migration job:

Details: System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]: Unexpected exception from plug-in (Execute): Plugins.ContactOnCreate: Microsoft.Xrm.Sdk.SaveChangesException: An error occured while processing this request. (Fault Detail is equal to Microsoft.Xrm.Sdk.OrganizationServiceFault).

When digging in the exception details, i found the following message: “The given key was not present in the dictionary”.

Info about my set up: On the create message of the Contact entity, I have a plugin that creates a predefined set of related records of a custom entity. On the related entity I have a business rule that set the contact lookup to read only if it has a value.

Explanation

When creating a CRM record with a lookup to another record where the name field is not set, the create request will fail if there is a business rule on the record you are trying to create, where the following conditions are met:

  • The business rules scope is set to object
  • The business rule has a condition that checks if the lookupv that points to an unnamed related record, is set

This scenario can happen in an integration or migration scenario, where some records might be created without a name value.

Resolution

You can avoid the error by ensuring that:

  • The primary attribute (the name field) is business required on all entities
  • Avoid setting business rule scope to object, unless you have to
  • Ensure that all integrations, migration jobs and plugins create new record with at least a dummy value in the primary attribute, if no value is available.
Advertisements