Logo

Welcome:

Welcome to Studer Consulting Group. At Studer Consulting Group we help companies solve problems through software. Whether you want a fully custom built application, implementation of Microsoft Dynamics 365 or an integration built between your current systems. We can handle it. With years of experience in the software development and Microsoft Dynamics 365, we can help your business grow.

Services We Offer:

(We also provide sub-contracting services in these areas.)

Microsoft Dynamics 365


  • Functional Consulting
  • Implementation
  • Plugin Development
  • Customization
  • Extending with Micrsoft Azure
  • CRM Portals
  • SSRS Reporting
  • Client-side Scripting (JavaScript)

System Integration \ Migration


  • API Development
  • Custom Coded
  • Scribe Online
  • KingswaySoft
  • SSIS

Software Development


  • .NET Core
  • ASP.NET Core
  • .NET
  • C#
  • JavaScript
  • Java
  • HTML
  • CSS
  • C\C++
  • SQL
  • XAML
  • Xamarin

Latest Blog Post:

Prevent NULL inserts during updates

12/5/2018 6:36:00 AM

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:

  1. Check's the plugin context to see if we are trying to update the emailaddress1 or emailaddress2.  
  2. 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.
  3. 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.

Contact Us:

(All fields are required.)