The GlideSysAttachment.write() method creates attachments directly in the database without going through the UI attachment processor. Most developers trip up on character encoding β special characters, line breaks, and Unicode get mangled unless you base64 encode the content first. The platform expects binary data, so raw text strings often corrupt during the write operation, especially in non-English environments or when processing user-generated content with unpredictable characters.
When to use this
- When generating reports, logs, or data exports programmatically in server-side scripts
- When creating audit trails or documentation that needs to persist with a record
- When processing data transformations where the intermediate or final output needs to be saved as a file
- When integrating with external systems that require file-based evidence of processed records
When NOT to use this
- Don't use this for large files (>10MB) β use the Import Set API or external storage instead
- Don't use this in Business Rules on high-volume tables β you'll create database bloat and performance issues
- Don't use this for temporary data β attachments persist until manually deleted and consume database space
- Don't use this for binary content without proper encoding β use
GlideSysAttachment.writeBase64()for images or documents
Key behaviors and gotchas
- The method returns the
sys_idof the created attachment record, or empty string on failure β always check for success - File names with special characters or spaces can cause download issues β sanitize with
replace(/[\s:]/g, '_') - Content-Type header affects how browsers handle downloads β use
text/plainfor readable files,application/octet-streamto force download - Domain separation applies β attachments inherit the domain of the target record and may not be visible across domains
- ACLs on the
sys_attachmenttable control attachment visibility β test with different user roles - Base64 encoding increases file size by ~33% β factor this into storage calculations for large implementations
Without base64 encoding, any text containing Unicode characters, line breaks, or special symbols will corrupt during the write operation. You'll get garbled characters when users download the file, and the corruption is irreversible once written.
Enjoying this? Get one deep-dive per week.
Join 1,000+ ServiceNow pros β scripts, GlideRecord patterns, Flow Designer techniques, and career moves. Free.