/// <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 ExecutePostReturnTransactionCreate(LocalPluginContext localContext) { if (localContext == null) { throw new ArgumentNullException("localContext"); } IPluginExecutionContext context = localContext.PluginExecutionContext; IOrganizationService service = localContext.OrganizationService; ITracingService trace = localContext.TracingService; Entity returnTransaction = (Entity)context.InputParameters["Target"]; string message = context.MessageName; string error = ""; try { EntityCollection returnTransactionCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_cmn_returntransaction", "gsc_cmn_returntransactionid", returnTransaction.Id, service, null, OrderType.Ascending, new[] { "gsc_receivingtransactionid", "gsc_invoiceno", "gsc_vpono", "gsc_site", "gsc_returnstatus", "gsc_returntransactionpn" }); Entity returnTransactionEntity = returnTransactionCollection.Entities[0]; ReturnTransactionHandler returnTransactionHandler = new ReturnTransactionHandler(service, trace); returnTransactionHandler.PopulateReturnTransactionFields(returnTransactionEntity); returnTransactionHandler.AdjustProductQuantity(returnTransactionEntity, "allocate"); } 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 ExecutePreValidateReturnTransactionDelete(LocalPluginContext localContext) { if (localContext == null) { throw new ArgumentNullException("localContext"); } IPluginExecutionContext context = localContext.PluginExecutionContext; IOrganizationService service = localContext.OrganizationService; ITracingService trace = localContext.TracingService; var returnTransaction = (EntityReference)context.InputParameters["Target"]; try { EntityCollection returnTransactionCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_cmn_returntransaction", "gsc_cmn_returntransactionid", returnTransaction.Id, service, null, OrderType.Ascending, new[] { "gsc_cmn_returntransactionid", "gsc_receivingtransactionid", "gsc_returnstatus" }); if (returnTransactionCollection.Entities.Count > 0) { ReturnTransactionHandler returnTransactionHandler = new ReturnTransactionHandler(service, trace); Entity returnTransactionEntity = returnTransactionCollection.Entities[0]; if (returnTransactionHandler.IsValidToDelete(returnTransactionEntity) == false) { throw new InvalidPluginExecutionException("Unable to delete return transaction record(s). Only record with open status can be deleted."); } returnTransactionHandler.AdjustProductQuantity(returnTransactionEntity, "cancel"); } } catch (Exception ex) { if (ex.Message.Contains("Unable to delete return transaction record(s). Only record with open status can be deleted.")) { throw new InvalidPluginExecutionException(ex.Message); } else { throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace, Environment.NewLine)); } } }
/// <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 ExecutePostReturnTransactionUpdate(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 documentChecklistEntity = (Entity)context.InputParameters["Target"]; if (documentChecklistEntity.LogicalName != "gsc_cmn_returntransaction") { return; } if (context.Mode == 0) //synchronous plugin { try { var preReturnStatus = preImageEntity.Contains("gsc_returnstatus") ? preImageEntity.GetAttributeValue <OptionSetValue>("gsc_returnstatus").Value : 0; var preReceivingTransactionId = preImageEntity.Contains("gsc_receivingtransactionid") ? preImageEntity.GetAttributeValue <EntityReference>("gsc_receivingtransactionid").Id : Guid.Empty; var preVrStatus = preImageEntity.Contains("gsc_vrstatus") ? preImageEntity.GetAttributeValue <OptionSetValue>("gsc_vrstatus").Value : 0; var postReturnStatus = postImageEntity.Contains("gsc_returnstatus") ? postImageEntity.GetAttributeValue <OptionSetValue>("gsc_returnstatus").Value : 0; var postReceivingTransactionId = postImageEntity.Contains("gsc_receivingtransactionid") ? postImageEntity.GetAttributeValue <EntityReference>("gsc_receivingtransactionid").Id : Guid.Empty; var postVrStatus = postImageEntity.Contains("gsc_vrstatus") ? postImageEntity.GetAttributeValue <OptionSetValue>("gsc_vrstatus").Value : 0; ReturnTransactionHandler returnTransactionHandler = new ReturnTransactionHandler(service, trace); //receiving transaction id changed if (preReceivingTransactionId != postReceivingTransactionId) { returnTransactionHandler.PopulateReturnTransactionFields(postImageEntity); returnTransactionHandler.AdjustProductQuantity(postImageEntity, "allocate"); } //return status changed if (preReturnStatus != postReturnStatus) { //status set to returned if (postReturnStatus == 100000001) { returnTransactionHandler.AdjustProductQuantity(postImageEntity, "postTransaction"); } //status set to cancelled else if (postReturnStatus == 100000002) { returnTransactionHandler.AdjustProductQuantity(postImageEntity, "cancel"); } } if (preVrStatus != postVrStatus) { returnTransactionHandler.ReplicateVrStatus(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); } } }