locked
update one to many relationship through plugin RRS feed

  • Question

  • Not able to get help from google if I use the above search criteria.  I am only getting the code for many-many using 'moniker', which is not what I want.

    I have a 1:N relationshionship between a custom entity anda a contact entity. When a new custom entity is saved, I want to use plugin to create a 1:N relationship between existing contact records and the newly created custom record. Can anyone point me to the link that contains the code for this.
    Wednesday, January 27, 2010 10:39 PM

Answers

  • You will register your plugin against the Create message for your custom entity in the Post pipeline. Here's something to get you started:
    public void Execute(IPluginExecutionContext context) 
    {
           // crm instance
           ICrmService crm = context.CreateCrmService(false);
    
           // get the id of our newly created record
           Guid id = (Guid)context.OutputParameters[ParameterName.Id]
     
           // create the relationship association on an existing contact record 
           DynamicEntity mycontactrecord = new DynamicEntity("contact");
    
           LookupProperty lookup = new LookupProperty("new_customentityid", new Lookup("new_customentityname", id));
           mycontactrecord.Properties.Add(lookup);
    
           KeyProperty key = new KeyProperty("contactid", new Key(contactid)); // assumes you managed to get your contactid elsewhere
           mycontactrecord.Properties.Add(key);
    
           // update the contact record
           TargetUpdateDynamic target = new TargetUpdateDynamic();
           target.Entity = mycontactrecord;
    
           UpdateRequest request = new UpdateRequest();
           request.Target = target;
    
           UpdateResponse response = (UpdateResponse)crm.Execute(request);
    }


    http://crmdude.wordpress.com/
    Thursday, January 28, 2010 2:09 AM
  • Thank you. However, I tried myself and the following worked.

    RMGuid = ((

    contact)colln.BusinessEntities[0]).contactid.Value;

    DynamicEntity() contactEntity =

    new DynamicEntity();

    contactEntity.Name =

    EntityName.contact.ToString();

    contactEntity.Properties.Add(

    new KeyProperty("contactid", new Key(RMGuid)));

    lookup =

    new Lookup();

    lookup.Value = officeId;

    lookup.type =

    CustomEntityName.Office;

    contactEntity.Properties.Add(

    new LookupProperty(OfficeEntityAttribute.RelationshipManager, lookup)); 

    service = context.CreateCrmService(

    true);

     

    TargetUpdateDynamic targetUpdate = new TargetUpdateDynamic();

    targetUpdate.Entity = contactEntity;

     

    UpdateRequest updReq = new UpdateRequest();

    updReq.Target = targetUpdate;

     

    UpdateResponse updRes = (UpdateResponse)service.Execute(updReq);

    Thursday, January 28, 2010 5:13 PM

All replies

  • How do I update a 1:N relationship through plug in between custom entity(parent) and contact(related entity)?
    Thursday, January 28, 2010 12:55 AM
  • You will register your plugin against the Create message for your custom entity in the Post pipeline. Here's something to get you started:
    public void Execute(IPluginExecutionContext context) 
    {
           // crm instance
           ICrmService crm = context.CreateCrmService(false);
    
           // get the id of our newly created record
           Guid id = (Guid)context.OutputParameters[ParameterName.Id]
     
           // create the relationship association on an existing contact record 
           DynamicEntity mycontactrecord = new DynamicEntity("contact");
    
           LookupProperty lookup = new LookupProperty("new_customentityid", new Lookup("new_customentityname", id));
           mycontactrecord.Properties.Add(lookup);
    
           KeyProperty key = new KeyProperty("contactid", new Key(contactid)); // assumes you managed to get your contactid elsewhere
           mycontactrecord.Properties.Add(key);
    
           // update the contact record
           TargetUpdateDynamic target = new TargetUpdateDynamic();
           target.Entity = mycontactrecord;
    
           UpdateRequest request = new UpdateRequest();
           request.Target = target;
    
           UpdateResponse response = (UpdateResponse)crm.Execute(request);
    }


    http://crmdude.wordpress.com/
    Thursday, January 28, 2010 2:09 AM
  • Thank you. However, I tried myself and the following worked.

    RMGuid = ((

    contact)colln.BusinessEntities[0]).contactid.Value;

    DynamicEntity() contactEntity =

    new DynamicEntity();

    contactEntity.Name =

    EntityName.contact.ToString();

    contactEntity.Properties.Add(

    new KeyProperty("contactid", new Key(RMGuid)));

    lookup =

    new Lookup();

    lookup.Value = officeId;

    lookup.type =

    CustomEntityName.Office;

    contactEntity.Properties.Add(

    new LookupProperty(OfficeEntityAttribute.RelationshipManager, lookup)); 

    service = context.CreateCrmService(

    true);

     

    TargetUpdateDynamic targetUpdate = new TargetUpdateDynamic();

    targetUpdate.Entity = contactEntity;

     

    UpdateRequest updReq = new UpdateRequest();

    updReq.Target = targetUpdate;

     

    UpdateResponse updRes = (UpdateResponse)service.Execute(updReq);

    Thursday, January 28, 2010 5:13 PM