create a record with plugin including with all fields in mscrm

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

namespace CreateBookingMovementFromEnquiry
{
    public class CreateBookingMovement : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            ITracingService trace = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(new Guid?(context.UserId));
            if (context.Depth > 1)
                return;

            Entity accountent = service.Retrieve(context.PrimaryEntityName, context.PrimaryEntityId, new Microsoft.Xrm.Sdk.Query.ColumnSet(true));
            //Entity entity= context.InputParameters["Target"] as Entity;
            //if (entity.LogicalName == "account")
            //{
            //}
            if (accountent != null && accountent.Contains("apy_booked"))
            {
                if (accountent.FormattedValues["apy_booked"].ToString() == "Booked")
                {
                    Entity contact = new Entity("contact");
                    contact["apy_lookup"] = new EntityReference(accountent.LogicalName, accountent.Id);
                    contact["apy_customerlookup"] = accountent.Contains("apy_customer") ? new EntityReference(accountent.GetAttributeValue<EntityReference>("apy_customer").LogicalName, accountent.GetAttributeValue<EntityReference>("apy_customer").Id) : null;
                    contact["apy_string"] = accountent.Contains("apy_name") ? accountent.GetAttributeValue<string>("apy_name") : string.Empty;
                    contact["apy_date"] = accountent.Contains("apy_date") ? accountent.GetAttributeValue<DateTime>("apy_date") : DateTime.Now;
                    contact["apy_bool"] = accountent.Contains("apy_existingcustomer") ? accountent.GetAttributeValue<bool>("apy_existingcustomer") : false;
                  if (contact.Attributes.Contains("OptionSetValue"))
                    {
                        contact["OptionSetValue"] = new OptionSetValue(((OptionSetValue)entity["OptionSetValue"]).Value);
                    }
 contact["Lookup"] = accountent.Contains("Lookup") ? new EntityReference(entity.GetAttributeValue<EntityReference>("Lookup").LogicalName, accountent.GetAttributeValue<EntityReference>("Lookup").Id) : null;
                    
                    if (accountent.Contains("apy_containerstatus"))
                    {
                        contact["apy_containerstatus"] = accountent.GetAttributeValue<OptionSetValue>("apy_containerstatus").Value;
                    }
                    contact["apy_int"] = accountent.Contains("apy_quantity") ? accountent.GetAttributeValue<int>("apy_quantity") : 0;
                    contact["apy_dateandtime"] = accountent.Contains("apy_validity") ? accountent.GetAttributeValue<DateTime>("apy_validity") : DateTime.Now;//DateTime.MinValue
                    contact["apy_currency"] = accountent.Contains("apy_totalcosts") ? accountent.GetAttributeValue<Money>("apy_totalcosts") : new Money(0);
 contact["apy_decimal"] = accountent.Contains("apy_decimal") ? accountent.GetAttributeValue<decimal>("apy_decimal") : 0;
                    service.Create(contact);
                }
            }
        }
    }
}

Comments

Popular posts from this blog

Meta Data Using WebApiRequest

Sample CRUD Operations Using C# in MSCRM

Basic Plugin Code in D365 using C#