Example #1
0
        //Created By : Raphael Herrera, Created On : 06/23/2016

        /*Purpose: Void Cash receipt. Unapply all connected applied sales document
         * Event/Message:
         *      Post/Update: gsc_void
         * Primary Entity: Sales Document
         */
        public void VoidCashReceipt(Entity cashReceipt)
        {
            _tracingService.Trace("Started VoidCashReceipt Method...");

            var salesDocumentConditionList = new List <ConditionExpression>
            {
                new ConditionExpression("gsc_cashreceiptid", ConditionOperator.Equal, cashReceipt.Id)
            };

            EntityCollection salesDocumentCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_sls_salesdocument", "gsc_cashreceiptid", cashReceipt.Id, _organizationService, null, OrderType.Ascending,
                                                                                               new[] { "gsc_apply", "gsc_cashreceiptid" });

            _tracingService.Trace("Retrieved applied sales document associated. " + salesDocumentCollection.Entities.Count + " records found...");

            SalesDocumentHandler sdHandler = new SalesDocumentHandler(_organizationService, _tracingService);

            foreach (Entity salesDocument in salesDocumentCollection.Entities)
            {
                //Delete connected sales documents
                _tracingService.Trace("Unapplying...");
                _organizationService.Delete(salesDocument.LogicalName, salesDocument.Id);
            }

            _tracingService.Trace("Ending VoidCashReceipt Method...");
        }
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 ExecutePostSalesDocumentUpdate(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;

            try
            {
                bool    preApply         = preImageEntity.GetAttributeValue <bool>("gsc_apply");
                decimal preAppliedAmount = preImageEntity.Contains("gsc_appliedamount") ? preImageEntity.GetAttributeValue <Money>("gsc_appliedamount").Value
                    : 0;

                bool    postApply         = postImageEntity.GetAttributeValue <bool>("gsc_apply");
                decimal postAppliedAmount = postImageEntity.Contains("gsc_appliedamount") ? postImageEntity.GetAttributeValue <Money>("gsc_appliedamount").Value
                    : 0;

                SalesDocumentHandler salesDocumentHandler = new SalesDocumentHandler(service, trace);

                //execute if apply was changed
                if (preApply != postApply)
                {
                    //apply document
                    if (postApply == true)
                    {
                        salesDocumentHandler.ApplyCashReceipt(postImageEntity);
                    }
                    //unapply document
                    else
                    {
                        salesDocumentHandler.UnapplyCashReceipt(postImageEntity);
                        service.Delete(postImageEntity.LogicalName, postImageEntity.Id);
                    }
                }

                //execute if applied amount was changed AND apply set to true
                if (preAppliedAmount != postAppliedAmount && postApply == true)
                {
                    salesDocumentHandler.ApplyCashReceipt(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);
            }
        }