Solved

Micosoft Dynamics CRM relationship/middle entity help

Posted on 2010-09-23
3
1,417 Views
Last Modified: 2012-05-10
Hello all,

I had a previous question which I've deleted, so I can restate the problem and the errors I'm receiving. I'm trying to find all of the products associated with an account in CRM. A middle/intersect entity has been created called new_accountproductdetail.

Account entity has a 1:N to the new_accountproductdetail entity on accountid to new_accountid attributes.

Product entity has a 1:N to the new_accountproductdetail entity on productid to new_producttid attributes.

When I run my code I get the following error:
 'New_accountproductdetail' entity doesn't contain attribute with Name = 'productid'.

The following code that I'm using has been based on the following article:
http://blogs.msdn.com/b/crm/archive/2008/02/14/crm-4-0-relationships-explained.aspx

public void GetPrimaryAcctProducts(CrmService service, string accountID)
          {
               try
               {

                    ConditionExpression conditionName = new ConditionExpression();
                    conditionName.AttributeName = "productid";
                    conditionName.Operator = ConditionOperator.Equal;
                    conditionName.Values = new object[1];
                    conditionName.Values[0] = "product";

                    FilterExpression selectByName = new FilterExpression();

                    selectByName.Conditions = new ConditionExpression[] { conditionName };

                    //Create nested link entity and apply filter criteria 
                    LinkEntity nestedLinkEntity = new LinkEntity();
                    nestedLinkEntity.LinkToEntityName = "account";
                    nestedLinkEntity.LinkFromAttributeName = "accountid";
                    nestedLinkEntity.LinkToAttributeName = "new_accountid";
                    nestedLinkEntity.LinkCriteria = selectByName;

                    //Create the nested link entities 
                    LinkEntity intersectEntity = new LinkEntity();
                    intersectEntity.LinkToEntityName = "new_accountproductdetail";
                    intersectEntity.LinkFromAttributeName = "new_productid";
                    intersectEntity.LinkToAttributeName = "productid";
                    intersectEntity.LinkEntities = new LinkEntity[] { nestedLinkEntity };

                    //Create Query expression and set the entity type to lead 
                    QueryExpression expression = new QueryExpression();
                    expression.EntityName = "product";
                    expression.LinkEntities = new LinkEntity[] { intersectEntity };

                    RetrieveMultipleRequest request = new RetrieveMultipleRequest();

                    request.Query = expression;

                    //Execute and examine the response 
                    RetrieveMultipleResponse response = (RetrieveMultipleResponse)service.Execute(request);

                    BusinessEntity[] entities = response.BusinessEntityCollection.BusinessEntities;

                    string totals = "Total related=" + entities.Length; 

               }
               catch (SoapException error)
               {
                    throw new Exception(error.Detail.SelectSingleNode("//description").InnerText);
               }
          }

Open in new window

0
Comment
Question by:Paul_Gastineau
  • 2
3 Comments
 
LVL 7

Accepted Solution

by:
stacko111 earned 500 total points
ID: 33746037
Without looking too deeply into this it looks like your code should be looking for 'new_productid' rather than 'productid'
0
 

Author Comment

by:Paul_Gastineau
ID: 33746847
Thanks for the reply stacko, I swapped all of the LinkFromAttributeName and LinkToAttributeName values. In the code I'd posted I'd also forgotten to list my input var accountID as the contionname.value.

This has gotten rid of the errors but I'm still not getting any products returned. Can anyone help?
public void GetPrimaryAcctProducts(CrmService service, string accountID)
          {
               try
               {

                    //This code will retrieve a list of "leads" associated with the entity "Foo" on the relationship whose intersect entity is "new_account_lead_custom" 
                    //Filter by the specific record that we are looking for 
                    //(In this example we assume that there are no other accounts with the name Foo, otherwise 
                    // if would be recommended to use the account "id" instead of the name. 

                    ConditionExpression conditionName = new ConditionExpression();
                    conditionName.AttributeName = "accountid";
                    conditionName.Operator = ConditionOperator.Equal;
                    conditionName.Values = new object[1];
                    conditionName.Values[0] = accountID;

                    FilterExpression selectByName = new FilterExpression();

                    selectByName.Conditions = new ConditionExpression[] { conditionName };

                    //Create nested link entity and apply filter criteria 
                    LinkEntity nestedLinkEntity = new LinkEntity();
                    nestedLinkEntity.LinkToEntityName = "account";
                    nestedLinkEntity.LinkFromAttributeName = "new_accountid";
                    nestedLinkEntity.LinkToAttributeName = "accountid";
                    nestedLinkEntity.LinkCriteria = selectByName;

                    //Create the nested link entities 
                    LinkEntity intersectEntity = new LinkEntity();
                    intersectEntity.LinkToEntityName = "new_accountproductdetail";
                    intersectEntity.LinkFromAttributeName = "productid";
                    intersectEntity.LinkToAttributeName = "new_productid";
                    intersectEntity.LinkEntities = new LinkEntity[] { nestedLinkEntity };

                    //Create Query expression and set the entity type to lead 
                    QueryExpression expression = new QueryExpression();
                    expression.EntityName = "product";
                    expression.LinkEntities = new LinkEntity[] { intersectEntity };

                    RetrieveMultipleRequest request = new RetrieveMultipleRequest();

                    request.Query = expression;

                    //Execute and examine the response 
                    RetrieveMultipleResponse response = (RetrieveMultipleResponse)service.Execute(request);

                    BusinessEntity[] entities = response.BusinessEntityCollection.BusinessEntities;

                    string totals = "Total related=" + entities.Length; 


               }
               catch (SoapException error)
               {
                    throw new Exception(error.Detail.SelectSingleNode("//description").InnerText);
               }
          }

Open in new window

0
 

Author Closing Comment

by:Paul_Gastineau
ID: 33747550
I had all of the LinkFromAttributeName and LinkToAttributeName reversed. Switching them cleared all of the errors.
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
CRM 2011 Deployment Package 2 170
Dynamics CRM 2016 - Auto-numbering 2 118
Gray out Sections based on Option Set Values - CRM 2011 6 98
SSRS Problems 9 102
Automatically creating a Trello card using data from a Microsoft Dynamics CRM record turned out to be an easy project that yielded great results.  Here's how I did this for an internal team at General Code.
For cloud, the “train has left the station” and in the Microsoft ERP & CRM world, that means the next generation of enterprise software from Microsoft is here: Dynamics 365 is Microsoft’s new integrated business solution that unifies CRM and ERP fun…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question