Friday, December 16, 2016

Associate Lead to Email Send in Dynamics CRM

The Associate Lead to Email Send custom workflow activity will accept a Lead and an Email Send and associate the two via a CRM many-to-many relationship named cdi_emailsend_lead


Email Send
EntityReference (cdi_emailsend)
Lookup to the Email Send
EntityReference (lead)
Lookup to the Lead

Indicates Success or failure of the code

OutputsAssociate two records

using Microsoft.Xrm.Sdk;

using Microsoft.Xrm.Sdk.Messages;

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 CustomWorkflow


    public class AssociateContactToEmailSend : CodeActivity


        #region Input Properties




        public InArgument<EntityReference> Guest { get; set; }


        [Input("Email Send")]


        public InArgument<EntityReference> EmailSend { get; set; }


        protected override void Execute(CodeActivityContext context)


            IWorkflowContext workflowContext = context.GetExtension<IWorkflowContext>();

            IOrganizationServiceFactory serviceFactory = context.GetExtension<IOrganizationServiceFactory>();

            IOrganizationService service = serviceFactory.CreateOrganizationService(workflowContext.UserId);

            //Create the tracing service

            ITracingService tracingService = context.GetExtension<ITracingService>();

            var guest = Guest.Get(context);

            var emailSend = EmailSend.Get(context);

            tracingService.Trace("Guest:" + guest.Id);

            tracingService.Trace("Email Send:" + emailSend.Id);

            Microsoft.Xrm.Sdk.EntityReference Moniker2 = new Microsoft.Xrm.Sdk.EntityReference();

            Moniker2.Id = guest.Id;

            Moniker2.LogicalName = "contact";//Entity Name

            // Code Create Moniker for second Entity: New_CustomEntity

            Microsoft.Xrm.Sdk.EntityReference Moniker1 = new Microsoft.Xrm.Sdk.EntityReference();

            Moniker1.Id = emailSend.Id;

            Moniker1.LogicalName = "cdi_emailsend";

            tracingService.Trace("Started Assosiating");

            AssociateManyToManyEntityRecords(service, Moniker1, Moniker2, "cdi_emailsend_contact");

            tracingService.Trace("Completed Assosiating");


        private bool AssociateManyToManyEntityRecords(IOrganizationService service, Microsoft.Xrm.Sdk.EntityReference moniker1, Microsoft.Xrm.Sdk.EntityReference moniker2, string strEntityRelationshipName)




                AssociateRequest request = new AssociateRequest();

                request.Target = moniker1;

                request.RelatedEntities = new EntityReferenceCollection { moniker2 };

                request.Relationship = new Relationship(strEntityRelationshipName);

                // Execute the request.


                return true;


            catch (Exception exp)


                return false;





No comments:

Post a Comment

Get files of last hour in Azure Data Factory

  Case I have a Data Factory pipeline that should run each hour and collect all new files added to the data lake since the last run. What is...