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

Inputs

Name
Type
Notes
Email Send
EntityReference (cdi_emailsend)
Lookup to the Email Send
Lead
EntityReference (lead)
Lookup to the Lead


Name
Type
Notes
Success
Boolean
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

        [RequiredArgument]

        [Input("Contact")]

        [ReferenceTarget("contact")]

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


        [RequiredArgument]

        [Input("Email Send")]

        [ReferenceTarget("cdi_emailsend")]

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

        #endregion

        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)

        {

            try

            {

                AssociateRequest request = new AssociateRequest();

                request.Target = moniker1;

                request.RelatedEntities = new EntityReferenceCollection { moniker2 };

                request.Relationship = new Relationship(strEntityRelationshipName);


                // Execute the request.

                service.Execute(request);



                return true;

            }

            catch (Exception exp)

            {

                return false;

            }

        }

    }

}








Comments

Popular posts from this blog

Workflow Activity Input and Output Parameters and Data Types in MS CRM 2013/15/16

Get Workflow Guid in Javascript using OData query

Convert Plugin to Sandbox mode,