AttachmentManager
AttachmentManager methods for moving, copying and validating attachments in server scripts, and the administrator methods for attachment storage.
AttachmentManager moves attachments between fields, copies email and message attachments onto records, and validates attachments created by script. It is a global object in server scripts, such as triggers, script libraries, jobs, inbound message scripts and system controllers.
Get an instance
Call the methods on the global object.
AttachmentManager.moveAttachments(current, "Attachments", "ResolutionAttachments", [attachmentId]);
Methods
A field that holds attachments is either a single reference (one attachment) or a multiple reference (a list).
| Method | Returns | Description | Since |
|---|---|---|---|
moveAttachments(record: TableRecord, fromField: string, toField: string) |
TableRecord | Moves the attachment from a single-reference field to another field and saves the record. | 1.11 |
moveAttachments(record: TableRecord, fromField: string, toField: string, attachmentIds: string[]) |
TableRecord | Moves the listed attachments out of a multiple-reference field and saves the record. Required when fromField is a multiple reference. |
1.11 |
copyEmailAttachmentsTo(fromEmailId: string, field: Field) |
nothing | Copies the attachments of a SystemEmail record into an attachment field. A single-reference field takes the first attachment. A journal field gets them as links. |
1.11 |
copyEmailAttachmentsTo(fromEmailId: string, relation: Relation) |
nothing | Copies the attachments of a SystemEmail record into a relation to the Attachment table, added to any already there. |
1.11 |
copyEmailAttachmentsToJournal(fromEmailId: string, field: Field, journalEntry: string) |
nothing | Copies an email’s attachments to the record and adds the journal entry with links to them. Inline images in the entry are relinked to the copies. | 1.11 |
copyEmailAttachmentsToJournal(fromEmailId: string, field: Field, journalEntry: string, emailData: object) |
nothing | As above. When emailData.is_auto_reply is true, the entry is formatted as an auto-reply with emailData.subject. |
1.11 |
copyMessageAttachmentsToJournal(messageRecordId: string, messageClass: string, journalField: Field, journalEntry: string) |
nothing | Copies the attachments of a message record of the given table to the record and adds the journal entry with links. Ignored with a warning if the field is not a journal. | 1.11 |
validateAttachment(record: TableRecord) |
string[] | Checks an Attachment record’s file name, type and data against the upload rules. Returns the error messages, or an empty array. | 1.11 |
Copied attachments take the target field’s record as their parent.
When emailData.is_auto_reply is true, attachments are copied only if the application property notification.email.autoreply.add.attachments is true.
Administrator methods
These methods exist only when the script runs as an administrator. For other users they are not on the object. They log progress to the script log.
| Method | Returns | Description | Since |
|---|---|---|---|
runAttachmentFileRemoval() |
nothing | Starts attachment file removal now instead of at its nightly run. Does nothing while removal is not enabled. | 1.11 |
migrateAttachments() |
nothing | Starts moving attachment data from the database to S3 in the background. Runs only while system.attachment.migration.enabled is true, within system.attachment.migration.allowed_hours. |
1.11 |
migrateAttachments(cursor: string) |
nothing | Restarts a migration after the attachment ID logged when it stopped. | 1.11 |
migrateAttachment(ids: string[]) |
object | Moves the named attachments to S3 now, without pacing or size limits. Returns an outcome per ID, such as moved or not found. |
1.11 |
| Method | Returns | Description | Since |
|---|---|---|---|
migrateAttachmentSizes() |
nothing | Fills in the Size of attachments that have none, in background passes. | 2027.1 |
migrateAttachmentSizes(fromCreatedOnMillis: number, afterId: string) |
nothing | Continues the size backfill from a logged position. | 2027.1 |
Examples
Move selected attachments
This script moves one attachment from the multiple-reference field Attachments into a second attachment field. The field names and the ID are examples.
Servicely Script
let attachmentId = "00000000000000000000000000000001";
AttachmentManager.moveAttachments(
current, "Attachments", "ResolutionAttachments", [attachmentId]
);
Response
// current.Attachments -> no longer holds the attachment
// current.ResolutionAttachments -> now holds it
Validate a scripted attachment
Servicely Script
let errors = AttachmentManager.validateAttachment(current);
if (errors.length > 0) {
log.warn("Attachment refused: " + errors.join(", "));
}
Response
// [] when the attachment passes
// ["<validation message>"] when it does not