Change Additional Comments and Work Notes to HTML?

josh_tessaro
Giga Expert

Our management and user base wants to be able to insert inline images into Additional Comments and work notes and have them display correctly in the journal (Activity log). This is something that our previous system did well and which is missing in ServiceNow.

I am looking for a way to do this as seamlessly as possible. I have followed this guide: Implementing HTML in Comments - ServiceNow Wiki, with both Work Notes and additional comments, however I am having some issues.

1. Things entered in the new fields are correctly transferring to the OOB comments and work_notes fields and displaying html formatting with the exception of images. which are not displayed at all.

2. When attaching a suggested KB it ends up in description instead of the correct field (comments or u_comments) as the expected comments field is no longer on the form. I have looked but cannot find the logic that controls where to insert the attached KB text.

Any suggestions on how to solve the above or to better accomplish our goals?

Thanks,

-Josh

1 ACCEPTED SOLUTION

The below was provided mostly by ServiceNow support:




Steps to Resolution:


1) Set the field in question (or create a custom field) to Type: HTML


2) In the dictionary for the field, create an attribute for HTML Sanitize and set the value to false.


3) Create a business rule that auto inserts code tags and sets the Style to display: block


4) [OPTIONAL] I created another onDisplay business rule that wiped out the field so the content of the field did not linger.



Additional details


2. add html_sanatize=false to the attributes of the new html field.


3. add on before business rule to field


function onBefore(current, previous) {


  //If no opening [code] tag found, wrap it.


  var note = current.work_notes + "";


  if (note.indexof("[code]") == -1) {


      var wrappedNote = "[code]" + note + "[/code]";


      note = wrappedNote;


  }



  // Clunky workaround to inject style attribute into image tags.


  var noteStyle = note.replace(/\<img style\=\"/g, "\<img style\=\"display\: block\; ");


  if (note != noteStyle) {


      current.work_notes = noteStyle;


  }


}




4. you will find that HTML fields do not clear field contents after submit so you can create an onDisplay script to clear the filed:


current.field_name = '';



NOTE: At this time this will prevent the Additional Comments block form generating correctly in notifications




Edit: fixed .replace as per Leslie's suggestion.


View solution in original post

35 REPLIES 35

Hello @josh.tessaro ,

I follow your coding to add to my custom application.

the behavior now as below;

1. Once image paste to the html field, attachment is created w/o record save/update

2. if there is any text entered in this field it will not transfer to activities journal filed, is that possible to transfer all content ? 

3. i also see this error, is this ok?

find_real_file.png

is this consider working ? i am not sure whether correct it only save the image not text.

Hope to hear from you.

or if any one have idea , pls share. 

Thank you.

leslie8
Mega Expert

Hello, Yes this still works for me. I have a slightly simpler version of the code. I am using the "Additional Comments" field in this example. I have also tested using a custom field. The attached shows the business rule and dictionary configuration.



Business Rule


function onBefore(current, previous) {


  var note = current.comments + "";  


  var noteWrap = ("[code]" + note + "[/code]" );


  var noteStyle = noteWrap.replace(/\<img style\=\"/g, "\<img style\=\"display\: block\; ");  


  current.comments = noteStyle;


}




Screen Shot 2015-07-17 at 12.07.49 PM.png\\Screen Shot 2015-07-17 at 12.08.16 PM.png



Screen Shot 2015-07-17 at 12.13.32 PM.png


I see that the code you pasted is orange in the same spot that it is for me...so I just ignored the error and did a test. I do see images in our custom Work notes field (u_work_notes).



However, one thing I am noticing is that any text added with an image is pasted twice.



u_work_notes.png



We set up our custom Work notes field (u_work_notes) to display on the incident form. However, we created a business rule to copy the html from the u_work_notes field to the OOB Work notes field (work_notes). I think that business rule is conflicting with this new business rule to insert the images so that each runs and copies the text to the activity log.



If you have any suggestions to help work around this, that would be great!


Thanks,


Lori


Hm, where exactly are you seeing the error messages?



Yes the OOTB work_notes field is the one that is displaying the second "help" at the bottom with no image below. When something is added to the work notes field, it is displayed in the activity log OOTB. Then you are copying it from the custom work notes field so that is why you have the second update. Can you consolidate and use one or the other?


Ah never mind I am seeing the same warning messages when running the validation on the code in the Business Rue code editor.



As Josh mentioned this is kind of a sloppy workaround that ServiceNow support suggested.



This will work for our purposes for now, but I hope that ServiceNow implements an actual fix for attachments, since that area has always been very poor.