Association of records using plugin in MS CRM

using Microsoft.Xrm.Sdk;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;

namespace Marketing_List
{
    public class Account_Association:IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = factory.CreateOrganizationService(context.UserId);

            if(context.InputParameters["Target"]is Entity && context.InputParameters.Contains("Target"))
            {
                Entity product = context.InputParameters["Target"] as Entity;
                EntityReference marketingList=(EntityReference)product.Attributes["new_marketinglist"];
                Entity marketing = service.Retrieve("list", marketingList.Id, new ColumnSet(true));
                if (marketing.Attributes.Contains("createdfromcode"))
                {
                    OptionSetValue targetType =(OptionSetValue)marketing.Attributes["createdfromcode"];
                    if(targetType.Value==1)
                    {
                        string fetchAccounts = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'><entity name='account'><attribute name='name' /><attribute name='primarycontactid' /><attribute name='telephone1'/><attribute name='accountid' /><order attribute='name' descending='false' /><link-entity name='listmember' from='entityid' to='accountid' visible='false' intersect='true'><link-entity name='list' from='listid' to='listid' alias='ac'><filter type='and'><condition attribute='listid' operator='eq' uiname='Test Marketing List' uitype='list' value='"+marketingList.Id+"'/></filter></link-entity></link-entity></entity></fetch>";
                        EntityCollection ec = service.RetrieveMultiple( new FetchExpression( fetchAccounts));
                        if(ec.Entities.Count>0)
                        {
                           // OrganizationRequestCollection associateRequestCollection = new OrganizationRequestCollection();
                            //Relationship relationship = new Relationship("new_new_myproducts_account");

                            //// Associate the contact record to Account
                            for (int i = 0; i < ec.Entities.Count;i++ )
                            {
                                //EntityReferenceCollection er=new EntityReferenceCollection();
                                //er.Add(new EntityReference(ec.Entities[i].LogicalName, ec.Entities[i].Id));

                                AssociateRequest assreq = new AssociateRequest();
                                assreq.Target = new EntityReference(product.LogicalName, product.Id);
                                assreq.RelatedEntities = new EntityReferenceCollection();
                                assreq.RelatedEntities.Add(new EntityReference(ec.Entities[i].LogicalName, ec.Entities[i].Id));
                                assreq.Relationship = new Relationship("new_new_myproducts_account");
                                service.Execute(assreq);

                                //service.Associate(product.LogicalName, product.Id, relationship,er);
                            }
                             
                        }
                    }

                }
            }
        }
    }
}

Comments

Popular posts from this blog

Basic Plugin Code in D365 using C#

CURD (Create, Update, Retrieve and Delete) Operation in D365 using Power Shell Script

Meta Data Using WebApiRequest