N:N Relationship with Account related Custom Entity Records using Plugin

*Retrieving the Account related RelatedEntity records and updating the Account entity field 'new_relationshiprecords' with all relatedEntity names belongs to perticular Account entity.

* Plugin is Registered on Account Entity Update of 'name' field PreOperation/PostOperation with Synchronus.

 using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.Xrm.Sdk;

using Microsoft.Xrm.Sdk.Query;

using System.Linq;

using System.Threading.Tasks;

using Microsoft.Xrm.Sdk.Messages;

using Microsoft.Xrm.Sdk.Metadata;

using System.Diagnostics;

using System.Collections;

using System.Web;

using System.Xml;

using Microsoft.Xrm.Sdk.Client;

namespace getOptionValue

{

    public class updateAcoount : IPlugin

    {

        public void Execute(IServiceProvider serviceProvider)

        {

            ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

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

            try

            {

                string relationentitynames = null;

                string concat = null;

                string entity2 = "account";

                string entity1 = "new_relationentity";

                string relationshipEntityName = "new_account_new_relationentity";

                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)

                { // Obtain the target entity from the input parameters.  (registered on account entity)

                    Entity entity = (Entity)context.InputParameters["Target"];

                    QueryExpression query = new QueryExpression(entity1);

                    query.ColumnSet = new ColumnSet(true);

                    LinkEntity linkEntity1 = new LinkEntity(entity1, relationshipEntityName, "new_relationentityid", "new_relationentityid", JoinOperator.Inner);

                    LinkEntity linkEntity2 = new LinkEntity(relationshipEntityName, entity2, "accountid", "accountid", JoinOperator.Inner);

                    linkEntity1.LinkEntities.Add(linkEntity2);

                    query.LinkEntities.Add(linkEntity1);

                    // Add condition to match the Bike name with "Honda"

                    linkEntity1.LinkCriteria = new FilterExpression();

                    linkEntity1.LinkCriteria.AddCondition(new ConditionExpression("accountid", ConditionOperator.Equal, entity.Id));

                    EntityCollection collRecords = service.RetrieveMultiple(query);

                    for (int i = 0; i < collRecords.Entities.Count; i++)

                    {

                        relationentitynames = collRecords.Entities[i].GetAttributeValue<string>("new_name");

                        concat = String.Concat(concat, relationentitynames, ";");

                    }                

                    Entity parententity = new Entity(entity.LogicalName);

                    parententity.Id = entity.Id;

                    parententity.Attributes["new_relationshiprecords"] = concat;

                    service.Update(parententity);

                 }   

            }

            catch (Exception ex)

            {

                throw new InvalidPluginExecutionException(ex.Message);

            }

        }

    }

}


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