RE: Export an Excel on Client Script in SS2.x

I’ve created a button on the User Server Event labelled ‘Export’ to enable a function upon clicking so that I may export the items in a Transfer Order as an Excel file.

Export an Excel on Client Script in SS2.x

I would like a download popup to prompt me to save the file.

I successfully created the button, but now on the client script side I am merely testing just the creation of the XLS file and trying to get the popup initiated for me to download but I keep on running into errors: latest ‘Uncaught ReferenceError: xmlString is not defined’

Server User Event

function(record,serverWidget) {

* Function definition to be triggered before record is loaded.


* @param {Object} scriptContext

* @param {Record} scriptContext.newRecord - New record

* @param {string} scriptContext.type - Trigger type

* @param {Form} scriptContext.form - Current form

* @Since 2015.2


function beforeLoad(scriptContext) {
var forms = scriptContext.form;
if (scriptContext.type == scriptContext.UserEventType.VIEW){


id : 'custpage_buttonid', //always prefix with 'custpage_'

label : 'Export', //label of the button

functionName: "onButtonClick()"


//forms.clientScriptModulePath = "SuiteScripts/exportToXLS.js";



Client Side Script

function(dialog) {
function pageInit(scriptContext) {}
function onButtonClick(scriptContext) {

title: "Alert",

message: "You click this export button!"
// XML content of the file

var xmlStr = '<?xml version="1.0"?><?mso-application progid="Excel.Sheet"?>';

xmlStr += '<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" ';

xmlStr += 'xmlns:o="urn:schemas-microsoft-com:office:office" ';

xmlStr += 'xmlns:x="urn:schemas-microsoft-com:office:excel" ';

xmlStr += 'xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" ';

xmlStr += 'xmlns:html="">';
xmlStr += '<Worksheet ss:Name="Sheet1">';

xmlStr += '<Table>' +

'<Row>' +

'<Cell><Data ss:Type="String"> First Header </Data></Cell>' +

'<Cell><Data ss:Type="String"> Second Header </Data></Cell>' +

'<Cell><Data ss:Type="String"> Third Header </Data></Cell>' +

'<Cell><Data ss:Type="String"> Fourth Header </Data></Cell>' +

'<Cell><Data ss:Type="String"> Fifth Header </Data></Cell>' +

xmlStr += '</Table></Worksheet></Workbook>';
//encode contents

var base64EncodedString = encode.convert({

string: xmlString,

inputEncoding: encode.Encoding.UTF_8,

outputEncoding: encode.Encoding.BASE_64
//create file

var xlsFile = file.create({

name: 'TEST.xls',

fileType: 'EXCEL',

contents: base64EncodedString

details: "File ID: " + fileid


file : xlsFile



mcfly Rookie Asked on September 24, 2019 in SuiteScript.

Is it work on the client script?

on September 16, 2020.
Add Comment
3 Answers

You have your string variable defined as xmlStr initially but later you reference it as xmlString.

Regardless, I would recommend probably using an Excel JavaScript library instead instead of manually forming strings from scratch.
I’ve used  in the past and liked it

Intermediate Answered on September 24, 2019.

Sorry I forgot to invert my commenting blocks. I fixed it now.

on September 24, 2019.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.