Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

How can I edit the formatted comments in an email notification via a mail script?

bcronrath
Kilo Guru

I am running into an issue where I can't seem to get the formatted comments to output when I use it a mail script.

Example:

I have a "comment added" type notification set up to go out whenever a comment is added and send to the caller.   Our comments are done using HTML code, so to preserver that, I can just put the following in the mail body:

${comments}


And it will put out the last few most recent comments with all the formatting from service-now preserved.   My problem comes when I try to edit any of this output.   As far as I'm aware this needs to be done through a mail script.   So let's say I create a mail script, and now instead of ${comments} I have the script name ${mail_script:HTML_Comment_Script}   (and of course have to create a script of this name in the mail scripts section).


var suggComm = current.comments;

template.print("\n" + suggComm);


Shouldn't this be the same as ${comments}?   What is different about the mail scripts in how I can access the same field information?   Currently what happens is just nothing comes back.

My end goal is I have to figure out a way to do two things:

a.)   Modify the img src tags to decode the %20 back to space characters because when it goes out in an email, if any file has a space in the name the encoded %20 is treated as literal set of characters and the image won't show in the received email.

b.)   Only grab the latest comment.   I know there is a global parameter to change how many get sent out, but changing that is not an option for me due to other workflows.   If anyone knows how to change this on a per-call basis I'd love to know how, seems like it would be a handy feature to just pass some sort of a parameter in that changes how many "deep" the comments go and disregards the global parameter.   However, either way I should be able to handle it with some regex scripting if i can just get the comment to actually output, which currently I can't do so I'm sort of dead in the water.

Best regards,

Brian Cronrath

1 ACCEPTED SOLUTION

Hello ntylenda,



I did come up with a solution, I'm not sure if it is the greatest but it was the best I could come up with.   I am basically stripping back out the [code] tags, and manually adding in the border format to mimic what Service Now does with the comment output.   Here is my mail script:



var suggComm = current.comments.getJournalEntry(1);



suggComm = suggComm.replace("[code]","");


suggComm = suggComm.replace("[/code]","");


suggComm = "<div><table cellpadding=\"0\" cellspacing=\"0\" style=\"table-layout:fixed\" width=\"100%\"><tbody><tr><td colspan=\"2\"><hr /></td></tr><tr style=\"\"><td class=\"tdwrap\"><strong>" + suggComm;


suggComm = suggComm.replace("(Journaled comments)","</strong>");


suggComm = suggComm + "</span></span></p></td></tr></tbody></table></div>";



template.print("\n" + suggComm);



Hope this helps!


View solution in original post

9 REPLIES 9

bcronrath
Kilo Guru

Also just to clarify - I need to keep the formatting.


I can get the plain text data to display if I use current.comments.getJournalEntry(1) for example, however I need the actual formatting included so this is not really an option for me (unless someone knows how to preserve formatting w/ this method)


we used current.comments.getJournalEntry(-1) to get all comments


Hi Brian,



Did you ever find a solution for this?   I am having the same problem with the formatting.



Thanks


Hello ntylenda,



I did come up with a solution, I'm not sure if it is the greatest but it was the best I could come up with.   I am basically stripping back out the [code] tags, and manually adding in the border format to mimic what Service Now does with the comment output.   Here is my mail script:



var suggComm = current.comments.getJournalEntry(1);



suggComm = suggComm.replace("[code]","");


suggComm = suggComm.replace("[/code]","");


suggComm = "<div><table cellpadding=\"0\" cellspacing=\"0\" style=\"table-layout:fixed\" width=\"100%\"><tbody><tr><td colspan=\"2\"><hr /></td></tr><tr style=\"\"><td class=\"tdwrap\"><strong>" + suggComm;


suggComm = suggComm.replace("(Journaled comments)","</strong>");


suggComm = suggComm + "</span></span></p></td></tr></tbody></table></div>";



template.print("\n" + suggComm);



Hope this helps!


Plonk that into a Mail Script for re-usability!



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022