/// <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 ExecutePostVehiclePurchaseOrderDetailCreate(LocalPluginContext localContext) { if (localContext == null) { throw new ArgumentNullException("localContext"); } IPluginExecutionContext context = localContext.PluginExecutionContext; IOrganizationService service = localContext.OrganizationService; ITracingService trace = localContext.TracingService; Entity vpoDetails = (Entity)context.InputParameters["Target"]; string message = context.MessageName; string error = ""; try { VehiclePurchaseOrderDetailsHandler vpoDetailsHandler = new VehiclePurchaseOrderDetailsHandler(service, trace); VehiclePurchaseOrderHandler vpoHandler = new VehiclePurchaseOrderHandler(service, trace); EntityCollection vpoDetailsCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_cmn_purchaseorderitemdetails", "gsc_cmn_purchaseorderitemdetailsid", vpoDetails.Id, service, null, OrderType.Ascending, new[] { "gsc_productid", "gsc_dnpamount", "gsc_purchaseorderid", "gsc_branchid" }); if (vpoDetailsCollection != null && vpoDetailsCollection.Entities.Count > 0) { Entity vpoDetailsToUpdate = vpoDetailsCollection.Entities[0]; vpoDetailsHandler.SetDNPAmount(vpoDetailsToUpdate); } } catch (Exception ex) { throw new InvalidPluginExecutionException(ex.Message); //throw new InvalidPluginExecutionException(String.Concat("(Exceptionss)\n", ex.Message, Environment.NewLine, ex.StackTrace, Environment.NewLine, "trace>>>" + 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 ExecutePostPurchaseOrderCreate(LocalPluginContext localContext) { if (localContext == null) { throw new ArgumentNullException("localContext"); } IPluginExecutionContext context = localContext.PluginExecutionContext; IOrganizationService _organizationService = localContext.OrganizationService; ITracingService trace = localContext.TracingService; Entity purchaseOrderEntity = (Entity)context.InputParameters["Target"]; string message = context.MessageName; if (purchaseOrderEntity.LogicalName != "gsc_cmn_purchaseorder") { return; } try { Entity purchaseOrder = _organizationService.Retrieve(purchaseOrderEntity.LogicalName, purchaseOrderEntity.Id, new ColumnSet(true)); VehiclePurchaseOrderHandler purchaseOrderHandler = new VehiclePurchaseOrderHandler(_organizationService, trace); //purchaseOrderHandler.GeneratePurchaseOrderNumber(purchaseOrder); purchaseOrderHandler.PopulateDetails(purchaseOrder); //purchaseOrderHandler.PopulateVendorDetails(purchaseOrder); // purchaseOrderHandler.PopulateShipDetails(purchaseOrder); } catch (Exception ex) { //throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace, Environment.NewLine, error)); throw new InvalidPluginExecutionException(ex.Message); } }
/// <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 ExecutePreValidatePurchaseOrderDelete(LocalPluginContext localContext) { if (localContext == null) { throw new ArgumentNullException("localContext"); } IPluginExecutionContext context = localContext.PluginExecutionContext; IOrganizationService service = localContext.OrganizationService; ITracingService trace = localContext.TracingService; var vehiclePurchaseOrderEntity = (EntityReference)context.InputParameters["Target"]; string message = context.MessageName; string error = ""; if (context.Depth > 1) { return; } try { VehiclePurchaseOrderHandler vpoHandler = new VehiclePurchaseOrderHandler(service, trace); EntityCollection vpoCollection = CommonHandler.RetrieveRecordsByOneValue(vehiclePurchaseOrderEntity.LogicalName, "gsc_cmn_purchaseorderid", vehiclePurchaseOrderEntity.Id, service, null, OrderType.Ascending, new[] { "gsc_cmn_purchaseorderid", "gsc_vpostatus" }); if (vpoCollection.Entities.Count > 0) { if (vpoHandler.ValidateDelete(vpoCollection.Entities[0]) != true) { throw new InvalidPluginExecutionException("Unable to delete VPO record(s)"); } } } catch (Exception ex) { throw new InvalidPluginExecutionException(ex.Message); // 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 ExecutePrePurchaseOrderCreate(LocalPluginContext localContext) { if (localContext == null) { throw new ArgumentNullException("localContext"); } IPluginExecutionContext context = localContext.PluginExecutionContext; IOrganizationService service = localContext.OrganizationService; ITracingService trace = localContext.TracingService; Entity vpoDetails = (Entity)context.InputParameters["Target"]; try { string message = context.MessageName; VehiclePurchaseOrderHandler vpoHandler = new VehiclePurchaseOrderHandler(service, trace); vpoHandler.PopulateShipToInformation(vpoDetails); } catch (Exception ex) { throw new InvalidPluginExecutionException(ex.Message); } }
/// <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 ExecutePostPurchaseOrderUpdate(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 { string message = context.MessageName; VehiclePurchaseOrderHandler vpoHandler = new VehiclePurchaseOrderHandler(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; Int32 preImageApproverLevel = preImageEntity.GetAttributeValue <OptionSetValue>("gsc_approverlevel") != null ? preImageEntity.GetAttributeValue <OptionSetValue>("gsc_approverlevel").Value : 0; Guid preImageVendorId = preImageEntity.Contains("gsc_vendorid") ? preImageEntity.GetAttributeValue <EntityReference>("gsc_vendorid").Id : Guid.Empty; Guid preImageBankId = preImageEntity.Contains("gsc_bankid") ? preImageEntity.GetAttributeValue <EntityReference>("gsc_bankid").Id : Guid.Empty; #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; Int32 postImageApproverLevel = postImageEntity.GetAttributeValue <OptionSetValue>("gsc_approverlevel") != null ? postImageEntity.GetAttributeValue <OptionSetValue>("gsc_approverlevel").Value : 0; Guid postImageVendorId = postImageEntity.Contains("gsc_vendorid") ? postImageEntity.GetAttributeValue <EntityReference>("gsc_vendorid").Id : Guid.Empty; Guid postImageBankId = postImageEntity.Contains("gsc_bankid") ? postImageEntity.GetAttributeValue <EntityReference>("gsc_bankid").Id : Guid.Empty; #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) { if (preImagePOStatus != 100000003) { vpoHandler.CreateProductQuantity(postImageEntity, preImageEntity); vpoHandler.SubtractUnservedVPO(postImageEntity); } vpoHandler.UpdatePurchaseOrderStatusCopy(postImageEntity); } if (preImageVendorId != postImageVendorId || preImageBankId != postImageBankId) { vpoHandler.PopulateDetails(postImageEntity); } if (preImageApproverLevel != postImageApproverLevel) { vpoHandler.CreateApprovalRecord(postImageEntity); } } catch (Exception ex) { throw new InvalidPluginExecutionException(ex.Message); // throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace)); } } }