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

            IPluginExecutionContext context = localContext.PluginExecutionContext;

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

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

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

            if (cfoQuantityDetailEntity.LogicalName != "gsc_sls_committedfirmorderquantitydetail")
            {
                return;
            }

            if (context.Mode == 0) //synchronous plugin
            {
                try
                {
                    CommittedFirmOrderQuantityDetailHandler cfoQuantityDetailHandler = new CommittedFirmOrderQuantityDetailHandler(service, trace);
                    cfoQuantityDetailHandler.RelateOrderPlanningRecord(cfoQuantityDetailEntity);
                }
                catch (Exception ex)
                {
                    //throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace, Environment.NewLine, error));
                    throw new InvalidPluginExecutionException(ex.Message);
                }
            }
        }
Exemple #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 ExecutePostCommittedFirmOrderQuantityDetailUpdate(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;

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

            Entity cfoEntity = (Entity)context.InputParameters["Target"];

            if (cfoEntity.LogicalName != "gsc_sls_committedfirmorderquantitydetail")
            {
                return;
            }
            string message = context.MessageName;

            if (context.Mode == 0) //Synchronous Plugin
            {
                try
                {
                    CommittedFirmOrderQuantityDetailHandler cfoQuantityDetailHandler = new CommittedFirmOrderQuantityDetailHandler(service, trace);
                    CommittedFirmOrderQuantityHandler       cfoQuantityHandler       = new CommittedFirmOrderQuantityHandler(service, trace);

                    var preAllocated = preImageEntity.Contains("gsc_allocatedquantity")
                      ? preImageEntity.GetAttributeValue <Int32>("gsc_allocatedquantity")
                      : 0;
                    var preVPOBalance = preImageEntity.Contains("gsc_vpobalance")
                      ? preImageEntity.GetAttributeValue <Int32>("gsc_vpobalance")
                      : 0;
                    var preSelected  = preImageEntity.GetAttributeValue <Boolean>("gsc_selected");
                    var preProductId = preImageEntity.Contains("gsc_productid")
                        ? preImageEntity.GetAttributeValue <EntityReference>("gsc_productid").Id
                        : Guid.Empty;

                    var postAllocated = postImageEntity.Contains("gsc_allocatedquantity")
                     ? postImageEntity.GetAttributeValue <Int32>("gsc_allocatedquantity")
                     : 0;
                    var postVPOBalance = postImageEntity.Contains("gsc_vpobalance")
                      ? postImageEntity.GetAttributeValue <Int32>("gsc_vpobalance")
                      : 0;
                    var postSelected  = postImageEntity.GetAttributeValue <Boolean>("gsc_selected");
                    var postProductId = postImageEntity.Contains("gsc_productid")
                       ? postImageEntity.GetAttributeValue <EntityReference>("gsc_productid").Id
                       : Guid.Empty;

                    if (preAllocated != postAllocated)
                    {
                        cfoQuantityDetailHandler.UpdateVPOBalance(postImageEntity);
                        cfoQuantityDetailHandler.ReplicateAllocatedQuantity(postImageEntity);
                    }

                    if (preVPOBalance != postVPOBalance)
                    {
                        cfoQuantityDetailHandler.UpdateStatustoCompleted(postImageEntity);
                    }
                    if (preProductId != postProductId)
                    {
                        cfoQuantityDetailHandler.RelateOrderPlanningRecord(postImageEntity);
                    }
                }

                catch (Exception ex)
                {
                    throw new InvalidPluginExecutionException(ex.Message);
                }
            }
        }