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

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

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

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

            try
            {
                EntityCollection invoiceRecords = CommonHandler.RetrieveRecordsByOneValue("invoice", "invoiceid", invoiceEntity.Id, service, null, OrderType.Ascending,
                                                                                          new[] { "salesorderid", "name" });

                if (invoiceRecords != null && invoiceRecords.Entities.Count > 0)
                {
                    InvoiceHandler invoiceHandler = new InvoiceHandler(service, trace);

                    Entity invoice = invoiceRecords.Entities[0];
                    invoiceHandler.ReplicateSalesInvoiceId(invoice);
                    invoiceHandler.ReplicateAllocatedVehicle(invoice);
                    invoiceHandler.ReplicateMonthlyAmortizations(invoice);
                    invoiceHandler.ReplicateDiscount(invoice);
                    invoiceHandler.ReplicateCoverageAvailable(invoice);
                    invoiceHandler.ReplicateCharge(invoice);
                    invoiceHandler.ReplicateRequirementChecklist(invoice);
                    invoiceHandler.ReplicateAccessories(invoice);
                    invoiceHandler.ReplicateCabChassis(invoice);
                    invoiceHandler.ChangeSOInvoiceCreatedDate(invoice);
                    invoiceHandler.UpdateSOStatus(invoice);
                }
            }
            catch (Exception ex)
            {
                //throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace, Environment.NewLine, error));
                throw new InvalidPluginExecutionException(ex.Message);
            }
        }