Softstuff Consulting
My occasional musings of a technical nature
  • Send us a message
  • About Softstuff Consulting
  • Privacy Policy
  • Main Website
C# , Dynamics365

A small CRM2015 plugin to create a relationship

by Ian Blair February 23, 2019 No Comments

Recently needing to provide the ability to create a N:N relationship in a workflow process provided an excuse to create a quick workflow plugin as unfortunately it isn’t currently possible to do it out-of-the-box if you have defined a relationship rather than using an intersect table.

Fortunately a plugin to do this is a very simple affair:

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;
using System;
using System.Activities;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
   
namespace Softstuff.Workflow.Relationships
{
    public class CreateManyToManyLink : CodeActivity
    {
        protected override void Execute(CodeActivityContext executionContext)
        {
            IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
            IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.InitiatingUserId);
  
            EntityReference account = Record1.Get<EntityReference>(executionContext);
            EntityReference new_testentity = Record2.Get<EntityReference>(executionContext);
            EntityReferenceCollection relatedEntities = new EntityReferenceCollection();
  
            // Add the related entity
            relatedEntities.Add(account);
  
            // Add the relationship schema name
            Relationship relationship = new Relationship("new_account_new_testentity");
  
            // Associate the account record to new_testentity
            service.Associate(new_testentity.LogicalName, new_testentity.Id, relationship, relatedEntities);
  
        }
  
        [Input("Account")]
        [ReferenceTarget("account")]
        public InArgument<EntityReference> Record1 { get; set; }
  
        [Input("New_TestEntity")]
        [ReferenceTarget("new_testentity")]
        public InArgument<EntityReference> Record2 { get; set; }
    } 
}

The plugin takes two parameters both EntityReferencetypes, and although I had hoped to be able to create a universal plugin that would work for all entities, but unfortunately you have to include the [ReferenceTarget(<entity>)] clause that limits theEntityReference to a single entity type.

For this example I have created a N:N relationship between account and new_testentity and the relationship is called new_account_new_testentity.

The lines in the plugin that actually do the work are:

relatedEntities.Add(account);   // the account entityreference is added to the EntityReferenceCollection.
Relationship relationship = new Relationship(<name of the relationship>);  // define the relationship
service.Associate(EntityReference,LogicalName,EntityReference.Id, relationship, relatedEntities);  // actually create the link

The only other thing to remember before you compile and deploy the plugin is to sign the assembly.

Deploy the assembly using the plugin registration tool and it should appear as a custom workflow step.

c#code

  • Previous Retrieving dates and times from MS CRM in code6 years ago
  • Next Using the CLR to create a Random Number generator6 years ago

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • How to make your Powershell scripts more annoying
  • What motherboard is in my PC
  • A Dynamics365 plugin thought experiment
  • Registering a Dynamics365 plugin and I get an error
  • Going back in time with Dynamics365

Categories

  • Bootstrap
  • C#
  • CSS
  • Dot Net Core
  • Dynamics365
  • JavaScript
  • Powershell
  • T-SQL
  • Thoughts
  • VBScript
  • Visual Studio
  • Windows
  • Xamarin

Recent Comments

  • S0ftStuffB055 on Call a Dynamics365 workflow from JavaScript
  • Siva on Call a Dynamics365 workflow from JavaScript
  • TC Sharpe on Throw exceptions in Dynamics365 workflow plugins, or don’t
  • BigOwl on Throw exceptions in Dynamics365 workflow plugins, or don’t
  • CRMGod on Access a Dynamics365 embedded webresource

Archives

  • January 2021
  • May 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • October 2019
  • June 2019
  • May 2019
  • February 2019
2025 Softstuff Consulting. Donna Theme powered by WordPress
  • Twitter