Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Convert email html body from multipart/alternative to UTF-8

tpeleg
Tera Expert

Hello experts,

 

I'm looking for a way to convert email html body to utf-8. (as sender cannot parse it before sending to SN side).

 

tpeleg_0-1699204691156.png

 

I tried to adjust  this piece html code which do working on the web:

function strip(html)
{
var tmp = document.createElement("DIV");
tmp.innerHTML = html;
return tmp.textContent||tmp.innerText;
}

alert(strip(document.body.innerHTML));

 

Inbound action code:

 

var finalb = strip(email.body_text);
    current.description = finalb;
   
    function strip(html) {
        gs.log("Tomer - html is " +html,"Tomer");
        var tmp;
        tmp.innerHTML = html;
        gs.log("Tomer - inner text is " + tmp.innerText,"Tomer");
        return tmp.textContent || tmp.innerText;
    }
    current.insert();
 
html is returning full html body but innerText is undefined.
 
any suggestion?
 
Thanks,

 

Tomer
5 REPLIES 5

Anand Kumar P
Giga Patron

Hi @tpeleg,

Try below script 

var finalb = strip(email.body_text);
current.description = finalb;

function strip(html) {
var tmp = document.createElement("DIV"); 
tmp.innerHTML = html;
return tmp.textContent || tmp.innerText;
}
current.insert();

 

Thanks,

Anand

Sohithanjan G
Kilo Sage

Hi @tpeleg 

  1. In the strip function, create a new DIV element, tmp, and set its innerHTML to the HTML content you want to process.

  2. Use tmp.innerText first to get the text content, and if it's undefined, fall back to tmp.textContent. This should cover a wider range of cases, as different browsers may behave differently.

  3. In your inbound action code, pass the HTML content of the email body email.body_html to the strip function, and then assign the resulting text to current.description.

 

function strip(html) {
    var tmp = document.createElement("DIV");
    tmp.innerHTML = html;
    return tmp.innerText || tmp.textContent;
}

var finalb = strip(email.body_html);
current.description = finalb;
current.insert();

 

Mark as accepted solution & HIT helpful if it suffices your requirement.


BR, Sohith

Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)

tpeleg
Tera Expert

@Anand Kumar P & @Sohithanjan G  Thank you!

But unfortantely the code is broken and not working as it should. seems like line:

 var tmp = document.createElement("DIV"); 

 

var finalb = strip(email.body_html);
gs.log("Tomer - finalb is: " + finalb,"Tomer");
current.description = finalb;
current.insert();

function strip(html) {
    gs.log("Tomer - html before tmp " + html,"Tomer");
    var tmp = document.createElement("DIV");
    gs.log("Tomer - html after tmp " + html,"Tomer");
    tmp.innerHTML = html;
    return tmp.innerText || tmp.textContent;
}

is not supported... (can see only before log).

 

any other suggestion?

 

Thanks again

 

Hi @tpeleg ,

Try below script like converting html to string and removing special characters

var htmlBody = email.body_text.toString(); 
var plainText = htmlBody.replace(/<[^>]+>/g, '');
var finalb = strip(plainText);
current.description = finalb;

function strip(html) {
var tmp = document.createElement("DIV"); 
tmp.innerHTML = html;
return tmp.textContent || tmp.innerText;
}
current.insert();

Thanks,

Anand