Please wait, loading...

 

Copy One LookUp Field Value To Anoher LookUp using Plugin (EntityReference) ( Copy Lookup to Lookup ) In MSCRM 2011 2013

March 20, 2014
Note : There is two way to copy lookup to Lookup value .

1. Both Lookup are on same entity  //   tes.new_testonlookups= test.new_testonlookup;

2 using EntityRefrence if the Process include more than 2 Entities.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
using System.ServiceModel;
using Microsoft.Xrm.Client;
using Microsoft.Xrm.Portal;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;

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

            // retrive record data/value on which plugin firing
            new_calculate test = (new_calculate)service.Retrieve(new_calculate.EntityLogicalName, context.PrimaryEntityId, new ColumnSet(true));

            // r contain Guid of the look up
            Guid r = test.new_testonlookup.Id;

     

            // retrive fields from 2nd entity where wants to copy the string
            new_testto tes = new new_testto();
            tes.new_name = test.new_name;
         
            // reff1 Cantain Reffrence of lookup with guid r
            EntityReference reff1 = new EntityReference(new_teston.EntityLogicalName, r);
            tes.new_testonlookups = reff1;

            // tes.new_testonlookups= test.new_testonlookup;  ( we can copy one look up to another lookup of same entity directlly )
             service.Create(tes);

        }

    }

}


SOURCE : JUST2CODE.IN
Subscribe to our YouTube channel : https://www.youtube.com/user/TheRussell2012

https://i0.wp.com/microsoftdynamics.in/wp-content/uploads/2020/04/Microsoftdynamics365.png?fit=640%2C651&ssl=1
Microsoft Dynamics Community Profile

Learn more