web
You’re offline. This is a read only version of the page.
close
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

I have the same question (0)
  • Verified answer
    Saroj Das Profile Picture
    3,355 on at

    add this method on Case form on load

    function trimHtml() {

       if (Xrm.Page.ui.getFormType() != 1) {//Check if the form type is not create form

           var caseDescription = Xrm.Page.getAttribute("new_description").getValue();

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

               var txtDesc = caseDescription.replace(/<[^>]*>?/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("new_description").setValue(""); // Clear the field to avoid duplicate text on next onload

               Xrm.Page.getAttribute("new_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.

               //alert(txtDesc);    

           }

       }

    }

    Thanks,

    Saroj

  • Community Member Profile Picture
    on at

    This works, but a little too well. Here is the output. It's removing all the spaces.

    Thisisatestmessage
    &nbsp;
    Thanks,
    BrandonHerring
    NetworkAdministrator
    ArmadaAnalytics,Inc.
    55BeattiePlace,15thFloor,Suite1510
    Greenville,SC29601
    864.751.9080(Direct)
    864.630.5429(Mobile)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    
    www.armadaanalytics.com&nbsp;
    &nbsp;


  • Saroj Das Profile Picture
    3,355 on at

    can you comment out the space removing code from the script and try. Can you please paste your complete code here?

    Thanks

    Saroj

  • Community Member Profile Picture
    on at

    I did as you suggested and it seems to be working fine now. Here in the code I used.

    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

    Hi Brandon,

    I know this is an old thread, but I'm having the same issue and wanted to utilize the code you all have perfected.  

    I see that it is suggested to add the code to the form on load.  This is the first time I would need to do something like this in CRM, so I was wondering could you provide a more detailed step-by-step on where exactly to add the JavaScript?  My assumption is under the form properties in the case form, in the event handler.  However when I go to add, CRM prompts that I must have a library first.  I'm not sure which library to choose.  Any advice would be very welcome.

    Thank you,

    Amanda

  • Community Member Profile Picture
    on at

    I can try to explain. When the CRM asks for a library, you create your own. You are correct in where to add the code in Form Properties. When you click on add, it will bring up a search box. Instead of searching, just click "New". When the next dialog opens, name your library and set Type to Script (JScript). Then click Text Editor and paste this code in there. After you save it, it will show up as a library. Now you can make the call to the function below.

    32806.Untitled1.png32806.Untitled1.png4403.Untitled2.png

  • SanchezITM Profile Picture
    on at

    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 !

  • Community Member Profile Picture
    on at

    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

  • Community Member Profile Picture
    on at

    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

    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.
    
           }
    
       }
    
    }


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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans