Wednesday, August 13, 2008

Stripping HTML Tags From an Email

When converting an email to case or lead you sometimes want to transfer the email body (description field) as well. The problem is that the field usually contains HTML tags which make the data unreadable. here is a simple method you can use to strip the HTML tags.


function OnCrmPageLoad()
{
//Create a factitious DOM element
var stubDescription = document.createElement( "<SPAN style='width:1px;height:1px'>");
//This field contains the email body (HTML)
var description = crmForm.all.description;
//Assign the HTML to the factitious DOM element
stubDescription.innerHTML = description.DataValue;
//Add it to the html document
document.body.appendChild( stubDescription );
//Assign the stripped body back to the description field
description.DataValue = stubDescription.innerText;
//remove the factitious DOM element
document.body.removeChild( stubDescription );
}

//Entry Point
OnCrmPageLoad();



The code can run within the email / lead / case or any other entity for that matter.
The entity which implements this code should have a description field which holds the initial html message.

17 comments:

Srihari said...

Too good post dude. This is the exact problem tht we were facing. Thank you :)

Sanjay Munjal said...

The script has typo in the line 06. The crmFrom should be crmForm.

Adi Katz said...

Thanks Sanjay.

Djack said...

Hi,
I am creating a report that display the email description. how can I remove the HTML Tags from emails and display only the text information.
tks for your help
Dany

Adi Katz said...

This option is only available when you enter each email from the browser.
The best approach, although it requires some programming, is to write a plug-in on the email pre create / update and write the text
to a new field e.g. new_textonlydescription and show that in your reports.

Adi Katz said...

take a look at this post:
http://blogs.msdn.com/ukcrm/archive/2008/07/10/converting-html-e-mail-to-plain-text.aspx

Tonedefdom said...

Where do I put this code? Is it in the onload event for a Case form or can I put it within the workflow somewhere to run just onece, when I create a case from an email?

Dominic

Allan Connolly said...

Hi Adi

Thanks for posting this code up, unfortunately i am not much of a coder so I have tried to put this on the onload form for both email and case, however I just get null in the description of the case when i convert the activity, what AM i doing wrong?

cheers

Allan

Henrik said...

Hi

Can this be used on HTML e-mails when they arrive in Quque?

If yes - how?

Darrin Devine said...

@Tonedefdom - in the OnLoad of any entity. Normally would be used to clean up the HTML from case created with an e-mail description. For in the workflow see @Henrick.

@Allan - It's intended to be in the OnLoad for an existing case record with an HTML description (actually any entity). Converting an activity to a case does not result in the activity description being carried of to the case description so I would expect a null.

@Henrick - For that purpose you would need a CRM custom workflow Activity. The code would be more like this URL http://blogs.msdn.com/ukcrm/archive/2008/07/10/converting-html-e-mail-to-plain-text.aspx

Joeri said...

Hello,

i know this is already a older article,
but i'm quite curious if this code can help me with the same problem, however in a slightly different way.

how can i get around this in a view.
i'm using a view on my accounts to show all activities, with their descriptions, and this is all good except for the mail activities.
the mail activities all show the jubmled HTML code, which is very annoying as my users all need to open the activity to be able to read what it is about.

thx in advance for any hint ;)

InagapM said...

Hi Guys,

Really great article, I would like to know if this would work on CRM 2011?

Thanks for any help you may provide!

Asha05 said...

Can any one explain what for the entry point ie function declaration is used @ end in below code... Should i need to used in the script too while creating web resource in CRM 2011.

HoCheung Li said...

For some reason when I tried this on the Email Entity, it would work on the very first email I open and thereafter I am getting an "Undefined" error when I open the 2nd, 3rd emails etc.

Can anyone tell me why this might be happening?

HoCheung Li said...

Okay i figured out why. For some reason, the code is loading *before* the description field is loaded. Does anyone know how to handle this? Is there a way a detect when the description is loaded?

─░smail Tutumluer said...

Some browsers may generate an error
IE9 is throwing an error:
DOM Exception: INVALID_CHARACTER_ERR (5)

try the following code

var sdesc= document.createElement("span");
sdesc.width = "1px";
sdesc.height = "1px";
sdesc.innerHTML = description;
document.body.appendChild(sdesc);

Newtechsupport.new_problem = sdesc.innerText;
document.body.removeChild(sdesc);

─░smail Tutumluer said...

Another code block to remove Font Definitions from the innerText

description = Xrm.Page.getAttribute("description").getValue();
if (description != null) {

var desc = document.createElement("span");
desc.width = "1px";
desc.height = "1px";
de= $(description);
var str;
for (var i = 0; i < de.length; i++) {
str =de[i].innerText + " ";
}

desc.innerHTML = str;
document.body.appendChild(desc);

Newtechsupport.new_problem = desc.innerText;
document.body.removeChild(desc);
}