---
sourceDocument: Xanadu API Reference
sourceDocumentLink: https://servicenow-prod.fluidtopics.net/r/xanadu/api-reference

 Release :

    - xanadu

ft:locale :

    - en-US

ft:publication_title :

    - Xanadu API Reference

ft:clusterId :

    - crapiref

bundleId :

    - crapiref

workflow :

    - Creator


---

# GlideSysAttachment - Scoped

# GlideSysAttachment - Scoped {#ariaid-title1}

* Release version: Xanadu
* 
* Updated August 1, 2024
* 
* ![](https://www.servicenow.com/docs/portal-asset/ico-clock) 5 minutes to read

The GlideSysAttachment API provides methods to handle attachments.

Content is returned as a string, not as a byte array when getContent() is
called.

Content is returned as a GlideScriptableInputStream object when getContentStream() is called. The GlideScriptableInputStream contains the actual bytes not converted into a string.

## Scoped GlideSysAttachment - GlideSysAttachment() {#ariaid-title2}

Creates an instance of the GlideSysAttachment class.
{#r_SGSA-GlideSysAttachment__table_iys_ctb_fv__entry__3}

| Name | Type | Description |
|-|-|-|
| None |   |   |
[Table 1. Parameters]

{#r_SGSA-GlideSysAttachment__table_iys_ctb_fv}

## Scoped GlideSysAttachment - copy(String sourceTable, String sourceID, String targetTable, String targetID) {#ariaid-title3}

Copies attachments from the source record to the target record.
{#r_SGSA-copy_S_S_S_S__id_kdj_vjz_g5b__entry__3}

| Name | Type | Description |
|-|-|-|
| sourceTable | String | Name of the table with the attachments to be copied. |
| sourceID | String | Source table's sys_id. |
| targetTable | String | Name of the table on which to add the attachments. |
| targetID | String | Target table's sys_id. |
[Table 2. Parameters]

{#r_SGSA-copy_S_S_S_S__id_kdj_vjz_g5b} {#r_SGSA-copy_S_S_S_S__id_db3_wjz_g5b__entry__2}

| Type | Description |
|-|-|
| String | Array of sys_ids of the attachments that were copied. |
[Table 3. Returns]

{#r_SGSA-copy_S_S_S_S__id_db3_wjz_g5b}  

    var attachment = new GlideSysAttachment();
    var incidentSysID = 'ab1b30031b04ec101363ff37dc4bcbfc';
    var incGR = new GlideRecord('incident');
    incGR.get(incidentSysID);

    var copiedAttachments = attachment.copy('incident', incidentSysID, 'problem', incGR.getValue('problem_id'));
    gs.info('Copied attachments: ' + copiedAttachments);

Output:

    Copied attachments: 6e4621df1bc420501363ff37dc4bcbb0,a87769531b0820501363ff37dc4bcba2

## Scoped GlideSysAttachment - deleteAttachment(String attachmentID) {#ariaid-title4}

Deletes the specified attachment.
{#r_SGSA-deleteAttachment_S__id_ajy_dlz_g5b__entry__3}

| Name | Type | Description |
|-|-|-|
| attachmentID | String | Attachment's sys_id. |
[Table 4. Parameters]

{#r_SGSA-deleteAttachment_S__id_ajy_dlz_g5b} {#r_SGSA-deleteAttachment_S__table_gjy_lyb_fv__entry__2}

| Type | Description |
|-|-|
| void |   |
[Table 5. Returns]

{#r_SGSA-deleteAttachment_S__table_gjy_lyb_fv}  

    var attachment = new GlideSysAttachment();
    var attachmentSysID = 'a87769531b0820501363ff37dc4bcba2';
    attachment.deleteAttachment(attachmentSysID);

## Scoped GlideSysAttachment - getAttachments(String tableName, String sys_id) {#ariaid-title5}

Returns a GlideRecord containing the matching attachment metadata such as name, type,
or size.
{#r_SGSA-getAttachments_S_S__id_bvk_jmz_g5b__entry__3}

| Name | Type | Description |
|-|-|-|
| tableName | String | Name of the table to which the attachment belongs; for example, incident. |
| sys_id | String | Sys_id of record to which the attachment belongs. |
[Table 6. Parameters]

{#r_SGSA-getAttachments_S_S__id_bvk_jmz_g5b} {#r_SGSA-getAttachments_S_S__id_urm_kmz_g5b__entry__2}

| Type | Description |
|-|-|
| GlideRecord | GlideRecord object containing the matching attachment metadata such as name, type, or size. |
[Table 7. Returns]

{#r_SGSA-getAttachments_S_S__id_urm_kmz_g5b}  
The following script lists attachment file names for a record with two attachments.

    var attachment = new GlideSysAttachment();

    var agr = attachment.getAttachments('<table_name>', '<record_sys_id>');

    while(agr.next())
    gs.info(agr.getValue('file_name'));

Output:

    *** Script: filename1.txt
    *** Script: filename2.txt

## Scoped GlideSysAttachment - getContent(GlideRecord sysAttachment) {#ariaid-title6}

Returns the attachment content as a string.
This method is for use in scoped applications only. This method returns `undefined` when used in the global scope. To read attachment content in the global scope, use the getContentStream()
method from the [GlideSysAttachment - Global](https://servicenow-prod.fluidtopics.net/qAqmDcMqsQk4lfWjZJO1bA#GlideSysAttachmentGlobalAPI "The GlideSysAttachment API provides methods for handling attachments.") API.  
This method supports the following file types for attachments.

* CSV (\*.csv)
* JSON (\*.json)
* Text (\*.txt)
{#r_SGSA-getContent_GR__ul_zd2_w21_cwb}
{#r_SGSA-getContent_GR__table_r2d_cng_2v__entry__3}

| Name | Type | Description |
|-|-|-|
| sysAttachment | GlideRecord | Attachment record. |
[Table 8. Parameters]

{#r_SGSA-getContent_GR__table_r2d_cng_2v} {#r_SGSA-getContent_GR__table_s2d_cng_2v__entry__2}

| Type | Description |
|-|-|
| String | Attachment contents as a string. Returns up to 5MB of data. |
[Table 9. Returns]

{#r_SGSA-getContent_GR__table_s2d_cng_2v}  

    var attachment = new GlideSysAttachment();

    var agr = attachment.getAttachments('incident', '78271e1347c12200e0ef563dbb9a7109'); //create attachment GlideRecord

    while(agr.next()) { //for each attachment on the incident record
       gs.info(agr.getValue('file_name')); //print file name of attachment
       var attachmentContent = attachment.getContent(agr);
       gs.info('Attachment content: ' + attachmentContent); //print attachment content
    }

Output:

    Doc1.txt
    Attachment content: I am text in a txt file attached to a record.

## Scoped GlideSysAttachment - getContentBase64(GlideRecord sysAttachment) {#ariaid-title7}

Returns the attachment content as a string with base64 encoding.
This method is for use in scoped applications only. This method returns
`undefined` when used in the global scope. To read attachment content in
the global scope, use the getContentStream method from the
GlideSysAttachment - Global API.
{#r_SGSA-getContentBase64_GR__table_kds_4sb_fv__entry__3}

| Name | Type | Description |
|-|-|-|
| sysAttachment | GlideRecord | Attachment record. |
[Table 10. Parameters]

{#r_SGSA-getContentBase64_GR__table_kds_4sb_fv} {#r_SGSA-getContentBase64_GR__table_lds_4sb_fv__entry__2}

| Type | Description |
|-|-|
| String | Attachment contents as a string with base64 encoding. Returns up to 5MB of data. |
[Table 11. Returns]

{#r_SGSA-getContentBase64_GR__table_lds_4sb_fv}  

    var attachment = new GlideSysAttachment();

    var agr = attachment.getAttachments('incident', '78271e1347c12200e0ef563dbb9a7109'); //create attachment GlideRecord

    while(agr.next()) { //for each attachment on the incident record
       gs.info(agr.getValue('file_name')); //print file name of attachment
       var attachmentContent = attachment.getContentBase64(agr);
       gs.info('Attachment content: ' + attachmentContent); //print attachment content
    }

Output:

    Doc1.txt
    Attachment content: SSBhbSB0ZXh0IGluIGEgdHh0IGZpbGUgYXR0YWNoZWQgdG8gYSByZWNvcmQuIA0=

## Scoped GlideSysAttachment - getContentStream(String sysID) {#ariaid-title8}

Returns a GlideScriptableInputStream object given the sys_id of an
attachment.
You can use the [GlideTextReader](https://servicenow-prod.fluidtopics.net/i7kOFVmwBG0Upc1dPwjSXg#c_GlideTextReaderScopedAPI "The GlideTextReader API provides the ability to read single lines from an input stream. Because an input stream is used, it is not subject to the 5MB attachment size limit.") API to read the content stream.
{#r_SGSA-getContentStream_S__id_xhr_q4z_g5b__entry__3}

| Name | Type | Description |
|-|-|-|
| sysID | String | Attachment sys_id. |
[Table 12. Parameters]

{#r_SGSA-getContentStream_S__id_xhr_q4z_g5b} {#r_SGSA-getContentStream_S__id_yry_q4z_g5b__entry__2}

| Type | Description |
|-|-|
| GlideScriptableInputStream | Stream that contains the attachment content. |
[Table 13. Returns]

{#r_SGSA-getContentStream_S__id_yry_q4z_g5b}  

    var attachment = new GlideSysAttachment();
    var attachmentSysID = '6e4621df1bc420501363ff37dc4bcbb0';
    var attachmentContentStream = attachment.getContentStream(attachmentSysID);
    gs.info('Attachment content stream: ' + attachmentContentStream);

Output:

    Attachment content stream: com.glide.communications.GlideScriptableInputStream@14bd299

## Scoped GlideSysAttachment - write(GlideRecord record, String fileName, String contentType, String content) {#ariaid-title9}

Attaches a specified attachment to the specified record.
{#r_SGSA-write_GR_S_S_S__id_zx4_v4z_g5b__entry__3}

| Name | Type | Description |
|-|-|-|
| record | GlideRecord | Record to which to attach the attachment. |
| fileName | String | Attachment file name. |
| contentType | String | MIME type of the attachment, such as `image/png`. Located in the Attachment \[sys_attachment\] table in the Content type field. |
| content | String | Attachment content. |
[Table 14. Parameters]

{#r_SGSA-write_GR_S_S_S__id_zx4_v4z_g5b} {#r_SGSA-write_GR_S_S_S__id_rnx_v4z_g5b__entry__2}

| Type | Description |
|-|-|
| String | Attachment sys_id. Returns null if the attachment was not added. |
[Table 15. Returns]

{#r_SGSA-write_GR_S_S_S__id_rnx_v4z_g5b}  

    var attachment = new GlideSysAttachment();

    //set up inputs
    var rec = new GlideRecord('incident');
    rec.get('78271e1347c12200e0ef563dbb9a7109');
    var fileName = 'example.txt';
    var contentType = 'text/csv';
    var content = 'The text that is stored inside my file';

    var agr = attachment.write(rec, fileName, contentType, content);

    gs.info('The attachment sys_id is: ' + agr);

Output:

    The attachment sys_id is: 01271e4317c13311e0ef563dbb9abe34

## Scoped GlideSysAttachment - writeBase64(GlideRecord now_GR, String fileName, String contentType, String content_base64Encoded) {#ariaid-title10}

Inserts an attachment for the specified record using base64 encoded
content.
{#r_SGSA-writeBase64_GR_S_S_S__table_nfx_fwx_hx__entry__3}

| Name | Type | Description |
|-|-|-|
| now_GR | GlideRecord | Record to which the attachment is to be attached. |
| fileName | String | Attachment's file name. |
| contentType | String | Attachment's content type. |
| content | String | Attachment content in base64 format. |
[Table 16. Parameters]

{#r_SGSA-writeBase64_GR_S_S_S__table_nfx_fwx_hx} {#r_SGSA-writeBase64_GR_S_S_S__table_ofx_fwx_hx__entry__2}

| Type | Description |
|-|-|
| String | Sys_id of the attachment created. |
[Table 17. Returns]

{#r_SGSA-writeBase64_GR_S_S_S__table_ofx_fwx_hx}  

    var attachment = new GlideSysAttachment();

    var rec = new GlideRecord('incident');
    var incidentSysID = 'ab1b30031b04ec101363ff37dc4bcbfc';
    rec.get(incidentSysID);
    var fileName = 'example.txt';
    var contentType = 'text/csv';
    var base64Encodedcontent = 'SSBhbSB0ZXh0Lg==';

    var agr = attachment.writeBase64(rec, fileName, contentType, base64Encodedcontent);

    gs.info('The attachment sys_id is: ' + agr);

Output:

    The attachment sys_id is: 10cde9971b0820501363ff37dc4bcba6

## Scoped GlideSysAttachment - writeContentStream(GlideRecord now_GR, String fileName, String contentType, GlideScriptableInputStream inputStream) {#ariaid-title11}

Inserts an attachment using the input stream.
{#r_SGS-writeContentStrm_GR_S_S_GSIS__id_n5v_npz_g5b__entry__3}

| Name | Type | Description |
|-|-|-|
| now_GR | GlideRecord | Record to which to attach the attachment. |
| fileName | String | Attachment file name. |
| contentType | String | MIME type of the attachment, such as `image/png`. Located in the Attachment \[sys_attachment\] table in the Content type field. |
| content | GlideScriptableInputStream | Attachment content. |
[Table 18. Parameters]

{#r_SGS-writeContentStrm_GR_S_S_GSIS__id_n5v_npz_g5b} {#r_SGS-writeContentStrm_GR_S_S_GSIS__id_lkf_npz_g5b__entry__2}

| Type | Description |
|-|-|
| String | Sys_id of the attachment created. |
[Table 19. Returns]

{#r_SGS-writeContentStrm_GR_S_S_GSIS__id_lkf_npz_g5b}  
Attaches a content stream from the sys_attachment table to a test_table record.

    function copyAttachmentToGlideRecord(conceptSysId) {

      // Get record from test_table using sys_id
      var targetGlideRecord = new GlideRecord("test_table");
      if (!targetGlideRecord.get(conceptSysId)) {
         throw ("Cannot find record created by test with sys_id: " + conceptSysId);
      }

      // Get record from sys_attachment table
      var sourceAttachmentGlideRecord = new GlideRecord('sys_attachment');    
      sourceAttachmentGlideRecord.query();
      sourceAttachmentGlideRecord.next();

      // Get field values from retrieved sys_attachment record
      var fileName = sourceAttachmentGlideRecord.getValue('file_name');
      var contentType = sourceAttachmentGlideRecord.getValue('content_type');
      var sourceAttachmentSysId = sourceAttachmentGlideRecord.getValue('sys_id');

      // Attach sys_attachment record content stream to test_table record
      var gsa = new GlideSysAttachment();
      gsa.writeContentStream(
        targetGlideRecord,
        fileName,
        contentType,
        gsa.getContentStream(sourceAttachmentSysId));
      gs.info("Attachment created");
    }


