Exceeding String limit and GlideSysAttachment workaround

izaquenogue
Tera Contributor

Hello comunity, the last few days I've been developing a Script Include that gets the info of many records and exports this info as an excel spreadsheet.

I use a string variable called recData, it contains a large xml that builds the spreadsheet. For each column a line is created and added to this string. 

I use GlideSysAttachment.write() to create the spreadsheet. My problem is that the variable recData always exceeds the string limit.

Is there a way to use more than one string and them put them together? or maybe use an array of string and use that as a parameter to the write() function?

I'd like to build a single excel spreadsheet, please enlighten me with your knowledge.

1 ACCEPTED SOLUTION

Roshnee Dash
Tera Guru

Hi @izaquenogue 

You can try with these steps

Use an Array and Join Method:

  • Instead of appending large XML content to a single string, store each row or segment in an array.

  • Use .join('\n') to merge all entries before writing to GlideSysAttachment.

var recDataArray = [];
recDataArray.push('<?xml version="1.0" encoding="UTF-8"?>');
recDataArray.push('<Workbook>');
recDataArray.push('<Row><Cell>Data1</Cell></Row>');
recDataArray.push('</Workbook>');

var finalData = recDataArray.join('\n');
GlideSysAttachment.write(current, "Export.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", finalData);
  •  

Use StringBuilder (gs.getProperty) for Efficient Concatenation:

  • ServiceNow provides gs.getProperty('sys_property') for handling large strings effectively.

  • Use a property to store segments, append them dynamically, and retrieve the final XML.

Write Data in Chunks Using GlideSysAttachment:

  • Instead of writing the entire XML string at once, try writing incrementally in chunks using multiple calls to GlideSysAttachment.write().

  • This helps ServiceNow manage memory efficiently.

Alternative Approach (Using GlideExcelBuilder)

  • If you're exporting structured data, consider using GlideExcelBuilder instead of manually generating XML.

  • It simplifies spreadsheet creation and avoids excessive string concatenation.

Your feedback makes the community stronger! If you found this helpful, marking it as the correct answer helps others.
Stay awesome,
Roshnee Dash

View solution in original post

3 REPLIES 3

Roshnee Dash
Tera Guru

Hi @izaquenogue 

You can try with these steps

Use an Array and Join Method:

  • Instead of appending large XML content to a single string, store each row or segment in an array.

  • Use .join('\n') to merge all entries before writing to GlideSysAttachment.

var recDataArray = [];
recDataArray.push('<?xml version="1.0" encoding="UTF-8"?>');
recDataArray.push('<Workbook>');
recDataArray.push('<Row><Cell>Data1</Cell></Row>');
recDataArray.push('</Workbook>');

var finalData = recDataArray.join('\n');
GlideSysAttachment.write(current, "Export.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", finalData);
  •  

Use StringBuilder (gs.getProperty) for Efficient Concatenation:

  • ServiceNow provides gs.getProperty('sys_property') for handling large strings effectively.

  • Use a property to store segments, append them dynamically, and retrieve the final XML.

Write Data in Chunks Using GlideSysAttachment:

  • Instead of writing the entire XML string at once, try writing incrementally in chunks using multiple calls to GlideSysAttachment.write().

  • This helps ServiceNow manage memory efficiently.

Alternative Approach (Using GlideExcelBuilder)

  • If you're exporting structured data, consider using GlideExcelBuilder instead of manually generating XML.

  • It simplifies spreadsheet creation and avoids excessive string concatenation.

Your feedback makes the community stronger! If you found this helpful, marking it as the correct answer helps others.
Stay awesome,
Roshnee Dash

Hello, Roshnee. Thank you for your answer!

In the end, I decided to use the chunking method with the array and join method. I didn't find anything about GlideExcelBuilder, could you direct me to the docs?

Again, thank you for your answer! 

Hi @izaquenogue 

For GlideExcelBuilder i have only basic knowledge.
If you found my solution helpful or if it resolved your issue, please consider marking it as 'Helpful' or 'Accept Solution' and closing the thread. Thank you!

Your feedback makes the community stronger! If you found this helpful, marking it as the correct answer helps others.
Stay awesome,
Roshnee Dash