Associate two records



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;


namespace AssociatedLeadEmail_WF
{
    public class AssociatedLeadEmail_WF : CodeActivity
    {
        #region Set the input mandatory parameters.
        //Input Value of Associated Lead Record
        [RequiredArgument]
        [Input("Email Send")]
        [ReferenceTarget("cdi_emailsend")]
        public InArgument<EntityReference> EmailSend { get; set; }

        [RequiredArgument]
        [Input("Lead")]
        [ReferenceTarget("lead")]
        public InArgument<EntityReference> Lead { get; set; }

        [Output("Success")]
        public OutArgument<bool> Success { get; set; }

        #endregion

        protected override void Execute(CodeActivityContext context)
        {
            //Create the tracing service
            ITracingService tracingService = context.GetExtension<ITracingService>();
            //Create the context
            IWorkflowContext workFlowContext = context.GetExtension<IWorkflowContext>();
            IOrganizationServiceFactory serviceFactory = context.GetExtension<IOrganizationServiceFactory>();
            IOrganizationService service = serviceFactory.CreateOrganizationService(workFlowContext.UserId);


            string str_EmailSend = this.EmailSend.Get(context) == null ? Guid.Empty.ToString() : this.EmailSend.Get(context).Id.ToString();
            string str_lead = this.Lead.Get(context) == null ? Guid.Empty.ToString() : this.Lead.Get(context).Id.ToString();
            try
            {
                Microsoft.Xrm.Sdk.EntityReference Moniker2 = new Microsoft.Xrm.Sdk.EntityReference();
                Moniker2.Id = Lead.Get(context).Id;
                Moniker2.LogicalName = "lead";//Entity Name

                // Code Create Moniker for second Entity: New_CustomEntity
                Microsoft.Xrm.Sdk.EntityReference Moniker1 = new Microsoft.Xrm.Sdk.EntityReference();
                Moniker1.Id = EmailSend.Get(context).Id;
                Moniker1.LogicalName = "cdi_emailsend";
                tracingService.Trace("Started Assosiating");
                AssociateManyToManyEntityRecords(service, Moniker1, Moniker2, "cdi_emailsend_lead");


            }
            catch (Exception ex)
            {
                //throw new Exception(ex.Message.ToString());
                //tracingService.Trace("Exception" + ex.Message.ToString());
                this.Success.Set(context, false);
                //return;
            }
        }

        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,