/// <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 ExecutePreValidateDocumentChecklistDelete(LocalPluginContext localContext) { if (localContext == null) { throw new ArgumentNullException("localContext"); } IPluginExecutionContext context = localContext.PluginExecutionContext; IOrganizationService service = localContext.OrganizationService; ITracingService trace = localContext.TracingService; var documentChecklistEntity = (EntityReference)context.InputParameters["Target"]; string message = context.MessageName; try { EntityCollection vehicleSalesReturnCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_sls_documentchecklist", "gsc_sls_documentchecklistid", documentChecklistEntity.Id, service, null, OrderType.Ascending, new[] { "gsc_documentchecklistpn" }); DocumentChecklistHandler documentChecklistHandler = new DocumentChecklistHandler(service, trace); if (documentChecklistHandler.ValidateDelete(vehicleSalesReturnCollection.Entities[0])) { throw new InvalidPluginExecutionException("You cannot delete a pre-defined document in this list."); } } catch (Exception ex) { throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace)); } }
/// <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 ExecutePostDocumentChecklistUpdate(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_sls_documentchecklist") { return; } if (context.Mode == 0) //synchronous plugin { try { string message = context.MessageName; DocumentChecklistHandler documentChecklistHandler = new DocumentChecklistHandler(service, trace); var preImageDocumentChecklistId = preImageEntity.Contains("gsc_documentid") ? preImageEntity.GetAttributeValue <EntityReference>("gsc_documentid").Id : Guid.Empty; var postImageDocumentChecklistId = postImageEntity.Contains("gsc_documentid") ? postImageEntity.GetAttributeValue <EntityReference>("gsc_documentid").Id : Guid.Empty; if (preImageDocumentChecklistId != postImageDocumentChecklistId) { documentChecklistHandler.ReplicateDocumentInfo(postImageEntity, message); } } 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 ExecutePreDocumentChecklistCreate(LocalPluginContext localContext) { if (localContext == null) { throw new ArgumentNullException("localContext"); } IPluginExecutionContext context = localContext.PluginExecutionContext; IOrganizationService service = localContext.OrganizationService; ITracingService trace = localContext.TracingService; Entity documentchecklist = (Entity)context.InputParameters["Target"]; string message = context.MessageName; string error = ""; try { DocumentChecklistHandler handler = new DocumentChecklistHandler(service, trace); handler.ReplicateDocumentInfo(documentchecklist, message); } catch (Exception ex) { throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace, Environment.NewLine, error)); } }
public void ReplicateDocumentInfoonUpdate() { #region 1. Setup / Arrange var orgServiceMock = new Mock <IOrganizationService>(); var orgService = orgServiceMock.Object; var orgTracingMock = new Mock <ITracingService>(); var orgTracing = orgTracingMock.Object; #region Document EntityCollection var Document = new EntityCollection() { EntityName = "gsc_sls_document", Entities = { new Entity() { Id = Guid.NewGuid(), LogicalName = "gsc_sls_document", Attributes = { { "gsc_documentpn", "Sample Document" }, { "gsc_documenttype", true } } }, new Entity() { Id = Guid.NewGuid(), LogicalName = "gsc_sls_document", Attributes = { { "gsc_documentpn", "Financing Document" }, { "gsc_documenttype", true } } } } }; #endregion #region Document Checklist Entity var DocumentChecklist = new Entity() { Id = Guid.NewGuid(), LogicalName = "gsc_sls_documentchecklist", Attributes = { { "gsc_documentid", new EntityReference(Document.EntityName, Document.Entities[0].Id) }, { "gsc_documentchecklistpn", "" }, { "gsc_documenttype", "" } } }; #endregion orgServiceMock.Setup((service => service.RetrieveMultiple( It.Is <QueryExpression>(expression => expression.EntityName == Document.EntityName) ))).Returns(Document); orgServiceMock.Setup(service => service.Retrieve( It.IsAny <string>(), It.IsAny <Guid>(), It.IsAny <ColumnSet>())).Returns(DocumentChecklist); orgServiceMock.Setup((service => service.Update(It.Is <Entity>(entity => entity.LogicalName == DocumentChecklist.LogicalName)))).Callback <Entity>(s => DocumentChecklist = s); #endregion #region 2. Call / Action var DocumentChecklistHandler = new DocumentChecklistHandler(orgService, orgTracing); Entity UpdatedDocumentChecklist = DocumentChecklistHandler.ReplicateDocumentInfo(DocumentChecklist, "Update"); #endregion #region 3. Verfiy Assert.AreEqual(DocumentChecklist["gsc_documentchecklistpn"], UpdatedDocumentChecklist["gsc_documentchecklistpn"]); Assert.AreEqual(DocumentChecklist.GetAttributeValue <Boolean>("gsc_documenttype"), UpdatedDocumentChecklist.GetAttributeValue <Boolean>("gsc_documenttype")); #endregion }