locked
N:N Relation Query in Plugin RRS feed

  • Question

  • Hi All,

    Hope i will get my solution here.

    I have a n:n relation in between Custom Entities.
    let NNTest1 and NNTest2.
    and i need to fire a query in btwn these.
    as per the SDK in between roles and systemuser , the sample is working.
    but in the same way in between custom entities its not working.
    i was checking in the DB the table created , i mean the intersect table contains no data.

    So if any body is having any idea on it, really welcome and appreciated.

    Sudhanshu
    Friday, August 28, 2009 4:03 PM

Answers

  • I'd like to give you the C# code for your requirement:

            public static BusinessEntity[] GetNN(CrmService service, string fromEntity, string fromEntityId,
                                                    string toEntity, string toEntityId, string relId)
            {
    
                //Retrieve new_NNTest2 from new_NNTest1 with N:N
                ConditionExpression conditionName = new ConditionExpression();
                conditionName.AttributeName = fromEntityId; //"new_NNTest1";
                conditionName.Operator = ConditionOperator.Equal;
                conditionName.Values = new object[1];
                conditionName.Values[0] = eid;
                FilterExpression selectByName = new FilterExpression();
                selectByName.Conditions = new ConditionExpression[] { conditionName };
    
                //Create nested link entity and apply filter criteria
                LinkEntity nestedLinkEntity = new LinkEntity();
                nestedLinkEntity.LinkToEntityName = fromEntity; //"new_NNTest1";
                nestedLinkEntity.LinkFromAttributeName = fromEntityId; //"new_NNTest1id";
                nestedLinkEntity.LinkToAttributeName = fromEntityId; //"new_NNTest1id";
                nestedLinkEntity.LinkCriteria = selectByName;
    
                //Create the nested link entities
                LinkEntity intersectEntity = new LinkEntity();
                intersectEntity.LinkToEntityName = relId; //"new_new_new_NNTest1_new_new_NNTest2";
                intersectEntity.LinkFromAttributeName = toEntityId; //"new_NNTest2id";
                intersectEntity.LinkToAttributeName = toEntityId; //"new_NNTest2id";
                intersectEntity.LinkEntities = new LinkEntity[] { nestedLinkEntity };
    
                //Create Query expression and set the entity type
                QueryExpression expression = new QueryExpression();
                expression.EntityName = toEntity; //"new_NNTest2";
                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;
    
                return entities;
                    
            }


    Hope it helps.

    Jim
    Jim Wang - MVP Dynamics CRM - http://jianwang.blogspot.com, http://mscrm.cn
    • Marked as answer by Jim Glass Jr Tuesday, September 15, 2009 3:06 PM
    Tuesday, September 15, 2009 2:44 PM
    Moderator

All replies

  • Hi,

    I have been working to find a resolution to this issue and found that the plug-ins don't fire properly when creating the N:N relationship. This blog post summarizes the solutions we have found to getting around this: http://consulting.ascentium.com/blog/crm/Post533.aspx .

    Chris
    Monday, September 14, 2009 10:56 PM
  • I'd like to give you the C# code for your requirement:

            public static BusinessEntity[] GetNN(CrmService service, string fromEntity, string fromEntityId,
                                                    string toEntity, string toEntityId, string relId)
            {
    
                //Retrieve new_NNTest2 from new_NNTest1 with N:N
                ConditionExpression conditionName = new ConditionExpression();
                conditionName.AttributeName = fromEntityId; //"new_NNTest1";
                conditionName.Operator = ConditionOperator.Equal;
                conditionName.Values = new object[1];
                conditionName.Values[0] = eid;
                FilterExpression selectByName = new FilterExpression();
                selectByName.Conditions = new ConditionExpression[] { conditionName };
    
                //Create nested link entity and apply filter criteria
                LinkEntity nestedLinkEntity = new LinkEntity();
                nestedLinkEntity.LinkToEntityName = fromEntity; //"new_NNTest1";
                nestedLinkEntity.LinkFromAttributeName = fromEntityId; //"new_NNTest1id";
                nestedLinkEntity.LinkToAttributeName = fromEntityId; //"new_NNTest1id";
                nestedLinkEntity.LinkCriteria = selectByName;
    
                //Create the nested link entities
                LinkEntity intersectEntity = new LinkEntity();
                intersectEntity.LinkToEntityName = relId; //"new_new_new_NNTest1_new_new_NNTest2";
                intersectEntity.LinkFromAttributeName = toEntityId; //"new_NNTest2id";
                intersectEntity.LinkToAttributeName = toEntityId; //"new_NNTest2id";
                intersectEntity.LinkEntities = new LinkEntity[] { nestedLinkEntity };
    
                //Create Query expression and set the entity type
                QueryExpression expression = new QueryExpression();
                expression.EntityName = toEntity; //"new_NNTest2";
                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;
    
                return entities;
                    
            }


    Hope it helps.

    Jim
    Jim Wang - MVP Dynamics CRM - http://jianwang.blogspot.com, http://mscrm.cn
    • Marked as answer by Jim Glass Jr Tuesday, September 15, 2009 3:06 PM
    Tuesday, September 15, 2009 2:44 PM
    Moderator
  • Hi Jim,

    Have you worked  on this bunch of code?
    Is that worked for you happily?
    It is not working for me.
    Can u please have a look on it and get back to me.
    I did something, for which its working for the system N:N relations but not for the Custom Entities.
    So any help is welcome.
    Tons of thanks for your valuable time.

    Regards

    Sudhanshu
    Wednesday, September 23, 2009 10:45 AM