Plugin C# code Get Soure To Set Target – Same Form

using System;
using Microsoft.Xrm.Sdk;

namespace SourcetoTarget
{
    public class SourcetoTarget : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            ITracingService tracingService =
                (ITracingService)serviceProvider.GetService(typeof(ITracingService));

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

            if (context.Depth > 1)
            {
                return;
            }

            if (context.InputParameters.Contains("Target") &&
                context.InputParameters["Target"] is Entity)
            {
                Entity sourceRecordInfo = (Entity)context.InputParameters["Target"];

                IOrganizationServiceFactory serviceFactory =
                    (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                try
                {
                    string firstName = " ";
                    string lastName = " ";
                    Money sourceValue = null;
                    OptionSetValue areuMajorSource = null;
                    DateTime? dob_sourceValue = null;
                    int? agesourceValue = null;
                    OptionSetValue genderSource = null;
                    string expsourceValue = null;

                    if (context.MessageName.ToLower() == "create")
                    {
                        // GET single line
                        if (sourceRecordInfo.Contains("sdc_firstname") && sourceRecordInfo["sdc_firstname"] != null)
                        {
                            firstName = (string)sourceRecordInfo["sdc_firstname"];
                        }
                        // GET single line
                        if (sourceRecordInfo.Contains("sdc_secondname") && sourceRecordInfo["sdc_secondname"] != null)
                        {
                            lastName = (string)sourceRecordInfo["sdc_secondname"];
                        }
                        // GET Currency
                        if (sourceRecordInfo.Contains("sdc_salarypermonthsource") && sourceRecordInfo["sdc_salarypermonthsource"] != null)
                        {
                            sourceValue = (Money)sourceRecordInfo["sdc_salarypermonthsource"];
                        }
                        // GET Option set
                        if (sourceRecordInfo.Contains("sdc_areyoumajorsource") && sourceRecordInfo["sdc_areyoumajorsource"] != null)
                        {
                            areuMajorSource = (OptionSetValue)sourceRecordInfo["sdc_areyoumajorsource"];
                        }
                        // GET Date and time
                        if (sourceRecordInfo.Contains("sdc_dateofbirthsource") && sourceRecordInfo["sdc_dateofbirthsource"] != null)
                        {
                            dob_sourceValue = (DateTime?)sourceRecordInfo["sdc_dateofbirthsource"];
                        }
                        // GET Whole number
                        if (sourceRecordInfo.Contains("sdc_agesource") && sourceRecordInfo["sdc_agesource"] != null)
                        {
                            agesourceValue = (int?)sourceRecordInfo["sdc_agesource"];
                        }
                        // GET Option set
                        if (sourceRecordInfo.Contains("sdc_gendersource") && sourceRecordInfo["sdc_gendersource"] != null)
                        {
                            genderSource = (OptionSetValue)sourceRecordInfo["sdc_gendersource"];
                        }
                        // GET Multiline
                        if (sourceRecordInfo.Contains("sdc_yourexperiencedetailssource") && sourceRecordInfo["sdc_yourexperiencedetailssource"] != null)
                        {
                            expsourceValue = (string)sourceRecordInfo["sdc_yourexperiencedetailssource"];
                        }

                        // SET Single line
                        sourceRecordInfo["sdc_name"] = firstName + " " + lastName;
                        // SET Currency
                        sourceRecordInfo["sdc_salarypermonthtarget"] = sourceValue;
                        // SET Option set
                        sourceRecordInfo["sdc_areyoumajortarget"] = areuMajorSource;
                        // SET Date and time
                        sourceRecordInfo["sdc_dateofbirthtarget"] = dob_sourceValue;
                        // SET Whole number
                        sourceRecordInfo["sdc_agetarget"] = agesourceValue;
                        // SET Option set
                        sourceRecordInfo["sdc_gendertarget"] = genderSource;
                        // SET Multiline
                        sourceRecordInfo["sdc_yourexperiencedetailstarget"] = expsourceValue;
                    }
                    else
                    {
                        // UPDATE event - read from post image
                        Entity postValues = (Entity)context.PostEntityImages["latestvalues"];

                        // GET single line
                        if (postValues.Contains("sdc_firstname") && postValues["sdc_firstname"] != null)
                        {
                            firstName = (string)postValues["sdc_firstname"];
                        }
                        // GET single line
                        if (postValues.Contains("sdc_secondname") && postValues["sdc_secondname"] != null)
                        {
                            lastName = (string)postValues["sdc_secondname"];
                        }
                        // GET Currency
                        if (postValues.Contains("sdc_salarypermonthsource") && postValues["sdc_salarypermonthsource"] != null)
                        {
                            sourceValue = (Money)postValues["sdc_salarypermonthsource"];
                        }
                        // GET Option set
                        if (postValues.Contains("sdc_areyoumajorsource") && postValues["sdc_areyoumajorsource"] != null)
                        {
                            areuMajorSource = (OptionSetValue)postValues["sdc_areyoumajorsource"];
                        }
                        // GET Date and time
                        if (postValues.Contains("sdc_dateofbirthsource") && postValues["sdc_dateofbirthsource"] != null)
                        {
                            dob_sourceValue = (DateTime?)postValues["sdc_dateofbirthsource"];
                        }
                        // GET Whole number
                        if (postValues.Contains("sdc_agesource") && postValues["sdc_agesource"] != null)
                        {
                            agesourceValue = (int?)postValues["sdc_agesource"];
                        }
                        // GET Option set
                        if (postValues.Contains("sdc_gendersource") && postValues["sdc_gendersource"] != null)
                        {
                            genderSource = (OptionSetValue)postValues["sdc_gendersource"];
                        }
                        // GET Multiline
                        if (postValues.Contains("sdc_yourexperiencedetailssource") && postValues["sdc_yourexperiencedetailssource"] != null)
                        {
                            expsourceValue = (string)postValues["sdc_yourexperiencedetailssource"];
                        }

                        // UPDATE entity
                        Entity entUpdate = new Entity();
                        entUpdate.LogicalName = sourceRecordInfo.LogicalName;
                        entUpdate.Id = sourceRecordInfo.Id;

                        // SET Single line
                        entUpdate["sdc_name"] = firstName + " " + lastName;
                        // SET Currency
                        entUpdate["sdc_salarypermonthtarget"] = sourceValue;
                        // SET Option set
                        entUpdate["sdc_areyoumajortarget"] = areuMajorSource;
                        // SET Date and time
                        entUpdate["sdc_dateofbirthtarget"] = dob_sourceValue;
                        // SET Whole number
                        entUpdate["sdc_agetarget"] = agesourceValue;
                        // SET Option set
                        entUpdate["sdc_gendertarget"] = genderSource;
                        // SET Multiline
                        entUpdate["sdc_yourexperiencedetailstarget"] = expsourceValue;

                        service.Update(entUpdate);
                    }
                }
                catch (Exception ex)
                {
                    tracingService.Trace("SourcetoTarget: {0}", ex.ToString());
                    throw;
                }
            }
        }
    }
}

Leave a Comment