/// <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 ExecutePreValidateCashReceiptCreate(LocalPluginContext localContext) { if (localContext == null) { throw new ArgumentNullException("localContext"); } IPluginExecutionContext context = localContext.PluginExecutionContext; IOrganizationService service = localContext.OrganizationService; ITracingService trace = localContext.TracingService; Entity cashReceipt = (Entity)context.InputParameters["Target"]; string message = context.MessageName; string error = ""; try { CashReceiptHandler cashReceiptHandler = new CashReceiptHandler(service, trace); cashReceiptHandler.PopulateCashReceipt(cashReceipt); } catch (Exception ex) { 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 ExecutePostCashReceiptCreate(LocalPluginContext localContext) { if (localContext == null) { throw new ArgumentNullException("localContext"); } IPluginExecutionContext context = localContext.PluginExecutionContext; IOrganizationService service = localContext.OrganizationService; ITracingService trace = localContext.TracingService; Entity CashReceiptEntity = (Entity)context.InputParameters["Target"]; if (CashReceiptEntity.LogicalName != "gsc_sls_cashreceipt") { return; } try { Entity cashReceipt = service.Retrieve(CashReceiptEntity.LogicalName, CashReceiptEntity.Id, new ColumnSet(true)); CashReceiptHandler cashReceiptHandler = new CashReceiptHandler(service, trace); cashReceiptHandler.PopulateSalesDocumentSubGrid(cashReceipt); } catch (Exception ex) { throw new InvalidPluginExecutionException(ex.ToString()); } // TODO: Implement your custom Plug-in business logic. }
/// <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 ExecutePostCashReceiptUpdate(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; Entity cashReceipt = (Entity)context.InputParameters["Target"]; if (!(context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)) { return; } if (cashReceipt.LogicalName != "gsc_sls_cashreceipt") { return; } if (context.Mode == 0) //Synchronous Plug-in { string message = context.MessageName; try { bool preVoid = preImageEntity.Contains("gsc_void") ? preImageEntity.GetAttributeValue <bool>("gsc_void") : false; bool postVoid = postImageEntity.Contains("gsc_void") ? postImageEntity.GetAttributeValue <bool>("gsc_void") : false; CashReceiptHandler cashReceiptHandler = new CashReceiptHandler(service, trace); //calls methods if void was changed if ((preVoid != postVoid) && (postVoid == true)) { cashReceiptHandler.VoidCashReceipt(cashReceipt); } } catch (Exception ex) { //throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace)); throw new InvalidPluginExecutionException(ex.Message); } } }