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

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

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

            try
            {
                ShippingClaimHandler shippingClaimHandler = new ShippingClaimHandler(service, trace);
                shippingClaimHandler.ReplicateReceivingTransactionFields(shippingClaimEntity, message);
            }
            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 ExecutePostShippingClaimCreate(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }

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

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

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

            try
            {
                ShippingClaimHandler shippingClaimHandler = new ShippingClaimHandler(service, trace);

                EntityCollection shippingClaimRecords = CommonHandler.RetrieveRecordsByOneValue("gsc_sls_shippingclaim", "gsc_sls_shippingclaimid", shippingClaimEntity.Id, service, null, OrderType.Ascending,
                                                                                                new[] { "gsc_receivingtransactionid" });

                if (shippingClaimRecords != null && shippingClaimRecords.Entities.Count > 0)
                {
                    Entity shippingClaim = shippingClaimRecords.Entities[0];
                    shippingClaimHandler.ReplicateReceivingTransactionDetailFields(shippingClaim);
                }
            }
            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 #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 ExecutePostShippingClaimUpdate(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 shippingClaimEntity   = (Entity)context.InputParameters["Target"];

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

            if (shippingClaimEntity.LogicalName != "gsc_sls_shippingclaim")
            {
                return;
            }

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

                try
                {
                    #region Pre-images
                    Guid preImageReceivingTransaction = preImageEntity.GetAttributeValue <EntityReference>("gsc_receivingtransactionid") != null
                        ? preImageEntity.GetAttributeValue <EntityReference>("gsc_receivingtransactionid").Id
                        : Guid.Empty;

                    Int32 preImageScrStatus = preImageEntity.GetAttributeValue <OptionSetValue>("gsc_scrstatus") != null
                        ? preImageEntity.GetAttributeValue <OptionSetValue>("gsc_scrstatus").Value
                        : 0;

                    #endregion

                    #region Post-images
                    Guid postImageReceivingTransaction = postImageEntity.GetAttributeValue <EntityReference>("gsc_receivingtransactionid") != null
                        ? postImageEntity.GetAttributeValue <EntityReference>("gsc_receivingtransactionid").Id
                        : Guid.Empty;

                    Int32 postImageScrStatus = postImageEntity.GetAttributeValue <OptionSetValue>("gsc_scrstatus") != null
                        ? postImageEntity.GetAttributeValue <OptionSetValue>("gsc_scrstatus").Value
                        : 0;

                    #endregion

                    ShippingClaimHandler shippingClaimHandler = new ShippingClaimHandler(service, trace);

                    //Functions on Invoice No change
                    if (preImageReceivingTransaction != postImageReceivingTransaction)
                    {
                        shippingClaimHandler.DeleteExistingShippingClaimDetail(postImageEntity);
                        shippingClaimHandler.ReplicateReceivingTransactionFields(postImageEntity, message);
                    }

                    //Functions on SCR Status change
                    if (preImageScrStatus != postImageScrStatus)
                    {
                        //Trigger function on 'PostImageScrStatus == Cancelled'
                        if (postImageScrStatus == 100000003)
                        {
                            shippingClaimHandler.AdjustCancelledOpenInventoryMovement(preImageEntity);
                            shippingClaimHandler.AdjustCancelledSubmittedInventoryMovement(preImageEntity);
                        }

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