Ejemplo n.º 1
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 ExecutePostVehicleInTransitTransferReceivingCreate(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }

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

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

            try
            {
                VehicleInTransitTransferReceivingHandler receivingHandler = new VehicleInTransitTransferReceivingHandler(service, trace);
                Guid             intransitTransferId      = entity.GetEntityReferenceGuid("gsc_intransittransferid");
                EntityCollection receivingTransferDetails = receivingHandler.GetIntransitTransferDetails(intransitTransferId);
                Entity           receivingTransfer        = service.Retrieve("gsc_iv_vehicleintransittransfer", intransitTransferId, new Microsoft.Xrm.Sdk.Query.ColumnSet(true));

                receivingHandler.CreateReceivingDetails(receivingTransfer, receivingTransferDetails, entity.Id);
                receivingHandler.GenerateComponents(entity);
            }
            catch (Exception ex)
            {
                //throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace, Environment.NewLine, error));
                throw new InvalidPluginExecutionException(ex.Message);
            }
        }
        /// <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 ExecutePreVehicleInTransitTransferReceivingCreate(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }

            // TODO: Implement your custom Plug-in business logic.

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

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

            try
            {
                VehicleInTransitTransferReceivingHandler receivingHandler = new VehicleInTransitTransferReceivingHandler(service, trace);

                Guid             inTransitId         = CommonHandler.GetEntityReferenceIdSafe(entity, "gsc_intransittransferid");
                EntityCollection inTransitCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_vehicleintransittransfer", "gsc_iv_vehicleintransittransferid", inTransitId, service,
                                                                                               null, Microsoft.Xrm.Sdk.Query.OrderType.Ascending, new[] { "gsc_sourcebranchid", "gsc_sourcesiteid", "gsc_viasiteid", "gsc_destinationbranchid", "gsc_destinationbranchid" });

                entity = receivingHandler.PopulateSites(entity, inTransitCollection.Entities[0]);

                // check if in-transit transfer already exists.
                bool isDuplicate = receivingHandler.DetectDuplicate(entity);

                if (isDuplicate)
                {
                    throw new InvalidPluginExecutionException("Duplicate detected, In-Transit Transfer already exist.");
                }
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException(ex.Message);
            }
        }
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 ExecutePostVehicleInTransitTransferReceivingUpdate(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 vehicleTransitReceiving = (Entity)context.InputParameters["Target"];

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

            if (vehicleTransitReceiving.LogicalName != "gsc_iv_vehicleintransittransferreceiving")
            {
                return;
            }

            if (context.Mode == 0) //Synchronous Plug-in
            {
                try
                {
                    var preStatus = preImageEntity.Contains("gsc_intransitstatus") ? preImageEntity.GetAttributeValue <OptionSetValue>("gsc_intransitstatus").Value
                        : 0;
                    var preDestinationSite = preImageEntity.Contains("gsc_destinationsiteid") ? preImageEntity.GetAttributeValue <EntityReference>("gsc_destinationsiteid").Id
                        : Guid.Empty;

                    var postStatus = postImageEntity.Contains("gsc_intransitstatus") ? postImageEntity.GetAttributeValue <OptionSetValue>("gsc_intransitstatus").Value
                        : 0;
                    var postDestinationSite = postImageEntity.Contains("gsc_destinationsiteid") ? postImageEntity.GetAttributeValue <EntityReference>("gsc_destinationsiteid").Id
                        : Guid.Empty;


                    VehicleInTransitTransferReceivingHandler vehicleReceivingHandler = new VehicleInTransitTransferReceivingHandler(service, trace);

                    if (preDestinationSite != postDestinationSite && postDestinationSite != Guid.Empty)
                    {
                        vehicleReceivingHandler.CopyDestinationSite(postImageEntity);
                    }

                    //BL for postStatus changed to Received
                    if (preStatus != postStatus)
                    {
                        if (postStatus == 100000001)//Received
                        {
                            vehicleReceivingHandler.ReceiveTransfer(preImageEntity);
                        }
                        if (postStatus == 100000002)//Cancelled
                        {
                            vehicleReceivingHandler.CancelTransfer(preImageEntity);
                        }
                        vehicleReceivingHandler.CopyStatus(postImageEntity);
                    }
                }

                catch (Exception ex)
                {
                    throw new InvalidPluginExecutionException(ex.Message);
                }
            }
        }
Ejemplo n.º 4
0
 public VehicleInTransitTransferHandler(IOrganizationService service, ITracingService trace, VehicleInTransitTransferReceivingHandler receivingHandler)
 {
     _organizationService = service;
     _tracingService      = trace;
     _receivingHandler    = receivingHandler;
 }
        /// <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));
                    }
                }
            }
        }