Convert String data to HTML

asdf13
Tera Contributor

Hi Folks,

 

I have integration data, in 3rd party has information in a table format, in Servicenow I have field data type HTML, but data is not updating in a table format, updating as a paragraph text.

Source data I have an example: Company Contact Country
/Alfreds /Futterkiste /Maria /Anders /Germany
/Centro comercial Moctezuma /Francisco /Chang Mexico

In the ServiceNow HTML field expecting data:

Company Contact Country
Alfreds Futterkiste Maria Anders Germany
Centro comercial Moctezuma Francisco Chang Mexico

 

-Is there is any solution for this issue, tried the below links but not help

Community link: https://community.servicenow.com/community?id=community_question&sys_id=048da8cbdbc71308a39a0b55ca9619b2

 

Regards,

Ck.

2 REPLIES 2

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi asdf,

If the raw data is as in the question, it would not be possible because it's difficult to know the content of each cell.

For example, "/Alfreds /Futterkiste" is suppose to be a value of 1 cell. With the row being "/Alfreds /Futterkiste /Maria /Anders /Germany", it'll be difficult to know if "Futterkiste" is suppose to be in the first cell or in the second cell because "/" does represent cell separation.

The next row is "/Centro comercial Moctezuma /Francisco /Chang Mexico". In this row, cell separator is "/". Since the cell separation rule is different between rows, it's not possible to convert using a script.

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi again,

If the original raw data is as below, it can be converted to html.

 '/Company/Contact/Country\n/Alfreds Futterkiste /Maria Anders /Germany\n/Centro comercial Moctezuma /Francisco /Chang Mexico'

Script

var raw = '/Company/Contact/Country\n/Alfreds Futterkiste /Maria Anders /Germany\n/Centro comercial Moctezuma /Francisco /Chang Mexico';
var rawList = raw.split('\n');
var htmlList = [];
for (var i=0; i<rawList.length; i++) {
  var row = rawList[i].substring(1);

  if (i == 0) {
    html = row.replaceAll('/', '</th><th style="text-align:left;">');
    htmlList.push('<tr><th style="text-align:left;">' + html + '</th></tr>');
  } else {
    html = row.replaceAll('/', '</td><td>');
    htmlList.push('<tr><td>' + html + '</td></tr>');
  }
}
html = '<table border="1">' + htmlList.join('') + '</table>';
gs.info(html);

Result

<table border="1"><tr><th style="text-align:left;">Company</th><th style="text-align:left;">Contact</th><th style="text-align:left;">Country</th></tr><tr><td>Alfreds Futterkiste </td><td>Maria Anders </td><td>Germany</td></tr><tr><td>Centro comercial Moctezuma </td><td>Francisco </td><td>Chang Mexico</td></tr></table>

find_real_file.png