Skip to content

Adding Attachments Lazily (persistent.addAttachmentsLazy)

You can use persistent.addAttachmentsLazy to add one or more attachments to existing documents. The term "Lazy" suggests that these changes happen after the script is done.

For attachments from the V1 documents the function: persistent.addAttachmentsLazyV1 can be used.

Syntax

persistent.addAttachmentsLazy(attachmentsArray);

Parameters

The addAttachmentsLazy function takes a single parameter, which must be an array of objects. Each object in the array represents an attachment to be added and must have the following properties:

Parameter Type Required Description
valuePath String Yes The path within the target document where the attachment should be linked or stored.
ddName String Yes The name of the document definition the target document belongs to.
_id String Yes (if externalId is not provided) The unique identifier (ID) of the target document. Required if externalId is not used.
externalId String Yes (if _id is not provided) An external identifier for the target document. Required if _id is not used.
content String Yes (must be base64 data URL format) The content of the attachment, encoded as a base64 data URL string (e.g., data:image/png;base64,...).
fileName String Yes The name of the file for the attachment (e.g., document.pdf, image.png).

Return value

Does not return anything, undefined.

Examples

Some examples that can be used inside of scripts.

Example 1 image

The following example shows how to add a single PNG image attachment to a document identified by its _id under the 'reports' document definition:

persistent.addAttachmentsLazy([
  {
    valuePath: 'picture',
    ddName: serverDocument._documentDefinitionName,
    _id: serverDocument._id,
    content: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKEJPQAA+OEfzJVNADtAAAAAElFTkSuQmCC',
    fileName: 'red_dot.png'
  }
]);

Example 2 pdf

This example shows how to add an attachment to a document identified by its externalId under the 'invoices' document definition:

persistent.addAttachmentsLazy([
  {
    valuePath: 'invoiceFile',
    ddName: 'invoices',
    externalId: 'INV-2023-001',
    content: 'data:application/pdf;base64,JVBERi0xLjMKJcOpa....', // Base64 content here
    fileName: 'invoice_document.pdf'
  }
]);