locked
QueryExpression Duplicate Field Names (linked tables) RRS feed

  • Question

  • Good Morning

    I'm using the toolkit sdk and created a new plugin which find records using QueryExpression and then create new entities.

    The problem I have is that when creating the new record I can't specify which linked table the value "new_name" is selected from in the query. Referring to "collection.Attributes["new_name"];" selects the value from "new_entity1" when I need the "new_name" value from the linked entity "new_entity3".

    I've tried a number of ways of referring to the correct value (collection["new_entity3.new_name"]) which cause the plugin to fail as the value can't be found.

    Below is an example of the code which links a many to many relationship.

    How can I specify the table that the value should be selected from?

    Thank you.

    Ben

    Microsoft.Xrm.Sdk.Query.QueryExpression query1 = new Microsoft.Xrm.Sdk.Query.QueryExpression();
    
    	query1.EntityName = "new_entity1";
    	query1.ColumnSet.AllColumns = true;
                 
    Microsoft.Xrm.Sdk.Query.LinkEntity link1 = new Microsoft.Xrm.Sdk.Query.LinkEntity();
    	link1.LinkFromEntityName = "new_entity1";
    	link1.LinkFromAttributeName = "new_entity1id";
    	link1.LinkToEntityName = "new_entity2";                  
    	link1.LinkToAttributeName = "new_entity2id1";
                        
    Microsoft.Xrm.Sdk.Query.LinkEntity link2 = new Microsoft.Xrm.Sdk.Query.LinkEntity();
    	link2.LinkFromEntityName = "new_entity2";
    	link2.LinkFromAttributeName = "new_entity2id2";
    	link2.LinkToEntityName = "new_entity3";
    	link2.LinkToAttributeName = "new_entity3id";                  
                          
    Microsoft.Xrm.Sdk.Query.ConditionExpression ce = new Microsoft.Xrm.Sdk.Query.ConditionExpression();
    	ce.AttributeName = "new_entity1id";
        ce.Operator = Microsoft.Xrm.Sdk.Query.ConditionOperator.Equal;
        ce.Values.Add((Guid)((EntityReference)entity.Attributes["new_entity1id"]).Id);
                        
    Microsoft.Xrm.Sdk.Query.FilterExpression filter2 = new Microsoft.Xrm.Sdk.Query.FilterExpression();
        filter2.Conditions.Add(ce);
        query1.Criteria.AddFilter(filter2);
    
        link1.LinkEntities.Add(link2);
    	query1.LinkEntities.Add(link1);
                        
    EntityCollection collection = service.RetrieveMultiple(query);
    
     foreach (Entity entity in collection.Entities)
    	{
                Entity thing = new Entity("new_thing");
    			
    			entity["new_name"] = "Name" + collection.Attributes["new_name"];
    			
    			service.Create(thing);
    	}



    Tuesday, September 25, 2012 9:42 AM

Answers

  • This works....

    link2.EntityAlias = "link"; 

    and then when creating the new entity.

    entity["new_name" = "Name" + ((AliasedValue)entity["link.new_name"]).Value;

    Thank you.

    Ben

    • Marked as answer by BenSandall Tuesday, September 25, 2012 11:39 AM
    Tuesday, September 25, 2012 11:39 AM