Empty Rich Text Fields Validation using Regular Expression in Dynamics 365

Sending
User Review
0 (0 votes)

Empty Rich Text Fields Validation

In the pursuit of enhancing data management within Dynamics 365 CRM, we recently integrated a ‘Rich Text Editor Control‘ into the Description field of the Appointment entity, and inserting text into the description, we observed an unexpected behavior. Upon exporting the data from Dynamics 365 CRM, we noticed that the Description field stored the text along with HTML tags. Surprisingly, even when we cleared the text from the Description field, the HTML tags persisted in the system. This discrepancy became evident when attempting to fetch records with empty Description fields, as the presence of HTML tags prevented these records from being retrieved. Additionally when we wanted to validate if a field of a record Is blank then this discrepancy resulted in inaccurate validation.

In this blog post, we delve into the intricacies of validating whether a Rich Text field truly contains a value using regular expressions in C#.

Let’s consider a use case scenario where a user inputs the word “test” into this Description field. What exactly happens behind the scenes within the system?

Upon entering “test” into the Description field, the system undergoes a process of data storage and representation. Interestingly, the seemingly straightforward input of “test” is transformed into a structured format within the system. Specifically, it is stored as:

<div class=”ck-content” data-wrapper=”true” dir=”ltr” style=”–ck-image-style-spacing: 1.5em; –ck-inline-image-style-spacing: calc(var(–ck-image-style-spacing) / 2); –ck-color-selector-caption-background: hsl(0, 0%, 97%); –ck-color-selector-caption-text: hsl(0, 0%, 20%); font-family: Segoe UI; font-size: 11pt;”><p style=”margin: 0;”>test</p></div>

Please refer to the below screencap for reference,

Empty Rich Text Fields Validation

In case the user clears the content from the Description field, ostensibly rendering it empty. However, behind the veil of apparent emptiness lies a nuanced reality within the system’s data storage mechanism.

Upon clearing the Description field, the system orchestrates a subtle yet significant transformation. Although visually devoid of content, the field is meticulously stored as:

<div class=”ck-content” data-wrapper=”true” dir=”ltr” style=”–ck-image-style-spacing: 1.5em; –ck-inline-image-style-spacing: calc(var(–ck-image-style-spacing) / 2); –ck-color-selector-caption-background: hsl(0, 0%, 97%); –ck-color-selector-caption-text: hsl(0, 0%, 20%); font-family: Segoe UI; font-size: 11pt;”></div>

Note: We have checked the system value by getting the data from the Advance Find from Dynamics 365 CRM and exporting it into Excel.

Please refer to the below screencap for reference,

Empty Rich Text Fields Validation

If we clear the value from the Description field as shown below in the Screenshot,

Empty Rich Text Fields Validation

It updates the actual value for the “Description” field in the background as shown in the below screenshot,

Empty Rich Text Fields Validation

As we observed in the above scenario, even when users clear the Description field on the form, the system keeps HTML tags instead of fully erasing the content. By holding onto these tags, Dynamics 365 CRM ensures that the field’s formatting remains unchanged.

When verifying whether Descriptions are empty for specific appointments, relying solely on visual inspection may yield misleading results due to the presence of HTML tags. These tags, even when the field appears visually empty, signify that data is still present within the system. Consequently, traditional methods of checking for emptiness may not provide accurate insights.

So, it requires the implementation of more robust validation processes, enhancing the accuracy and reliability of data analysis within the Dynamics 365 CRM environment.

To accurately identify and validate the Description field when it appears empty, we will walk through with below-listed steps:

Step 1: Using Regular Expressions for Validation

To accurately determine if the Description field is truly empty, we can use a regular expression pattern.

The pattern  @”<[^>]+>[^\s]*[^<]+[^>]*<\/[^>]+>”  checks if a string contains HTML tags with non-whitespace content inside. If the field includes tags with content, the pattern returns true, indicating non-emptiness. Otherwise, it returns false, signifying an empty field.

Step 2: Let’s implement this validation logic in C#

 string description = entity.GetAttributeValue("description"); // Regular expression pattern to match HTML tags with non-whitespace content inside string pattern = @"<[^>]+>[^\s]*[^<]+[^>]*<\/[^>]+>"; // Check if the description string contains HTML tags with content bool containsHtmlWithText = System.Text.RegularExpressions.Regex.IsMatch(description, pattern); if (containsHtmlWithText) { //Write code here after getting the HTML Tag with Text } else { //Throw error of empty Description field throw new InvalidPluginExecutionException("Description field is empty please fill the data to proceed"); } 

This code effectively uses a regular expression pattern to identify HTML tags with non-whitespace content inside the Description field. If such content is found, the code proceeds with the appropriate logic to handle cases where HTML tags with text are present. Otherwise, if the Description field is empty or contains only whitespace characters, an InvalidPluginExecutionException is thrown, indicating that the field needs to be filled to proceed.

This approach ensures robust validation of the Description field’s content, accounting for the presence of HTML tags and providing clear error handling for empty or insufficiently filled fields.

We are using “Regex.IsMatch(description, pattern)” which will return true if the description string contains an HTML tag with text in it and it returns false if there is no HTML tag in it.

Step 3: So, When the description field doesn’t contain data but has HTML tags stored in the system we are displaying an error message as shown in the screenshot below,

Empty Rich Text Fields Validation

Conclusion:

Regular expressions provide a robust solution for validating Rich Text fields in Dynamics 365. By employing a regular expression pattern to identify HTML tags with content, we can accurately determine if a Rich Text field is truly empty. This approach ensures accurate data validation and facilitates reliable business logic implementation in Dynamics 365 applications.

Attach2Dynamics

The post Empty Rich Text Fields Validation using Regular Expression in Dynamics 365 first appeared on Microsoft Dynamics 365 CRM Tips and Tricks.