/// <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 ExecutePreValidateVehicleTransferCreate(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }

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

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

            try
            {
                VehicleTransferHandler vehicleTransferHandler = new VehicleTransferHandler(service, trace);
                vehicleTransferHandler.PopulateFields(vehicleTransfer);
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace, Environment.NewLine, error));
            }
        }
        /// <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 ExecutePreValidateVehicleTransferDelete(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }

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

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

            try
            {
                EntityCollection vehicleTransferCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_vehicletransfer", "gsc_iv_vehicletransferid", vehicleTransfer.Id, service,
                                                                                                     null, OrderType.Ascending, new[] { "gsc_transferstatus", "gsc_vehicletransferpn", "gsc_siteid" });

                VehicleTransferHandler vehicleTransferHandler = new VehicleTransferHandler(service, trace);
                vehicleTransferHandler.ValidateDelete(vehicleTransferCollection.Entities[0]);
            }

            catch (Exception ex)
            {
                if (ex.Message.Contains("Unable to delete Posted Vehicle Transfer transactions."))
                {
                    throw new InvalidPluginExecutionException("Unable to delete Posted Vehicle Transfer transactions.");
                }
                else
                {
                    throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace));
                }
            }
        }
Ejemplo n.º 3
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 ExecutePostVehicleTransferUpdate(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }

            IPluginExecutionContext context = localContext.PluginExecutionContext;

            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 vehicleTransferEntity = (Entity)context.InputParameters["Target"];

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

            if (vehicleTransferEntity.LogicalName != "gsc_iv_vehicletransfer")
            {
                return;
            }

            if (context.Mode == 0) //Synchronous Plug-in
            {
                try
                {
                    VehicleTransferHandler vehicleTransferHandler = new VehicleTransferHandler(service, trace);

                    string preInventoryId = preImageEntity.Contains("gsc_inventoryidtoallocate") ? preImageEntity.GetAttributeValue <string>("gsc_inventoryidtoallocate")
                        : string.Empty;
                    var preTransactionStatus = preImageEntity.Contains("gsc_transferstatus") ? preImageEntity.GetAttributeValue <OptionSetValue>("gsc_transferstatus").Value
                        : 0;
                    var preTransactionStatusCopy = preImageEntity.Contains("gsc_transferstatuscopy") ? preImageEntity.GetAttributeValue <OptionSetValue>("gsc_transferstatuscopy").Value
                        : 0;
                    String preImageAllocatedVehicleToDelete = preImageEntity.Contains("gsc_allocateditemstodelete")
                        ? preImageEntity.GetAttributeValue <String>("gsc_allocateditemstodelete")
                        : String.Empty;
                    Guid preImageSiteId = preImageEntity.GetAttributeValue <EntityReference>("gsc_siteid") != null
                        ? preImageEntity.GetAttributeValue <EntityReference>("gsc_siteid").Id
                        : Guid.Empty;

                    string postInventoryId = postImageEntity.Contains("gsc_inventoryidtoallocate") ? postImageEntity.GetAttributeValue <string>("gsc_inventoryidtoallocate")
                        : string.Empty;
                    var postTransactionStatus = postImageEntity.Contains("gsc_transferstatus") ? postImageEntity.GetAttributeValue <OptionSetValue>("gsc_transferstatus").Value
                        : 0;
                    String postImageAllocatedVehicleToDelete = postImageEntity.Contains("gsc_allocateditemstodelete")
                        ? postImageEntity.GetAttributeValue <String>("gsc_allocateditemstodelete")
                        : String.Empty;
                    Guid postImageSiteId = postImageEntity.GetAttributeValue <EntityReference>("gsc_siteid") != null
                      ? postImageEntity.GetAttributeValue <EntityReference>("gsc_siteid").Id
                      : Guid.Empty;

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

                    if (preImageSiteId != postImageSiteId)
                    {
                        // vehicleTransferHandler.UpdateDestinationSite(postImageEntity);
                    }

                    //BL for update of transaction status to posted
                    if (preTransactionStatus != postTransactionStatus && postTransactionStatus == 100000000)
                    {
                        if (vehicleTransferHandler.RestrictPosting(postImageEntity) == true)
                        {
                            throw new InvalidPluginExecutionException("Cannot proceed with posting, one of the transferred vehicle is in-transit site.");
                        }
                        else
                        {
                            vehicleTransferHandler.PostTransaction(postImageEntity);
                        }
                    }

                    if (preTransactionStatus != postTransactionStatus)
                    {
                        vehicleTransferHandler.CancelTransaction(postImageEntity);
                        vehicleTransferHandler.UpdateVTSatus(postImageEntity);
                    }

                    if (preImageAllocatedVehicleToDelete != postImageAllocatedVehicleToDelete && postImageAllocatedVehicleToDelete != String.Empty)
                    {
                        vehicleTransferHandler.DeleteAllocatedVehicle(postImageEntity);
                        vehicleTransferHandler.UpdateTransferredVehicle(postImageEntity);
                    }
                }

                catch (Exception ex)
                {
                    throw new InvalidPluginExecutionException(ex.Message);
                    // throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace));
                }
            }
        }