Shorten string on mail notification if its over x number of characters

NeilH2
Giga Guru

I am redesigning our current emails and its become a requirement that the description field on the email be shortened if it gets over a certain length and ... be put in its place.

My code is below but no matter what i do i cannot get it to print the result. (i'm using 25 characters as an example.)


<mail_script>
var string = current.description;
if(string.length > 25) {
var short = string.substring(0, 24)+"...");
template.print(short);
</mail_script>
1 ACCEPTED SOLUTION

michael_baker
Tera Guru

Neil,

Try the below code, it works in the demo site.



<mail_script>
var strVar = current.description.toString();

if (strVar.length > 25) {
var strVar = strVar.substring(0, 25) + "...";
}

template.print(strVar);
</mail_script>

Hope this helps!


View solution in original post

4 REPLIES 4

michael_baker
Tera Guru

Neil,

Try the below code, it works in the demo site.



<mail_script>
var strVar = current.description.toString();

if (strVar.length > 25) {
var strVar = strVar.substring(0, 25) + "...";
}

template.print(strVar);
</mail_script>

Hope this helps!


Excellent thanks, works like a charm


This is exactly what I needed. Thank you both!


If it's helpful, I added a little bit of html to mine to set it apart from other text in the email script. SNOW's script editor also pointed out the extra 'var' declaration in line 05 of your script; I removed it and it worked fine (it was just a warning at any rate).



Here's the whole email script I'm using for "incident_additional_details":



(function runMailScript(current, template, email, email_action, event) {


  template.print('<p><font size="3" color="#4d4d4d" face="helvetica"><u>');


  template.print(gs.getMessage('Additional Details'));


  template.print('</u></font></p>');


  template.print('<font size="3" color="#4d4d4d" face="helvetica">');


  template.print('<p>' + gs.getMessage('Customer') + ': ${caller_id}<br/>');


  template.print('<p>' + gs.getMessage('Category') + ': ${category}</p>');


  template.print('<p>' + gs.getMessage('Priority') + ': ${priority}</p>');


  template.print('<p>' + gs.getMessage('Description Preview') + ':<br/>');


  template.print('<BLOCKQUOTE><i>');


  var strVar = current.description.toString();


  if (strVar.length > 150) {  


  strVar = strVar.substring(0, 150) + "...";  


  }


  template.print(strVar);


  template.print('</i></BLOCKQUOTE></p></font>');


})(current, template, email, email_action, event);



Result:


find_real_file.png