/// <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 ExecutePostAccountCreate(LocalPluginContext localContext) { if (localContext == null) { throw new ArgumentNullException("localContext"); } IPluginExecutionContext context = localContext.PluginExecutionContext; IOrganizationService service = localContext.OrganizationService; ITracingService trace = localContext.TracingService; Entity accountEntity = (Entity)context.InputParameters["Target"]; string message = context.MessageName; string error = ""; try { AccountHandler accountHandler = new AccountHandler(service, trace); accountHandler.SetCity(accountEntity); accountHandler.ValidatePrimaryContact(accountEntity); accountHandler.PopulateDealerCode(accountEntity); accountHandler.PopulateTaxRate(accountEntity); accountHandler.PopulateWithholdingTaxRate(accountEntity, "gsc_partswithholdingtaxid", "gsc_partswithholdingtaxrate"); accountHandler.PopulateWithholdingTaxRate(accountEntity, "gsc_vehiclewithholdingtaxid", "gsc_vehiclewithholdingtaxrate"); accountHandler.ReplicateAddresstoBillingAddress(accountEntity); accountHandler.PopulatePrimaryContactDetails(accountEntity); } 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 ExecutePostAccountUpdate(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 accountEntity = (Entity)context.InputParameters["Target"]; if (accountEntity.LogicalName != "account") { return; } if (context.Mode == 0) //Synchronous Plugin { try { AccountHandler accountHandler = new AccountHandler(service, trace); #region pre-Image String preImageStreet = preImageEntity.Contains("address1_line1") ? preImageEntity.GetAttributeValue <String>("address1_line1") : String.Empty; Guid preImageProvinceId = preImageEntity.GetAttributeValue <EntityReference>("gsc_provinceid") != null ? preImageEntity.GetAttributeValue <EntityReference>("gsc_provinceid").Id : Guid.Empty; Guid preImageCountryId = preImageEntity.GetAttributeValue <EntityReference>("gsc_countryid") != null ? preImageEntity.GetAttributeValue <EntityReference>("gsc_countryid").Id : Guid.Empty; String preImageZipCode = preImageEntity.Contains("address1_postalcode") ? preImageEntity.GetAttributeValue <String>("address1_postalcode") : String.Empty; String preImageTelephone = preImageEntity.Contains("telephone1") ? preImageEntity.GetAttributeValue <String>("telephone1") : String.Empty; String preImageTin = preImageEntity.Contains("gsc_tin") ? preImageEntity.GetAttributeValue <String>("gsc_tin") : String.Empty; var preImageMobile = preImageEntity.Contains("mobilephone") ? preImageEntity.GetAttributeValue <String>("mobilephone") : String.Empty; var preImageDealer = preImageEntity.GetAttributeValue <EntityReference>("parentaccountid") != null ? preImageEntity.GetAttributeValue <EntityReference>("parentaccountid").Id : Guid.Empty; var preImageProvince = preImageEntity.GetAttributeValue <EntityReference>("gsc_provinceid") != null ? preImageEntity.GetAttributeValue <EntityReference>("gsc_provinceid").Id : Guid.Empty; var preImageTaxId = preImageEntity.GetAttributeValue <EntityReference>("gsc_taxid") != null ? preImageEntity.GetAttributeValue <EntityReference>("gsc_taxid").Id : Guid.Empty; var preImagePartsTaxId = preImageEntity.GetAttributeValue <EntityReference>("gsc_partswithholdingtaxid") != null ? preImageEntity.GetAttributeValue <EntityReference>("gsc_partswithholdingtaxid").Id : Guid.Empty; var preImageVehicleTaxId = preImageEntity.GetAttributeValue <EntityReference>("gsc_vehiclewithholdingtaxid") != null ? preImageEntity.GetAttributeValue <EntityReference>("gsc_vehiclewithholdingtaxid").Id : Guid.Empty; var preImagePrimaryContactId = preImageEntity.GetAttributeValue <EntityReference>("primarycontactid") != null ? preImageEntity.GetAttributeValue <EntityReference>("primarycontactid").Id : Guid.Empty; var preImageAccountName = preImageEntity.Contains("name") ? preImageEntity.GetAttributeValue <String>("name") : String.Empty; var preImageIsPotential = preImageEntity.GetAttributeValue <Boolean>("gsc_ispotential"); var preImageCityName = preImageEntity.Contains("gsc_cityname") ? preImageEntity.GetAttributeValue <string>("gsc_cityname") : string.Empty; var preImageCityIdName = preImageEntity.Contains("gsc_cityid") ? preImageEntity.GetAttributeValue <EntityReference>("gsc_cityid").Name : String.Empty; #endregion #region post-Image String postImageStreet = postImageEntity.Contains("address1_line1") ? postImageEntity.GetAttributeValue <String>("address1_line1") : String.Empty; Guid postImageProvinceId = postImageEntity.GetAttributeValue <EntityReference>("gsc_provinceid") != null ? postImageEntity.GetAttributeValue <EntityReference>("gsc_provinceid").Id : Guid.Empty; Guid postImageCountryId = postImageEntity.GetAttributeValue <EntityReference>("gsc_countryid") != null ? postImageEntity.GetAttributeValue <EntityReference>("gsc_countryid").Id : Guid.Empty; String postImageZipCode = postImageEntity.Contains("address1_postalcode") ? postImageEntity.GetAttributeValue <String>("address1_postalcode") : String.Empty; String postImageTelephone = postImageEntity.Contains("telephone1") ? postImageEntity.GetAttributeValue <String>("telephone1") : String.Empty; String postImageTin = postImageEntity.Contains("gsc_tin") ? postImageEntity.GetAttributeValue <String>("gsc_tin") : String.Empty; var postImageMobile = postImageEntity.Contains("mobilephone") ? postImageEntity.GetAttributeValue <String>("mobilephone") : String.Empty; var postImageDealer = postImageEntity.GetAttributeValue <EntityReference>("parentaccountid") != null ? postImageEntity.GetAttributeValue <EntityReference>("parentaccountid").Id : Guid.Empty; var postImageProvince = postImageEntity.GetAttributeValue <EntityReference>("gsc_provinceid") != null ? postImageEntity.GetAttributeValue <EntityReference>("gsc_provinceid").Id : Guid.Empty; var postImageTaxId = postImageEntity.GetAttributeValue <EntityReference>("gsc_taxid") != null ? postImageEntity.GetAttributeValue <EntityReference>("gsc_taxid").Id : Guid.Empty; var postImagePartsTaxId = postImageEntity.GetAttributeValue <EntityReference>("gsc_partswithholdingtaxid") != null ? postImageEntity.GetAttributeValue <EntityReference>("gsc_partswithholdingtaxid").Id : Guid.Empty; var postImageVehicleTaxId = postImageEntity.GetAttributeValue <EntityReference>("gsc_vehiclewithholdingtaxid") != null ? postImageEntity.GetAttributeValue <EntityReference>("gsc_vehiclewithholdingtaxid").Id : Guid.Empty;; var postImagePrimaryContactId = postImageEntity.GetAttributeValue <EntityReference>("primarycontactid") != null ? postImageEntity.GetAttributeValue <EntityReference>("primarycontactid").Id : Guid.Empty; var postImageAccountName = postImageEntity.Contains("name") ? postImageEntity.GetAttributeValue <String>("name") : String.Empty; var postImageIsPotential = postImageEntity.GetAttributeValue <Boolean>("gsc_ispotential"); var postImageCityName = postImageEntity.Contains("gsc_cityname") ? postImageEntity.GetAttributeValue <string>("gsc_cityname") : String.Empty; var postImageCityIdName = postImageEntity.Contains("gsc_cityid") ? postImageEntity.GetAttributeValue <EntityReference>("gsc_cityid").Name : String.Empty; #endregion if (preImageStreet != postImageStreet || preImageCityIdName != postImageCityIdName || preImageProvinceId != postImageProvinceId || preImageCountryId != postImageCountryId || preImageZipCode != postImageZipCode || preImageMobile != postImageMobile || preImageTelephone != postImageTelephone || preImageTin != postImageTin) { accountHandler.UpdateCustomerInfo(postImageEntity); } if (preImageAccountName != postImageAccountName) { accountHandler.CheckForExistingRecords(postImageEntity); } if (preImageDealer != postImageDealer) { accountHandler.PopulateDealerCode(accountEntity); } if (preImageProvince != postImageProvince) { accountHandler.PopulateRegion(accountEntity); } if (preImageTaxId != postImageTaxId) { accountHandler.PopulateTaxRate(accountEntity); } if (preImagePartsTaxId != postImagePartsTaxId) { accountHandler.PopulateWithholdingTaxRate(accountEntity, "gsc_partswithholdingtaxid", "gsc_partswithholdingtaxrate"); } if (preImageVehicleTaxId != postImageVehicleTaxId) { accountHandler.PopulateWithholdingTaxRate(accountEntity, "gsc_vehiclewithholdingtaxid", "gsc_vehiclewithholdingtaxrate"); } if (preImagePrimaryContactId != postImagePrimaryContactId) { accountHandler.PopulatePrimaryContactDetails(postImageEntity); } if (preImageIsPotential != postImageIsPotential && postImageIsPotential == false) { accountHandler.CreateContactPerson(postImageEntity); } if (postImageCityName != postImageCityIdName && postImageCityName != String.Empty && preImageCityIdName == postImageCityIdName) { accountHandler.SetCity(postImageEntity); } } catch (Exception ex) { if (ex.Message.Contains("This record has been identified as a fraud account. Please ask the customer to provide further information.")) { throw new InvalidPluginExecutionException("This record has been identified as a fraud account. Please ask the customer to provide further information."); } else if (ex.Message.Contains("Account name already exists.")) { throw new InvalidPluginExecutionException("Account name already exists."); } else { throw new InvalidPluginExecutionException(ex.Message); } } } }