locked
2011 - Plugin add related entites RRS feed

  • Question

  • I have two custom entities which have a recommended relationship. Here's the definitions:

    CE1:
     - CE2Id:Guid

    CE2:
     - IsDefault:bool 

    You can see that we have a N:1 relationship between CE1:CE2

    on the form of CE1 I want the selection of CE2 to be optional and in a Create/Update pre-operation** plugin to set the relationship between the two to the CE2 labelled IsDefault.

     

    new_ce2 ce2;
    if(ce1.new_ce2_new_ce1 != null) //Check the relationship
    	ce2 = ce1.new_ce2_new_ce1;
    else { ce2 = 
    GetDefaultCE2(service, ce1); //the related entity might not be filled in but the ID might be set so get it, if not get the default
    if(ce2== null) throw new InvalidOperationException("No Default found");
    	if(ce1.new_ce2Id == null || ce1.new_ce2Id.Id == Guid.Empty)
    { EntityCollection ec = new EntityCollection(); ec.EntityName = ce2.LogicalName; ec.Entities.Add(ce2); ce1.RelatedEntities[new Relationship("new_ce2_new_ce1")] = ec;
    } }

     

    However this doesn't seem to set the relationship. 

     

    **Reason for pre-op is because some other values are being populated as well, and I want these filled in before it gets to the db.


    Thank you in advance for your help. If you think you may be able to help with any of my unanswered threads please look at them here
    Tuesday, April 12, 2011 4:46 PM

Answers

  • Turns out I was just looking at the wrong set of properties:

    ce1.new_ce2Id.Id = new EntityReference("new_ce2_new_ce1", ce2.Id);


    Thank you in advance for your help. If you think you may be able to help with any of my unanswered threads please look at them here
    Monday, May 2, 2011 3:08 PM