locked
how to retrieve n:n relationship in dynamics crm? RRS feed

  • Question

  • currency entity is associated with lead.i'm retrieving associated currency records.but i was unable to get the records.where im doing wrong.could you plz clarify anyone.it has N:N Relationship.below is my code

    if (context.InputParameters.Contains("Target") && (context.InputParameters["Target"]) is Entity)

                {
                    Entity en = (Entity)context.InputParameters["Target"];
                    QueryExpression _Query = new QueryExpression
                    {
                        EntityName = "transactioncurrency",
                        ColumnSet = new ColumnSet(true),
                        LinkEntities = 
                        { 
                            new LinkEntity 
                            { 
                            Columns=new ColumnSet(true),
                            LinkFromEntityName="lead",
                            LinkFromAttributeName="leadid",
                            LinkToEntityName="new_lead_transactioncurrency",
                            LinkToAttributeName="transactioncurrencyid",
                            LinkCriteria=new FilterExpression  
                            {


                                FilterOperator=LogicalOperator.And,
                                Conditions=
                                { 
                                    new ConditionExpression  

                                    {
                                    AttributeName="transactioncurrencyid",
                                    Operator=ConditionOperator.Equal,
                                    Values={en.Id}
                                    }
                                }

                            }

                            }
                        }
                    };

                    EntityCollection entitycollect = service.RetrieveMultiple(_Query);
                    int entitycount = entitycollect.Entities.Count;
                }
            }

                                                                                     
    Monday, June 16, 2014 9:06 AM

All replies

  • Where do you try this one? I had the same problem when I used a PostXXX Plugin. I resolve it to call in PreValidateXXXX plugin.

    And I saved it in sharedvariable and pass it between plugin :

    NB: You can get N:N only in PreValidate

    Retrieve N:N Relation in Prevalidate

    Save the result in Shared Variable:

    context.SharedVariables.Add("EntitySaved", (Object)entity);

    Use the entity in PostXXX Plugin:

    var context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)
                serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));
     
            // Obtain the contact from the execution ParentContext shared variables.
            if (context.ParentContext.SharedVariables.Contains("EntitySaved"))

            {

    }

         



    Alessandro Graps

    Monday, June 16, 2014 11:52 AM
  • Thanks you for Suggesting.

    hsk srinivas

    Monday, June 16, 2014 1:36 PM
  • ok.. I hope to resolve the problem.. Please it's ok please mark it like solve...

    Bye

    Alessandro


    Alessandro Graps

    Monday, June 16, 2014 1:38 PM
  • i'm getting error at transactioncurrency does'nt contain leadid.could you plz clarify me where i'm doing wrong.plz rephrase the code.

    hsk srinivas

    Monday, June 16, 2014 1:38 PM
  • Hi

    you can watch this link:

    http://bogdano-crm.blogspot.ch/2013/03/get-related-entities-from-many-to-many.html

    http://mscrmkb.blogspot.ch/2010/12/crm-2011-retrieve-related-entity-data.html

    In this link to can watch a pass sharedvariable:

    http://thomasthankachan.com/2011/08/12/pass-data-between-plug-ins-using-sharedvariables/


    Alessandro Graps

    Monday, June 16, 2014 1:45 PM