Dynamics Connector: The CrmReference mapping query returned no results for property

First I want to give a huge thanks to this blog: http://crmbusiness.wordpress.com/2012/02/23/crmnav-connector-crm-exception-message-entity-doesnt-contain-attribute-with-name-name/

This blog had the exact solution to a problem that was difficult to diagnose and then finding a fix seemed near impossible.

I have committed to using the Dynamics Connector for my integration between GP 2010 and CRM 2011.  We use CRM 2011 for opportunities and then create quotes and sales documents in GP that reference an opportunity number (custom field in CRM) .  This opportunity number is used to associate our sales documents to the opportunity.

I simply mapped the Opportunity number that was stored in a user defined field in GP to the appropriate look up field on the map. I get

  The CrmReference mapping query returned no results for property: Opportunity with dynamics_integrationkey = 000020722.

So I ran the configuration utility to allow opportunities to be seen as a lookup.

still getting error….

I quickly realized that at no point had the dynamics_integrationkey been populated with anything in the opportunity entity.  Once I had the opportunity number in the dynamics_integrationkey field in CRM, I expected to no longer see that error.  The accounts pulling over from GP were using the GP customer number as a dynaimcsintegration_key and they were mapping across just fine.  But when I ran the integration I continued to see that error.  For a little debugging I create a mock opportunity with a name of 12345 and used that as my dynamicsintegration_key in the map and sure enough it associated all of the sales documents to that opportunity.  So with that I had determined that it was NOT using the dynmaicsintegration_key at all to do the lookup.  It was using the name.  Now this is a problem in this situation.  It seems unreasonable to have users enter the “name” of the opportunity into GP.  Sometimes those can be very long.

I came across the blog mentioned above. In this blog he was using a solution that was created orginally for mapping custom entities that have the new_ prefix and reworked it to alter the look up from name to the dynamicsintegration_key.  It seemed like a very strong-arm way to approach this issue.  But I have dealt with this long enough and needed a solution.

I did what is suggested.  I created a plugin with the code just exactly like Hosk.  Only I updated it to only look at the opportunity entity when intercepting the FetchXML.

this is what my code ended up looking like


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
using Xrm;
using Microsoft.Xrm.Sdk.Query;
namespace CRM2011Plugin
{
 public class InterceptFetchforConnector: IPlugin
 {
 public void Execute(IServiceProvider serviceProvider)
 {
 // Obtain the execution context from the service provider.
 Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)
 serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));
 if (context.PrimaryEntityName.Contains("opportunity")) //Replace with your prefix
 {
 // tracingService.Trace("in the if statement");
 if (context.InputParameters.Contains(("Query")))
 {
 QueryExpression query = (QueryExpression)context.InputParameters["Query"]; ;
 FilterExpression fexp = query.Criteria;
 foreach (ConditionExpression condition in fexp.Conditions)
 {
 if (condition.AttributeName == "name")
 condition.AttributeName = "dynamics_integrationkey";
 }
 }
 }
 }
 }
}

Walla it worked like a charm. (sorry the formatting is bad on the code)

I registered  two steps for the plugin

Message: Retrieve
Entity: Opportunity
Pre-validation

Message: RetrieveMultiple
Entity: Opportunity
Pre-validation

At this point I only need this plugin to intercept the fetch xml on opportunities.  I don’t want it to run on every retrieve.

My conclusion on this is.  With the connector it seems that any lookups are going to use the name field to find a match.  This is usually not an issue.  But my assumption was the reason for the dynamicsintegration_key was so you could map your lookups to the fields that you need.  Name is not always the one that is used. I wrote a workflow that on the creation of an opportunity it will add the Opportunity Number to the dynamicsintegration_key.

Now all my sales documents are being successfully associated with their opportunity.

About dorothyjarry

Super Dots
This entry was posted in Connector for Microsoft Dynamics, CRM, CRM 2011, Dynamics. Bookmark the permalink.

3 Responses to Dynamics Connector: The CrmReference mapping query returned no results for property

  1. Hosk says:

    I would make sure you are using the latest connector versions because they have fixed a lot of the bugs.

    They may have fixed this problem or they might not have

    also the latest connector release is a lot more robust than previous versions.

    It’s good to see someone else blogging about the connector, the more people the better I say

    Like

    • dorothyjarry says:

      thanks Hosk
      I am on V2. I have not upgraded to R2 which just came out. I am not 100% this is a bug. I wanted the system to work different than it does and you gave me a way to accomplish that.

      I can see this simple solution being a starting point for a lot of other things with plugins and fetchxmL in general.

      Dorothy

      Dorothy

      Like

  2. lallegro says:

    Thanks νery nice blog!

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s