The GlideSysAttachment API provides methods for handling attachments.

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

GlideSysAttachment - GlideSysAttachment()

Creates an instance of the GlideSysAttachment class.

Table 1. Parameters
Name Type Description
None

GlideSysAttachment - copy(String sourceTable, String sourceID, String targetTable, String targetID)

Copies attachments from the source record to the target record.

Table 3. Returns
Type Description
String Array of sys_ids of the attachments that were copied.

Example

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

GlideSysAttachment - deleteAttachment(String attachmentID)

Deletes the specified attachment.

Table 4. Parameters
Name Type Description
attachmentID String Attachment's sys_id.
Table 5. Returns
Type Description
void

Example

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

GlideSysAttachment - getAttachments(String tableName, String sys_id)

Returns a GlideRecord containing the matching attachment metadata such as name, type, or size.

Table 6. Parameters
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 7. Returns
Type Description
GlideRecord GlideRecord object containing the matching attachment metadata such as name, type, or size.

Example

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

GlideSysAttachment - getContentStream(String sysID)

Returns a GlideScriptableInputStream object given the sys_id of an attachment.

You can use the GlideTextReader API to read the content stream.

Table 8. Parameters
Name Type Description
sysID String Attachment sys_id.
Table 9. Returns
Type Description
GlideScriptableInputStream Stream that contains the attachment content.

Example

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

GlideSysAttachment - write(GlideRecord record, String fileName, String contentType, String content)

Attaches a specified attachment to the specified record.

Table 11. Returns
Type Description
String Attachment sys_id. Returns null if the attachment was not added.

Example

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

GlideSysAttachment - writeContentStream(GlideRecord now_GR, String fileName, String contentType, GlideScriptableInputStream inputStream)

Inserts an attachment using the input stream.

Table 13. Returns
Type Description
String Sys_id of the attachment created.

Example

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");
}