- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-10-2022 10:50 PM
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 .
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-10-2022 11:01 PM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-10-2022 11:30 PM
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();
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-10-2022 11:49 PM
To summarize, these are the steps I took.
1. Table has String field named u_field1.
2. Displayed as single line text on the form.
3. Execute the script to unescape HTML characters. The characters are changed when viewed on the form.
4. Change the type to "HTML" and Max length to "65536".
5. Reload the form
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-10-2022 11:01 PM
Hi
Here is your solution : https://community.servicenow.com/community?id=community_question&sys_id=048da8cbdbc71308a39a0b55ca96...
Mark my answer correct & Helpful, if Applicable.
Thanks,
Sandeep

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-10-2022 11:04 PM
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)
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: &
Feel free to mark correct, If I answered your query.
Will be helpful for future visitors looking for similar questions 🙂
Aman Kumar