Zum Hauptinhalt springen

getAttachmentsForCurrentRecord

This function returns a list of all attachments associated with the current record. The list is sorted with the most recent attachments first (by ID in descending order), making it easy to find the latest uploaded files.

Syntax

app.getAttachmentsForCurrentRecord()

Parameters

This function takes no parameters.

Return Value

Returns a Promise that resolves to:

  • An array of attachment objects, each containing metadata about one attachment
  • An empty array if the record has no attachments

Each attachment object in the returned array contains the following properties:

PropertyTypeDescription
idNumberThe unique identifier of the attachment
nameStringThe filename of the attachment
documentTypeIdNumberThe ID of the document type (category)
documentTypeNameStringThe name of the document type (category)
sizeNumberThe file size in bytes
createDateStringThe date when the attachment was created
createUserIdNumberThe ID of the user who created the attachment
createUserNameStringThe username of the user who created the attachment
extensionStringThe file extension (e.g., "pdf", "docx")

Examples

Example 1: List all attachments for the current record:

// Get all attachments and log them to the console
const attachments = app.getAttachmentsForCurrentRecord();
console.log(attachments);

Example 2: Filter attachments by document type:

// Get all attachments for the current record
const allAttachments = app.getAttachmentsForCurrentRecord();

// Filter to find invoices (assuming document type ID 2 represents invoices)
const invoices = allAttachments.filter(attachment => {
return attachment.documentTypeId == 2
});

// Get the first invoice as a blob and print it
if (invoices.length > 0) {
const attachmentBlob = app.getAttachmentById(invoices[0].id);
app.printBlob(attachmentBlob);
}

Example 3: Count attachments by file type:

// Get all attachments
const attachments = app.getAttachmentsForCurrentRecord();

// Group by file extension
const fileTypeCounts = {};
attachments.forEach(attachment => {
const extension = attachment.extension.toLowerCase();
fileTypeCounts[extension] = (fileTypeCounts[extension] || 0) + 1;
});

// Display a summary of file types
for (const fileType in fileTypeCounts) {
console.log(`${fileType}: ${fileTypeCounts[fileType]} files`);
}

Notes

  • This function only returns metadata about the attachments, not the actual file content
  • To retrieve the actual file content, use getAttachmentById() with the ID from one of the returned attachment objects
  • The sorting by ID puts the newest attachments first, as IDs are typically assigned sequentially
  • The function works only with the current record; to get attachments for other records, use different attachment-related functions