/// <summary>
        /// Executes the plug-in.
        /// </summary>
        /// <param name="localContext">The <see cref="LocalPluginContext"/> which contains the
        /// <see cref="IPluginExecutionContext"/>,
        /// <see cref="IOrganizationService"/>
        /// and <see cref="ITracingService"/>
        /// </param>
        /// <remarks>
        /// For improved performance, Microsoft Dynamics CRM caches plug-in instances.
        /// The plug-in's Execute method should be written to be stateless as the constructor
        /// is not called for every invocation of the plug-in. Also, multiple system threads
        /// could execute the plug-in at the same time. All per invocation state information
        /// is stored in the context. This means that you should not use global variables in plug-ins.
        /// </remarks>
        protected void ExecutePreValidateVehicleInTransitTransferCreate(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }

            IPluginExecutionContext context = localContext.PluginExecutionContext;
            IOrganizationService    service = localContext.OrganizationService;
            ITracingService         trace   = localContext.TracingService;
            Entity vehicleInTransitTransfer = (Entity)context.InputParameters["Target"];

            string message = context.MessageName;
            string error   = "";

            try
            {
                VehicleInTransitTransferHandler vehicleInTransitTransferHandler = new VehicleInTransitTransferHandler(service, trace);
                vehicleInTransitTransferHandler.PopulateFields(vehicleInTransitTransfer);
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace, Environment.NewLine, error));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Executes the plug-in.
        /// </summary>
        /// <param name="localContext">The <see cref="LocalPluginContext"/> which contains the
        /// <see cref="IPluginExecutionContext"/>,
        /// <see cref="IOrganizationService"/>
        /// and <see cref="ITracingService"/>
        /// </param>
        /// <remarks>
        /// For improved performance, Microsoft Dynamics CRM caches plug-in instances.
        /// The plug-in's Execute method should be written to be stateless as the constructor
        /// is not called for every invocation of the plug-in. Also, multiple system threads
        /// could execute the plug-in at the same time. All per invocation state information
        /// is stored in the context. This means that you should not use global variables in plug-ins.
        /// </remarks>
        protected void ExecutePreValidateVehicleInTransitTransferDelete(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }

            IPluginExecutionContext context = localContext.PluginExecutionContext;
            IOrganizationService    service = localContext.OrganizationService;
            ITracingService         trace   = localContext.TracingService;
            var    vehicleInTransit         = (EntityReference)context.InputParameters["Target"];
            string message = context.MessageName;

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

            try
            {
                EntityCollection vehicleInTransitCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_vehicleintransittransfer", "gsc_iv_vehicleintransittransferid", vehicleInTransit.Id, service,
                                                                                                      null, OrderType.Ascending, new[] { "gsc_intransittransferstatus" });

                VehicleInTransitTransferHandler vehicleInTransitHandler = new VehicleInTransitTransferHandler(service, trace);
                vehicleInTransitHandler.ValidateTransaction(vehicleInTransitCollection.Entities[0], message);
            }

            catch (Exception ex)
            {
                if (ex.Message.Contains("Unable to delete Shipped Vehicle In-Transit Transfer record."))
                {
                    throw new InvalidPluginExecutionException("Unable to delete Shipped Vehicle In-Transit Transfer record.");
                }
                else
                {
                    throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace));
                }
            }
        }
        /// <summary>
        /// Executes the plug-in.
        /// </summary>
        /// <param name="localContext">The <see cref="LocalPluginContext"/> which contains the
        /// <see cref="IPluginExecutionContext"/>,
        /// <see cref="IOrganizationService"/>
        /// and <see cref="ITracingService"/>
        /// </param>
        /// <remarks>
        /// For improved performance, Microsoft Dynamics CRM caches plug-in instances.
        /// The plug-in's Execute method should be written to be stateless as the constructor
        /// is not called for every invocation of the plug-in. Also, multiple system threads
        /// could execute the plug-in at the same time. All per invocation state information
        /// is stored in the context. This means that you should not use global variables in plug-ins.
        /// </remarks>
        protected void ExecutePostVehicleInTransitTransferUpdate(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }

            IPluginExecutionContext context = localContext.PluginExecutionContext;

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

            Entity preImageEntity  = (context.PreEntityImages != null && context.PreEntityImages.Contains(this.preImageAlias)) ? context.PreEntityImages[this.preImageAlias] : null;
            Entity postImageEntity = (context.PostEntityImages != null && context.PostEntityImages.Contains(this.postImageAlias)) ? context.PostEntityImages[this.postImageAlias] : null;

            IOrganizationService service    = localContext.OrganizationService;
            ITracingService      trace      = localContext.TracingService;
            Entity vehicleInTransitTransfer = (Entity)context.InputParameters["Target"];

            if (!(context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity))
            {
                return;
            }

            if (vehicleInTransitTransfer.LogicalName != "gsc_iv_vehicleintransittransfer")
            {
                return;
            }

            if (context.Mode == 0) //Synchronous Plug-in
            {
                string message = context.MessageName;

                try
                {
                    string preInventoryId = preImageEntity.Contains("gsc_inventoryidtoallocate") ? preImageEntity.GetAttributeValue <string>("gsc_inventoryidtoallocate")
                        : String.Empty;
                    var preTransferStatus = preImageEntity.Contains("gsc_intransittransferstatus") ? preImageEntity.GetAttributeValue <OptionSetValue>("gsc_intransittransferstatus").Value
                        : 0;
                    String preImageAllocatedItemsToDelete = preImageEntity.Contains("gsc_allocateditemstodelete")
                        ? preImageEntity.GetAttributeValue <String>("gsc_allocateditemstodelete")
                        : String.Empty;
                    Guid preImageSourceSite = preImageEntity.GetAttributeValue <EntityReference>("gsc_sourcesiteid") != null
                        ? preImageEntity.GetAttributeValue <EntityReference>("gsc_sourcesiteid").Id
                        : Guid.Empty;

                    string postInventoryId = postImageEntity.Contains("gsc_inventoryidtoallocate") ? postImageEntity.GetAttributeValue <string>("gsc_inventoryidtoallocate")
                        : String.Empty;
                    var postTransferStatus = postImageEntity.Contains("gsc_intransittransferstatus") ? postImageEntity.GetAttributeValue <OptionSetValue>("gsc_intransittransferstatus").Value
                        : 0;
                    String postImageAllocatedItemsToDelete = postImageEntity.Contains("gsc_allocateditemstodelete")
                        ? postImageEntity.GetAttributeValue <String>("gsc_allocateditemstodelete")
                        : String.Empty;
                    Guid postImageSourceSite = postImageEntity.GetAttributeValue <EntityReference>("gsc_sourcesiteid") != null
                        ? postImageEntity.GetAttributeValue <EntityReference>("gsc_sourcesiteid").Id
                        : Guid.Empty;

                    VehicleInTransitTransferReceivingHandler receivingHandler        = new VehicleInTransitTransferReceivingHandler(service, trace);
                    VehicleInTransitTransferHandler          vehicleInTransitHandler = new VehicleInTransitTransferHandler(service, trace, receivingHandler);

                    //BL for update of inventoryidtoallocate
                    if (preInventoryId != postInventoryId && postInventoryId != string.Empty)
                    {
                        vehicleInTransitHandler.AllocateVehicle(postImageEntity);
                    }

                    //BL for status change to Cancelled
                    if (preTransferStatus != postTransferStatus && postTransferStatus == 100000003)
                    {
                        vehicleInTransitHandler.ValidateTransaction(preImageEntity, message);
                    }
                    //BL for status change to Shipped
                    if (preTransferStatus != postTransferStatus && postTransferStatus == 100000001)
                    {
                        bool isShipping = postImageEntity.GetAttributeValue <bool>("gsc_isshipping");
                        vehicleInTransitHandler.ShipVehicle(preImageEntity, isShipping);
                    }
                    if (preImageAllocatedItemsToDelete != postImageAllocatedItemsToDelete && postImageAllocatedItemsToDelete != String.Empty)
                    {
                        vehicleInTransitHandler.DeleteInTransitTransferVehicle(postImageEntity);
                    }
                    if (preTransferStatus != postTransferStatus)
                    {
                        vehicleInTransitHandler.ReplicateVehicleInTransitTransferStatus(postImageEntity);
                    }
                    if (preImageSourceSite != postImageSourceSite)
                    {
                        vehicleInTransitHandler.ReplicateSourceField(postImageEntity);
                    }
                }

                catch (Exception ex)
                {
                    if (ex.Message.Contains("The inventory for entered vehicle is not available."))
                    {
                        throw new InvalidPluginExecutionException("The inventory for entered vehicle is not available.");
                    }
                    else if (ex.Message.Contains("Unable to cancel Shipped Vehicle In-Transit Transfer record."))
                    {
                        throw new InvalidPluginExecutionException("Unable to cancel Shipped Vehicle In-Transit Transfer record.");
                    }
                    else if (ex.Message.Contains("No Allocated Vehicle to Ship."))
                    {
                        throw new InvalidPluginExecutionException("No Allocated Vehicle to Ship.");
                    }
                    else
                    {
                        throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace));
                    }
                }
            }
        }