locked
[CRM 2013] How to create a relationship between 2 records wih a workflow? RRS feed

  • Question

  • Hi everyone,

    I have created a N:N relationship between 2 entities, and I would like to know if it is possible to create a relationship between 2 records of this entities using a workflow? If it is, how can I do it? And if it's not, do you know a way to automatically create this relationship between records?

    Thank you for your help.

    Raphael

    Wednesday, November 27, 2013 1:03 PM

Answers

  • Hi,

    If created N:N relationship is Manual then you should be able to create/associate it through OOB workflow create record step.

    Otherwise you have to write custom code. It could be plugin or custom workflow :

    here is sample code :

    public static void AssociateRecords(EntityReference A, EntityReference B)
    {
                // Creating EntityReferenceCollection 
                EntityReferenceCollection relatedEntities = new EntityReferenceCollection();
    
                // Add the related entity 
                relatedEntities.Add(A);
    
                // Add the relationship schema name
                Relationship relationship = new Relationship("new_relationshipname");
    
                // Associate the A record to B
                _Service.Associate(B.LogicalName, B.Id, relationship, relatedEntities);
    
      }


    Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !!!
    Vikram Singh. !!!  My Blog


    • Edited by _Vikram Wednesday, November 27, 2013 1:33 PM
    • Marked as answer by Rapothin Wednesday, November 27, 2013 4:41 PM
    Wednesday, November 27, 2013 1:32 PM

All replies

  • Hi,

    If created N:N relationship is Manual then you should be able to create/associate it through OOB workflow create record step.

    Otherwise you have to write custom code. It could be plugin or custom workflow :

    here is sample code :

    public static void AssociateRecords(EntityReference A, EntityReference B)
    {
                // Creating EntityReferenceCollection 
                EntityReferenceCollection relatedEntities = new EntityReferenceCollection();
    
                // Add the related entity 
                relatedEntities.Add(A);
    
                // Add the relationship schema name
                Relationship relationship = new Relationship("new_relationshipname");
    
                // Associate the A record to B
                _Service.Associate(B.LogicalName, B.Id, relationship, relatedEntities);
    
      }


    Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !!!
    Vikram Singh. !!!  My Blog


    • Edited by _Vikram Wednesday, November 27, 2013 1:33 PM
    • Marked as answer by Rapothin Wednesday, November 27, 2013 4:41 PM
    Wednesday, November 27, 2013 1:32 PM
  • Thank you for your help.

    With a manual N:N relationship it works very well.

    Wednesday, November 27, 2013 4:42 PM