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

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

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

            try
            {
                VehiclePurchasOrderHandler purchaseOrderHandler = new VehiclePurchasOrderHandler(service, trace);
                purchaseOrderHandler.PopulatePurchaseOrderFields(purchaseOrderEntity);
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace, Environment.NewLine, error));
            }
        }
Example #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 ExecutePostVehiclePurchaseOrderUpdate(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 vehiclePurchaseOrder = (Entity)context.InputParameters["Target"];

            if (vehiclePurchaseOrder.LogicalName != "gsc_cmn_purchaseorder")
            {
                return;
            }

            if (context.Mode == 0) //synchronous plugin
            {
                try
                {
                    VehiclePurchasOrderHandler vpoHandler = new VehiclePurchasOrderHandler(service, trace);

                    Entity purchaseOrder = service.Retrieve(vehiclePurchaseOrder.LogicalName, vehiclePurchaseOrder.Id, new ColumnSet(true));

                    #region pre images
                    var preDeliveryDate = preImageEntity.Contains("gsc_desireddate") ? preImageEntity.GetAttributeValue <DateTime>("gsc_desireddate")
                        : (DateTime?)null;
                    var preApprovalStatus = preImageEntity.Contains("gsc_approvalstatus") ? preImageEntity.GetAttributeValue <OptionSetValue>("gsc_approvalstatus").Value
                        : 0;
                    var preImagePOStatus = preImageEntity.GetAttributeValue <OptionSetValue>("gsc_vpostatus") != null
                        ? preImageEntity.GetAttributeValue <OptionSetValue>("gsc_vpostatus").Value
                        : 0;

                    #endregion

                    #region post images
                    var postDeliveryDate = postImageEntity.Contains("gsc_desireddate") ? postImageEntity.GetAttributeValue <DateTime>("gsc_desireddate")
                        : (DateTime?)null;
                    var postApprovalStatus = postImageEntity.Contains("gsc_approvalstatus") ? postImageEntity.GetAttributeValue <OptionSetValue>("gsc_approvalstatus").Value
                        : 0;
                    var postImagePOStatus = postImageEntity.GetAttributeValue <OptionSetValue>("gsc_vpostatus") != null
                        ? postImageEntity.GetAttributeValue <OptionSetValue>("gsc_vpostatus").Value
                        : 0;

                    #endregion

                    if (preDeliveryDate != postDeliveryDate)
                    {
                        if (vpoHandler.ValidateDesiredDate(postImageEntity) == false)
                        {
                            throw new InvalidPluginExecutionException("Delivery date cannot be earlier than Purchase Order creation date.");
                        }
                    }

                    //approval status changed
                    if (preApprovalStatus != postApprovalStatus)
                    {
                        if (postApprovalStatus == 100000003)
                        {
                            vpoHandler.GetLevel1ApproverEmails(postImageEntity);
                        }
                    }

                    if (preImagePOStatus != postImagePOStatus)
                    {
                        vpoHandler.AdjustProductQuantity(purchaseOrder);
                        vpoHandler.UpdatePurchaseOrderStatusCopy(postImageEntity);
                    }
                }
                catch (Exception ex)
                {
                    if (ex.Message.Contains("Delivery date cannot be earlier than Purchase Order creation date."))
                    {
                        throw new InvalidPluginExecutionException(ex.Message);
                    }
                    else
                    {
                        throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace));
                    }
                }
            }
        }