/// <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 ExecutePreVehicleSalesReturnCreate(LocalPluginContext localContext) { if (localContext == null) { throw new ArgumentNullException("localContext"); } IPluginExecutionContext context = localContext.PluginExecutionContext; IOrganizationService service = localContext.OrganizationService; ITracingService trace = localContext.TracingService; Entity vehicleSalesReturnEntity = (Entity)context.InputParameters["Target"]; string message = context.MessageName; string error = ""; try { VehicleSalesReturnHandler vehicleSalesReturnHandler = new VehicleSalesReturnHandler(service, trace); if (vehicleSalesReturnHandler.IsValidInvoice(vehicleSalesReturnEntity) == false) { throw new InvalidPluginExecutionException("Sales invoice selected already returned."); } vehicleSalesReturnHandler.ReplicateInvoicedVehicle(vehicleSalesReturnEntity, message); } catch (Exception ex) { if (ex.Message.Contains("Sales invoice selected already returned.")) { throw new InvalidPluginExecutionException(ex.Message); } else { 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 ExecutePostVehicleSalesReturnUpdate(LocalPluginContext localContext) { if (localContext == null) { throw new ArgumentNullException("localContext"); } IPluginExecutionContext context = localContext.PluginExecutionContext; IOrganizationService service = localContext.OrganizationService; ITracingService trace = localContext.TracingService; 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; if (!(context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)) { return; } if (postImageEntity.LogicalName != "gsc_sls_vehiclesalesreturn") { return; } string message = context.MessageName; try { VehicleSalesReturnHandler vehicleSalesReturnHandler = new VehicleSalesReturnHandler(service, trace); var preImageInvoiceId = preImageEntity.Contains("gsc_invoiceid") ? preImageEntity.GetAttributeValue <EntityReference>("gsc_invoiceid").Id : Guid.Empty; var postImageInvoiceId = postImageEntity.Contains("gsc_invoiceid") ? postImageEntity.GetAttributeValue <EntityReference>("gsc_invoiceid").Id : Guid.Empty; var preImagePostTransaction = preImageEntity.Contains("gsc_posttransaction") ? preImageEntity.GetAttributeValue <bool>("gsc_posttransaction") : false; var postImagePostTransaction = postImageEntity.Contains("gsc_posttransaction") ? postImageEntity.GetAttributeValue <bool>("gsc_posttransaction") : false; //execute when Invoice id was changed if (preImageInvoiceId != postImageInvoiceId) { vehicleSalesReturnHandler.ReplicateInvoicedVehicle(postImageEntity, message); } //execute when PostTransaction was changed if (preImagePostTransaction != postImagePostTransaction && postImagePostTransaction != false) { trace.Trace("Post Transaction COndition"); EntityCollection salesReturnDetailCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_salesreturndetail", "gsc_vehiclesalesreturnid", postImageEntity.Id, service, null, OrderType.Ascending, new[] { "gsc_inventoryid", "gsc_branchsiteid" }); trace.Trace("Condition if not null"); if (salesReturnDetailCollection.Entities.Count > 1) { trace.Trace("More than 1 SalesReturnDetail record retrieved... " + salesReturnDetailCollection.Entities.Count); } trace.Trace("Sales ReturnDetail"); SalesReturnDetailHandler salesReturnDetailHandler = new SalesReturnDetailHandler(service, trace); //Update Quantity Cout trace.Trace("Update Quantity Cout"); if (salesReturnDetailCollection.Entities.Count > 0) { salesReturnDetailHandler.PostTransaction(salesReturnDetailCollection.Entities[0]); } //Update VSR Status trace.Trace("Update VSR Status"); vehicleSalesReturnHandler.PostTransaction(postImageEntity); trace.Trace("DeleteTransactedVehicle"); vehicleSalesReturnHandler.DeleteTransactedVehicle(postImageEntity); trace.Trace("CreateReturnedInventoryHistory"); vehicleSalesReturnHandler.CreateReturnedInventoryHistory(postImageEntity); } } catch (Exception ex) { if (ex.Message.Contains("Sales invoice selected already returned.")) { throw new InvalidPluginExecutionException(ex.Message); } else { throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace)); } } }
public void TestMethod1() { #region 1. Setup / Arrange var orgServiceMock = new Mock <IOrganizationService>(); var orgService = orgServiceMock.Object; var orgTracingMock = new Mock <ITracingService>(); var orgTracing = orgTracingMock.Object; #region Color Entity Collection //var ColorCollection = new EntityCollection() //{ // EntityName = "VehicleColor", // Entities = // { // new Entity // { // Id = Guid.NewGuid(), // LogicalName = "vehiclecolor", // Attributes = // { // {"gsc_colorpn", String.Empty} // } // } // } //}; #endregion #region Sales Order Entity Collection var SalesOrderCollection = new EntityCollection() { EntityName = "Order", Entities = { new Entity { Id = Guid.NewGuid(), LogicalName = "order" } } }; #endregion #region VehicleSalesReturn Entity Collection var VehicleSalesReturnCollection = new EntityCollection() { EntityName = "VehicleSalesReturn", Entities = { new Entity { Id = Guid.NewGuid(), LogicalName = "gsc_sls_vehiclesalesreturn", Attributes = { { "gsc_modeldescription", String.Empty }, { "gsc_modelcode", String.Empty }, { "gsc_modelyear", String.Empty }, { "gsc_color", String.Empty }, { "gsc_csno", String.Empty }, { "gsc_engineno", String.Empty }, { "gsc_vin", String.Empty }, { "gsc_productionno", String.Empty }, { "gsc_returnedquantity", 0 }, { "gsc_returnedamount", new Money(0) }, { "gsc_salesorderid", Guid.Empty }, { "gsc_paymentmode", null }, { "gsc_customertype", null }, { "gsc_salesinvoicestatus", String.Empty } } } } }; #endregion #region Invoice Entity Collection var SalesInvoiceCollection = new EntityCollection() { EntityName = "Invoice", Entities = { new Entity { Id = Guid.NewGuid(), LogicalName = "invoice", Attributes = { { "gsc_modeldescription", "description" }, { "gsc_modelcode", "model code" }, { "gsc_modelyear", "2002" }, { "gsc_csno", "1" }, { "gsc_engineno", "2" }, { "gsc_vin", "3" }, { "gsc_productionno", "4" }, { "gsc_unitprice", new Money(900000) }, { "gsc_salesorderid", new EntityReference("order", SalesOrderCollection.Entities[0].Id)}, { "gsc_paymentmode", new OptionSetValue(1) }, { "gsc_customertype", new OptionSetValue(1) }, { "gsc_salesinvoicestatus", "Released" } }, FormattedValues = { { "gsc_paymentmode", "Cash" }, { "gsc_customertype", "Individual" }, } } } }; #endregion #endregion #region 2. Call/Action VehicleSalesReturnHandler handler = new VehicleSalesReturnHandler(orgService, orgTracing); handler.ReplicateInvoicedVehicle(VehicleSalesReturnCollection.Entities[0], "Update"); #endregion #region 3. Verify Assert.AreEqual("description", VehicleSalesReturnCollection.Entities[0].GetAttributeValue <string>("gsc_modeldescription")); Assert.AreEqual("model code", VehicleSalesReturnCollection.Entities[0].GetAttributeValue <string>("gsc_modelcode")); Assert.AreEqual("2002", VehicleSalesReturnCollection.Entities[0].GetAttributeValue <string>("gsc_modelyear")); Assert.AreEqual("1", VehicleSalesReturnCollection.Entities[0].GetAttributeValue <string>("gsc_csno")); Assert.AreEqual("2", VehicleSalesReturnCollection.Entities[0].GetAttributeValue <string>("gsc_engineno")); Assert.AreEqual("3", VehicleSalesReturnCollection.Entities[0].GetAttributeValue <string>("gsc_vin")); Assert.AreEqual("4", VehicleSalesReturnCollection.Entities[0].GetAttributeValue <string>("gsc_productionno")); Assert.AreEqual(1, VehicleSalesReturnCollection.Entities[0].GetAttributeValue <Int32>("gsc_returnedquantity")); Assert.AreEqual(SalesInvoiceCollection.Entities[0].GetAttributeValue <EntityReference>("gsc_salesorderid").Id, VehicleSalesReturnCollection.Entities[0].GetAttributeValue <EntityReference>("gsc_salesorderid").Id); Assert.AreEqual(SalesInvoiceCollection.Entities[0].GetAttributeValue <OptionSetValue>("gsc_paymentmode"), VehicleSalesReturnCollection.Entities[0].GetAttributeValue <OptionSetValue>("gsc_paymentmode")); Assert.AreEqual(SalesInvoiceCollection.Entities[0].GetAttributeValue <OptionSetValue>("gsc_customertype"), VehicleSalesReturnCollection.Entities[0].GetAttributeValue <OptionSetValue>("gsc_customertype")); Assert.AreEqual("Released", VehicleSalesReturnCollection.Entities[0].GetAttributeValue <string>("gsc_salesinvoicestatus")); //Assert.AreEqual(SalesInvoiceCollection.Entities[0].GetAttributeValue<Money>("gsc_unitprice"), (decimal)VehicleSalesReturnCollection.Entities[0].GetAttributeValue<Money>("gsc_returnedamount").Value); #endregion }