/// <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 ExecutePostInvoiceUpdate(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 invoice = (Entity)context.InputParameters["Target"]; if (!(context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)) { return; } if (invoice.LogicalName != "invoice") { return; } if (context.Mode == 0) //Synchronous Plug-in { string message = context.MessageName; try { #region Pre-images var preImageCancelSO = preImageEntity.Contains("gsc_cancelorder") ? preImageEntity.GetAttributeValue <String>("gsc_cancelorder") : String.Empty; var preImageSalesInvoiceStatus = preImageEntity.Contains("gsc_salesinvoicestatus") ? preImageEntity.GetAttributeValue <OptionSetValue>("gsc_salesinvoicestatus").Value : 0; var preImageSalesInvoiceCopy = preImageEntity.Contains("gsc_invoicestatuscopy") ? preImageEntity.GetAttributeValue <OptionSetValue>("gsc_invoicestatuscopy").Value : 0; var preImageInvoiceId = preImageEntity.Contains("name") ? preImageEntity.GetAttributeValue <String>("name") : String.Empty; var preImageisDeliveryReceiptandGatePass = preImageEntity.GetAttributeValue <Boolean>("gsc_isdeliveryreceiptandgatepass"); #endregion #region Post-images var postImageCancelSO = postImageEntity.Contains("gsc_cancelorder") ? postImageEntity.GetAttributeValue <String>("gsc_cancelorder") : String.Empty; var postImageSalesInvoiceStatus = postImageEntity.Contains("gsc_salesinvoicestatus") ? postImageEntity.GetAttributeValue <OptionSetValue>("gsc_salesinvoicestatus").Value : 0; var postImageSalesInvoiceCopy = postImageEntity.Contains("gsc_invoicestatuscopy") ? postImageEntity.GetAttributeValue <OptionSetValue>("gsc_invoicestatuscopy").Value : 0; var postImageInvoiceId = postImageEntity.Contains("name") ? postImageEntity.GetAttributeValue <String>("name") : String.Empty; var postImageisDeliveryReceiptandGatePass = postImageEntity.GetAttributeValue <Boolean>("gsc_isdeliveryreceiptandgatepass"); var postCancelled = postImageEntity.GetAttributeValue <Boolean>("gsc_cancelled"); #endregion InvoiceHandler invoiceHandler = new InvoiceHandler(service, trace); if (preImageCancelSO != postImageCancelSO) { invoiceHandler.SetOrderCancelledStatus(postImageEntity); } if (preImageSalesInvoiceCopy != postImageSalesInvoiceCopy) { invoiceHandler.UpdateStatus(postImageEntity); } if (preImageSalesInvoiceStatus != postImageSalesInvoiceStatus) { if (postCancelled != true) { invoiceHandler.SetInvoiceCancelledStatus(postImageEntity); } invoiceHandler.DeleteInvoicedVehicle(postImageEntity); invoiceHandler.CreateVehicleClaimAndDiscounts(postImageEntity); invoiceHandler.UpdateInvoicedStatus(postImageEntity); invoiceHandler.UpdatedPrintedStatus(postImageEntity); invoiceHandler.PostInvoice(postImageEntity); invoiceHandler.AdjustProductQuantity(postImageEntity); if (postImageSalesInvoiceStatus == 100000003) //If from DR and GatePass, status = Printed { //if (!invoiceHandler.ValidateSubmitDRandGatePass(postImageEntity)) //{ // throw new InvalidPluginExecutionException("Record cannot be printed because PDI has not yet been completed."); //} } // Create Transacted Vehicle When Invoice Status Is Released if (postImageSalesInvoiceStatus == 100000004) { invoiceHandler.CreateTransactedVehicle(postImageEntity); invoiceHandler.CreateSoldInventoryHistory(postImageEntity); } } if (preImageInvoiceId != postImageInvoiceId) { invoiceHandler.CheckInvoiceIdIfDuplicate(postImageEntity); } if (preImageisDeliveryReceiptandGatePass != postImageisDeliveryReceiptandGatePass) { invoiceHandler.DeliveryReceiptandGatePassIdSequenceGen(postImageEntity); //throw new InvalidPluginExecutionException("TEST"); } } catch (Exception ex) { throw new InvalidPluginExecutionException(ex.Message); // throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace)); } } }