String type field not getting converted to HTML

Tapish Sharma1
Kilo Sage

Hi,

I wanted to change the field type of few fields from string to HTML. I changed it from the dictionary but the fields still are string. How to convert them to HTML ? Changing type is not working .

1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Tapish,

The recommended approach is to (1) create a new field of type HTML, (2) copy the content of String field to HTML field, (3)delete the String field.

View solution in original post

8 REPLIES 8

I've executed the following script to escape HTML characters before changing the type to HTML.

var gr = new GlideRecord('<name of table>');
gr.query();
while (gr.next()) {
  var str =GlideStringUtil.escapeHTML(gr.getValue('<name of String field to convert>'));
  gr.setValue('<name of String field to convert>', str);
  gr.update();
}

To summarize, these are the steps I took.

1. Table has String field named u_field1.

find_real_file.png

2. Displayed as single line text on the form.

find_real_file.png

3. Execute the script to unescape HTML characters. The characters are changed when viewed on the form.

find_real_file.png

4. Change the type to "HTML" and Max length to "65536".

find_real_file.png

5. Reload the form

find_real_file.png

Community Alums
Not applicable

Hi @Tapish Sharma ,

Here is your solution : https://community.servicenow.com/community?id=community_question&sys_id=048da8cbdbc71308a39a0b55ca96...

Mark my answer correct & Helpful, if Applicable.

Thanks,

Sandeep

Aman Kumar S
Kilo Patron

Hey,

I think you should not change the type of the field once you have created it, preferred approach would be:

1. Create a new HTML type field

2. Deactivate the string field

3. Write a fix script or background script to copy values from string field to new html field and fetch values using:

getHTMLValue(String sourceString)

Replaces illegal HTML characters into HTML notation.

Using this method removes illegal characters that might cause the UI to render improperly, or trigger a client side attack such as JavaScript or HTML injection.

htmlvalue=GlideStringUtil.getHTMLValue(mydata); gs.info(htmlvalue);

 

Ex:

mydata='&';

htmlvalue=GlideStringUtil.getHTMLValue(mydata);

gs.info(htmlvalue);

 

Output: &amp;

 

Feel free to mark correct, If I answered your query.

Will be helpful for future visitors looking for similar questions 🙂

 

 

 

 

Best Regards
Aman Kumar