How to Create HTML File from an Incident in ServiceNow
In ServiceNow, you may need to export Incident details into an HTML file for reporting, sharing, or archiving purposes. This tutorial walks you through the process of generating an HTML file directly from an Incident record using a Script Include and a UI Action.
Why Generate HTML from ServiceNow Incident?
- Share a styled, readable version of Incident details
- Archive Incidents outside ServiceNow
- Create printable reports with a clean layout
Step 1: Create a Script Include
var IncidentHTMLGenerator = Class.create();
IncidentHTMLGenerator.prototype = {
initialize: function() {},
generateHTML: function(incidentSysId) {
var inc = new GlideRecord("incident");
if (inc.get(incidentSysId)) {
var html = '<!DOCTYPE html>';
html += '<html lang="en">';
html += '<head><meta charset="UTF-8">';
html += '<title>Incident ' + inc.number + '</title></head>';
html += '<body>';
html += '<h1>Incident Details</h1>';
html += '<p><strong>Number:</strong> ' + inc.number + '</p>';
html += '<p><strong>Short Description:</strong> ' + inc.short_description + '</p>';
html += '<p><strong>Description:</strong> ' + inc.description + '</p>';
html += '<p><strong>Priority:</strong> ' + inc.priority + '</p>';
html += '<p><strong>State:</strong> ' + inc.state.getDisplayValue() + '</p>';
html += '</body></html>';
return html;
}
return null;
},
type: 'IncidentHTMLGenerator'
};
Step 2: Create a UI Action
var htmlContent = new IncidentHTMLGenerator().generateHTML(current.sys_id);
if (htmlContent) {
var sa = new GlideSysAttachment();
var contentStream = new java.io.ByteArrayInputStream(new java.lang.String(htmlContent).getBytes("UTF-8"));
sa.write(current, "Incident_" + current.number + ".html", "text/html", contentStream);
gs.addInfoMessage("HTML file created and attached to this Incident.");
} else {
gs.addErrorMessage("Incident not found or failed to generate HTML.");
}
action.setRedirectURL(current);
Step 3: Test the Functionality
- Open any Incident record in ServiceNow
- Click the Generate HTML button
- Check the Attachments section — your
.html
file will be there - Download and open it in your browser
Conclusion
By combining a Script Include and a UI Action, you can easily generate an HTML version of any Incident in ServiceNow. This is especially useful for reporting, sharing with non-ServiceNow users, and creating clean, printable reports.