Recently, I had an issue that I needed to overcome. The problem was that the Dynamics 365 instance I was working on, was integrated with multiple other systems with some long running external processes and possible old data in the UI when a record is open, overriding what the value was set to from the external processes. What was happening is that on create of a contact, we can have a NULL e-mail. This is normal behavior. Our external process would be running and update the NULL e-mail field while the record was open in the UI. Because the UI hadn't been refreshed and the user made other changes, the e-mail would be blanked out (NULL). To make sure that the field once populated (yes, this is a business requirement) could not be cleared, I wrote a small pre-operation plugin that does the following:
Because this is a pre-operation removing the values from the context will prevent the value from being cleared out. Also, this is a stripped down code snippet, so you will need to check it for any typo's or syntax errors.
- Check's the plugin context to see if we are trying to update the emailaddress1 or emailaddress2.
- If we are trying to update either email address. Than we check to see if context value is null, "" or string.empty. We also double check to make sure that the field is in the context, since my first if is an OR statement.
- If there is a value, then we move on. But, if we are trying to blank out the email address, I simply remove the attribute from the context.
Here is a code snippet:
using Microsoft.Xrm.Sdk;
using System;
namespace ContactEmailValidation
{
public class ValidateEmail : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
//Convert context to entity to make it easier to work with.
Entity PreValidationData = (Entity)context.InputParameters["Target"];
//Check if email fields are in the context. If they are then we are trying to update them.
if(PreValidationData.Contains(ContactConstants.PrimaryEmail) || PreValidationData.Contains(ContactConstants.SecondaryEmail))
{
//Get current database contact data
using (OrganizationServiceContext context = new OrganizationServiceContext(service))
{
CurrentDatabaseData = (from c in context.CreateQuery("contact")
where (Guid)c[contactid] == id
select c).SingleOrDefault();
}
tracer.Trace($"Execution context does have either primary email or secondary email in it.");
//If primary email is trying to update to null, remove it from the context so we don't clear out the email.
if (PreValidationData.Contains(emailaddress1) && (PreValidationData[emailaddress1] == null || PreValidationData[emailaddress1].ToString() == "" || PreValidationData[emailaddress1].ToString() == string.Empty))
{
tracer.Trace($"Trying to update primary email to null. Removing from context...");
PreValidationData.Attributes.Remove("emailaddress1");
}
//If secondary email is trying to update to null, remove it from the context so we don't clear out the email.
if (PreValidationData.Contains(emailaddress2) && (PreValidationData[emailaddress2] == null || PreValidationData[emailaddress2].ToString() == "" || PreValidationData[emailaddress2].ToString() == string.Empty))
{
tracer.Trace($"Trying to update secondary email to null. Removing from context...");
PreValidationData.Attributes.Remove("emailaddress2");
}
//Update the plugin context that will be saved to the database.
tracer.Trace($"Setting target to new context values.");
context.InputParameters["Target"] = PreValidationData;
}
}
}
}
Because this is a pre-operation removing the values from the context will prevent the value from being cleared out. Also, this is a stripped down code snippet, so you will need to check it for any typo's or syntax errors.
There is a most cashout limit of 10x your deposit throughout the board, and you’ll should fulfill truthful wagering necessities before requesting a fiat withdrawal. Notably, their 600% crypto match comes with a a lot steeper rollover. You’ll be greeted by 코인카지노 400+ free-to-play slots upon signing up with Ducky Luck. Additionally, virtually none of their machines could be found wherever else!
ReplyDelete