Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Removing HTML from Description in Case Record

(0) ShareShare
ReportReport
Posted on by

We're leveraging the auto record creation with a rule that creates a Case when an email is sent to the "help desk" queue. The rule is working great, with one exception. Incoming emails are formatted in HTML so all of the body tags and nonbreaking space characters are showing up making the Case Description almost impossible to read. I've added some JavaScript to remove the HTML with some regular expressions, but it leaves behind a ton of white space. I've tried using the native .trim() function to no avail. I've even tried to use the native .replace() function with disappointing results. Has anyone ever ran across this issue and how did you solve it? I'm tearing my hair out here...

Here is the code I used.

var regex = /(&nbsp;|<([^>]+)>)/g
body = Xrm.Page.getAttribute("description").getValue();
result = body.replace(regex, "");
Xrm.Page.getAttribute("description").setValue(result);

*This post is locked for comments

  • John Tertin Profile Picture
    on at
    RE: Removing HTML from Description in Case Record

    I had the same problem as SanchezITM, and after taking a look at this realized that since the code uses "not equal" operators between the undefined/null/empty checks, the IF statement referenced should contain AND operators, not OR operators. Changing the referenced line to this fixed the issue:

    if (caseDescription != undefined && caseDescription != null && caseDescription != "") {

    Also, you can remove the additional empty lines created by this script by adding an additional 'or' component to the regular expression in the replace method to also match line breaks ("\r").  For example:

    var txtDesc = caseDescription.replace(/(&nbsp;|\r|<([^>]+)>)/g, "");

    Of course, this is a double-edged sword as it will replace all of them (including breaks between paragraphs, email signatures, etc, but I found it helpful anyway given the display on the form is a small area and this content is typically pretty short.

    Hope it helps!

  • Community Member Profile Picture
    on at
    RE: Removing HTML from Description in Case Record

    This appears to work, right up until I try to create a new workflow where Case is the required entity to be updated.  Any time I select "Set Properties" in the workflow with Case selected, I get an error stating that the entity does not exist, "Unknown Entity Name" - "The entity name doesn't exist.  Please specify an existing entity name."  I had to fully delete the library that had this script in order to be able to use the workflow again.

    If anyone has any ideas as to why that was happening I would appreciate it

  • Community Member Profile Picture
    on at
    RE: Removing HTML from Description in Case Record

    Make sure this line is in your code since it checks for the empty condition.

           var caseDescription = Xrm.Page.getAttribute("description").getValue();
    
           if (caseDescription != undefined || caseDescription != null || caseDescription != "") 


  • SanchezITM Profile Picture
    on at
    RE: Removing HTML from Description in Case Record

    Hi Brandon,

    If my Description field is empty I got this error.

    TypeError: Cannot read property 'replace' of null in trimHtml

    Do you know how can I fix that?

    Thank you!

  • Community Member Profile Picture
    on at
    RE: Removing HTML from Description in Case Record

    Thank you so much Brandon, I really appreciate it.  

    I have the same spacing issue as described above, but otherwise this is miles better than what it was.  Thank you!

  • SanchezITM Profile Picture
    on at
    RE: Removing HTML from Description in Case Record

    Thank you Brandon, works perfect, only one thing, my text looks like this, blank space before and after the text, there is any way for delete this?

    sin.png

    Thank you !!

  • Community Member Profile Picture
    on at
    RE: Removing HTML from Description in Case Record

    This is what I ended up doing. I commented out both lines that were removing any kind of space.

    function trimHtml() {
    
       if (Xrm.Page.ui.getFormType() != 1) {//Check if the form type is not create form
    
           var caseDescription = Xrm.Page.getAttribute("description").getValue();
    
           if (caseDescription != undefined || caseDescription != null || caseDescription != "") {
    
               var txtDesc = caseDescription.replace(/(&nbsp;|<([^>]+)>)/g, ""); //remove all HTML tags
    
               //txtDesc = txtDesc.replace(/ /g, '');//remove the   tag
    
               //txtDesc = txtDesc.replace(/^\s+|\s+$/g, "");// Remove the extra spaces in the begining and end.
    
               Xrm.Page.getAttribute("description").setValue(""); // Clear the field to avoid duplicate text on next onload
    
               Xrm.Page.getAttribute("description").setValue(txtDesc); // Set the field with final email body
    
               Xrm.Page.data.entity.save();// Save the form after removing the spaces in Description field.
    
           }
    
       }
    
    }


  • Community Member Profile Picture
    on at
    RE: Removing HTML from Description in Case Record

    I'm back looking at this thread because the same code you gave me here has stopped functioning properly again. Even with the line completely removed from the code, it's removing all spaces again. I'm not sure why.

  • Community Member Profile Picture
    on at
    RE: Removing HTML from Description in Case Record

    When you are on the Form Properties page, the call is down in the Event Handlers section. Set the Control to "Form" and the Event to "OnLoad". Then click Add. Set the Library to the one you made before, then set the function to whatever you named your function. In my case it's trimHtml. Then click OK. When the form loads, you will see the HTML at first, but it will soon go away.

    1108.Capture2.PNG1108.Capture2.PNG

  • SanchezITM Profile Picture
    on at
    RE: Removing HTML from Description in Case Record

    Hi Brandon, I have followed your steps but now, I dont know how can I make the call to the function. Can you help me?

    I have created the function in Customization/entities/case/forms/edit the form/form properties/

    Now in events, how can I call the function?

    Thank you !

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 293,278 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,011 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans