Removing Attachments Lazily (persistent.removeAttachmentsLazy)
You can use persistent.removeAttachmentsLazy
to remove one or more specific attachments from a document based on their file name or ID. Like other "Lazy" functions, this operation is staged and executed after the script finishes running.
For attachments from the V1 documents the function: persistent.removeAttachmentsLazyV1
can be used.
Syntax
persistent.removeAttachmentsLazy(attachmentsToRemoveArray);
Parameters
The removeAttachmentsLazy function takes a single parameter, which must be an array of objects. Each object in the array represents an attachment to remove and must have the following properties:
Parameter | Type | Required | Description |
---|---|---|---|
valuePath | String | Yes | The path within the target document pointing to the attachment field. |
ddName | String | Yes | The document definition name of the target document. |
_id | String | Yes (if externalId is not provided) |
The unique identifier (ID) of the target document. |
externalId | String | Yes (if _id is not provided) |
An external identifier for the target document. |
fileName | String | Yes (if fileId is not provided) |
The name of the specific file to remove from the field specified by valuePath . |
fileId | String | Yes (if fileName is not provided) |
The ID of the specific file to remove from the field specified by valuePath . |
Return value
This function performs an action and does not return a value.
Examples
Some examples will follow on the different document identifiers.
Example remove _id
The following example shows how to remove a specific attachment by its name from a document identified by its _id
in the 'invoices' document definition:
persistent.removeAttachmentsLazy([
{
valuePath: 'attachments.invoiceFiles',
ddName: 'invoices',
_id: 'invoice_789',
fileName: 'receipt.pdf'
}
]);
Example remove externalId
This example shows how to remove an attachment by its ID from a document identified by its externalId
in the 'reports' document definition:
persistent.removeAttachmentsLazy([
{
valuePath: 'report.generatedFiles',
ddName: 'reports',
externalId: 'REPORT-XYZ',
fileId: 'file_abc'
}
]);