Render HTML Markup on a Suitelet From an HTML File in File Cabinet

Scenario

A user wants to create their own form/HTML page using a Suitelet. Instead of concatenating a string that represents HTML markup, the user would like to use an HTML file uploaded into their File Cabinet and render it onto a Suitelet.

Solution

This can be accomplished by using the write method of the ServerResponse object, demonstrated in the following Suitelet script:

/**
 * @NApiVersion 2.1
 * @NScriptType Suitelet
 */
define(['N/file'], (file) => {

  const onRequest = (scriptContext) => {
    if (scriptContext.request.method == "GET") {
      //replace id value with HTML file's Internal ID from File Cabinet.
      var newFile = file.load({
        id: 1
      })
      var contents = newFile.getContents()
      scriptContext.response.write(contents)
    }
  }
  return {
    onRequest
  }
});

Leave a comment

Your email address will not be published. Required fields are marked *