getAttachmentById
This function retrieves an attachment file as a Blob object using its unique ID. This is useful when you already know the specific attachment ID you want to retrieve, either from a previous query or from metadata stored in your application.
Syntax
app.getAttachmentById(attachmentId)
Parameters
Parameter | Type | Description |
---|---|---|
attachmentId | Number | The unique ID of the attachment to retrieve |
Return Value
Returns a Promise that resolves to:
- A Blob object containing the file data if the attachment exists
null
if the attachment cannot be found or retrieved
Examples
Example 1: Basic usage to retrieve and show an attachment:
// Get an attachment with ID 1234
const pdfBlob = app.getAttachmentById(1234);
// Display the content in a document viewer
app.showBlob("documentViewer", pdfBlob);
Example 2: Retrieving and printing an invoice attachment:
// Get all attachments for the current record
const allAttachmentsForThisRecord = app.getAttachmentsForCurrentRecord();
// Filter to find invoices (assuming document type ID 2 represents invoices)
const invoices = allAttachmentsForThisRecord.filter(singleAttachment => {
return singleAttachment.documentTypeId == 2
});
// Get the first invoice as a blob
const attachmentBlob = app.getAttachmentById(invoices[0].id);
// Print the invoice
app.printBlob(attachmentBlob);
Example 3: Retrieving and downloading an attachment:
// Get the attachment with ID 5678
const blob = app.getAttachmentById(5678);
// Use the saveBlob helper function to prompt the user to download it
saveBlob(blob, "document.pdf");
Notes
- This function is often used in combination with
getAttachmentsForCurrentRecord()
to first find the IDs of attachments and then retrieve specific files - For retrieving attachments by filename rather than ID, use
getAttachmentByFileName()
- When working with a large number of attachments, it may be more efficient to use
getAttachmentInfoById()
first to retrieve metadata without downloading the actual file - The function returns a standard JavaScript Blob object that can be used with various browser APIs