Recent Posts

ads

Based on parent entity field value automatically create child records using plugin in microsoft dynamics crm

Hi folks today I accompanied one new prerequisites in microsoft dynamics crm.

Prerequisite: 


I have two  microsoft crm Entites like parent and chaild. In the parent substance I will make the Number of   child   field. At the point when I am making the parent substance I might want to give number of  child field=4 then naturally I have to make 4 child element records in microsoft crm.


Arrangement: 


Here I used to plugin to met with the prerequisites. 


Plugin:

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


namespace chaildcreation
{
    public class chaildcreation : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext Context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            if (Context.InputParameters.Contains("Target") && Context.InputParameters["Target"] is Entity)
            {
                ITracingService TraceService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
                IOrganizationServiceFactory ServiceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService Service = ServiceFactory.CreateOrganizationService(Context.UserId);
                Entity parentEntity = (Entity)Context.InputParameters["Target"];

                try
                {

                    if (parentEntity.Contains("lvt_noofchailds"))
                    {
                        int Noofchailds = (int)parentEntity["lvt_noofchailds"];
                        {
                            if (Noofchailds > 0)
                            {
                                for (int i = 0; i < Noofchailds; i++)
                                {
                                    // create chaild entity
                                    Entity chaild = new Entity("lvt_chaild");
                                    chaild["lvt_name"] = parentEntity["lvt_name"];
          
                                    

                                    
                                    
                                    Guid chaildId = Service.Create(chaild);
                                    //ASSOCIATED CHAILD TO PARENT

                                }
                            }
                        }
                    }
                }

                catch (Exception Ex)
                {

                    throw new InvalidPluginExecutionException(Ex.Message);
                }

                }
               
                                }
                            }

                        }
           


Message : Create
Stage   : post_operation

Post a Comment

0 Comments