/// <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 ExecutePostVehiclePostDeliveryMonitoringCreate(LocalPluginContext localContext) { if (localContext == null) { throw new ArgumentNullException("localContext"); } IPluginExecutionContext context = localContext.PluginExecutionContext; IOrganizationService service = localContext.OrganizationService; ITracingService trace = localContext.TracingService; Entity vehiclePostDeliveryMonitoring = (Entity)context.InputParameters["Target"]; string message = context.MessageName; string error = ""; try { VehiclePostDeliveryMonitoringHandler postDeliveryMontoringHandler = new VehiclePostDeliveryMonitoringHandler(service, trace); postDeliveryMontoringHandler.ReplicateSurveyQuestions(vehiclePostDeliveryMonitoring); } catch (Exception ex) { throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace, Environment.NewLine, error)); } }
public void ValidateSurveyTransactions() { #region 1. Setup / Arrange var orgServiceMock = new Mock <IOrganizationService>(); var orgService = orgServiceMock.Object; var orgTracingMock = new Mock <ITracingService>(); var orgTracing = orgTracingMock.Object; #region Vehicle Post-Delivery Monitoring EntityCollection var VehiclePostDeliveryMonitoring = new EntityCollection { EntityName = "gsc_sls_vehiclepostdeliverymonitoring", Entities = { new Entity { Id = Guid.NewGuid(), LogicalName = "gsc_sls_vehiclepostdeliverymonitoring", EntityState = EntityState.Created, Attributes = new AttributeCollection { { "gsc_surveytransactionsid", new EntityReference("gsc_cmn_surveytransactions", Guid.NewGuid()) }, { "gsc_completed", true } } } } }; #endregion #region Survey Transactions EntityCollections var SurveyTransactions = new EntityCollection { EntityName = "gsc_cmn_surveytransactions", Entities = { new Entity { Id = VehiclePostDeliveryMonitoring.Entities[0].Id, LogicalName = "gsc_cmn_surveytransactions", EntityState = EntityState.Created, Attributes = new AttributeCollection { //{"gsc_yesornoanswer", new OptionSetValue()}, //{"gsc_textanswer", String.Empty} } } } }; #endregion orgServiceMock.Setup((service => service.RetrieveMultiple( It.Is <QueryExpression>(expression => expression.EntityName == VehiclePostDeliveryMonitoring.EntityName) ))).Returns(VehiclePostDeliveryMonitoring); orgServiceMock.Setup((service => service.RetrieveMultiple( It.Is <QueryExpression>(expression => expression.EntityName == SurveyTransactions.EntityName) ))).Returns(SurveyTransactions); #endregion #region 2. Call / Action var VehiclePostDeliveryMonitoringHandler = new VehiclePostDeliveryMonitoringHandler(orgService, orgTracing); //Entity vehiclePostDeliveryMonitoring = VehiclePostDeliveryMonitoringHandler.ValidateSurveyQuestions(VehiclePostDeliveryMonitoring.Entities[0]); #endregion #region 3. Verify //Assert.AreEqual(); #endregion }
/// <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 ExecutePostVehiclePostDeliveryMonitoringUpdate(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; Entity vehiclePostDeliveryMonitoring = (Entity)context.InputParameters["Target"]; if (!(context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)) { return; } if (vehiclePostDeliveryMonitoring.LogicalName != "gsc_sls_vehiclepostdeliverymonitoring") { return; } if (context.Mode == 0) //Synchronous Plug-in { string message = context.MessageName; try { #region Pre-images var preImageSurveySetup = preImageEntity.GetAttributeValue <EntityReference>("gsc_surveysetupid") != null ? preImageEntity.GetAttributeValue <EntityReference>("gsc_surveysetupid").Id : Guid.Empty; var preImageCompleted = preImageEntity.GetAttributeValue <Boolean>("gsc_completed"); #endregion #region Post-images var postImageSurveySetup = postImageEntity.GetAttributeValue <EntityReference>("gsc_surveysetupid") != null ? postImageEntity.GetAttributeValue <EntityReference>("gsc_surveysetupid").Id : Guid.Empty; var postImageCompleted = postImageEntity.GetAttributeValue <Boolean>("gsc_completed"); #endregion VehiclePostDeliveryMonitoringHandler vehiclePostDeliveryMonitoringHandler = new VehiclePostDeliveryMonitoringHandler(service, trace); if (preImageSurveySetup != postImageSurveySetup) { vehiclePostDeliveryMonitoringHandler.DeleteExistingSurveyQuestions(postImageEntity); } if (preImageCompleted != postImageCompleted) { vehiclePostDeliveryMonitoringHandler.ValidateSurveyQuestions(postImageEntity); vehiclePostDeliveryMonitoringHandler.UpdateStatusAndCallDate(postImageEntity); } } catch (Exception ex) { if (ex.Message.Contains("Survey has no answer.")) { throw new InvalidPluginExecutionException("Please complete the survey provided."); } else { throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace)); } } } }