# Modify Squeeze origin attachment

## Overview

The `OnAfterSetOriginFileNameOnBeforeModifyDocumentAttachment` integration event allows third-party developers to customize document attachment records before they are modified during the SQUEEZE attachment processing workflow. An origin attachment is the relevant attachment that leads to the creation of a document (e.g. the vendor´s invoice).

## Event Declaration

```PASCAL
[IntegrationEvent(false, false)]
local procedure OnAfterSetOriginFileNameOnBeforeModifyDocumentAttachment(var DocumentAttachment: Record "DXP Document Attachment")
begin
end;
```

## Event Parameters

<div id="bkmrk-parameter-type-descr"><div><table><thead><tr><th style="width: 173px;">Parameter</th><th style="width: 322px;">Type</th><th style="width: 314px;">Description</th></tr></thead><tbody><tr><td style="width: 173px;">`DocumentAttachment`</td><td style="width: 322px;">`Record "DXP Document Attachment"` (var)</td><td style="width: 314px;">The document attachment record that is about to be modified. Passed by reference, allowing modifications.</td></tr></tbody></table>

</div></div>## When This Event is Triggered

This event is fired during the `SaveAttachments` procedure when:

<div id="bkmrk-an-attachment-is-bei">1. An attachment is being processed from SQUEEZE
2. The attachment is identified as an origin file (`IsOriginFile = true`)
3. The system has set the origin file name and `DXP Is Origin File` flag
4. Just before the `DocumentAttachment.Modify(true)` call

</div>## Use Cases

This integration event is useful for:

<div id="bkmrk-custom-field-populat">- **Custom Field Population**: Setting additional custom fields on the document attachment
- **File Name Transformation**: Applying custom naming conventions or formatting
- **Metadata Enhancement**: Adding custom metadata or tags to attachments
- **Validation Logic**: Implementing custom validation before the record is saved
- **Integration Requirements**: Preparing data for external system integrations

</div>## Implementation Example

### Change The Filename Before Modify

```PASCAL
[EventSubscriber(ObjectType::Codeunit, Codeunit::"DXP SQZ API Mgt.", 'OnAfterSetOriginFileNameOnBeforeModifyDocumentAttachment', '', false, false)]
local procedure OnAfterSetOriginFileNameOnBeforeModifyDocumentAttachment(var DocumentAttachment: Record "DXP Document Attachment")
var
    CoreDocument: Record "DXP Document";
    SQZDocumentHeader: Record "DXP SQZ Document Header";
    Vendor: Record Vendor;
    NewFileName: Text[250];
    VendorPrefix: Text[50];
begin
    // Get the core document using the document attachment's Document No.
    if not CoreDocument.Get(DocumentAttachment."Document No.") then
        exit;

    // Check if the core document has a linked SQUEEZE document
    if IsNullGuid(CoreDocument."Linked-to Record Id") then
        exit;

    // Get the SQZ Document Header using the SystemId from Linked-to Record Id
    if not GetSQZDocumentHeaderFromRecordId(CoreDocument."Linked-to Record Id", SQZDocumentHeader) then
        exit;

    // Check if Buy-from Vendor No. is populated
    if SQZDocumentHeader."Buy-from Vendor No." = '' then
        exit;

    // Get the vendor record
    if not Vendor.Get(SQZDocumentHeader."Buy-from Vendor No.") then
        exit;

    // Create vendor prefix from Search Name (fallback to Name if Search Name is empty)
    if Vendor."Search Name" <> '' then
        VendorPrefix := Vendor."Search Name"
    else
        VendorPrefix := Vendor.Name;

    // Clean the vendor prefix (remove invalid filename characters and limit length)
    VendorPrefix := CleanFilenameText(VendorPrefix, 30);

    // Check if vendor prefix already exists in filename to avoid duplicates
    if DocumentAttachment."File Name".StartsWith(VendorPrefix + '_') then
        exit;

    // Create new filename with vendor prefix
    NewFileName := VendorPrefix + '_' + DocumentAttachment."File Name";

    // Update the attachment filename
    DocumentAttachment."File Name" := NewFileName;
end;

local procedure GetSQZDocumentHeaderFromRecordId(LinkedRecordId: Guid; var SQZDocumentHeader: Record "DXP SQZ Document Header"): Boolean
var
    RecRef: RecordRef;
    SystemIdFieldRef: FieldRef;
begin
    // Method 1: Try to get the record directly if LinkedRecordId is actually a SystemId
    if SQZDocumentHeader.GetBySystemId(LinkedRecordId) then
        exit(true);

    // Method 2: If that fails, we need to find the record another way
    // This assumes the Linked-to Record Id might be stored differently
    SQZDocumentHeader.Reset();
    SQZDocumentHeader.SetRange(SystemId, LinkedRecordId);
    exit(SQZDocumentHeader.FindFirst());
end;

local procedure CleanFilenameText(InputText: Text; MaxLength: Integer): Text
var
    CleanText: Text;
begin
    // Remove invalid filename characters
    CleanText := DelChr(InputText, '=', '<>|"/\:*?');
    
    // Replace spaces with underscores for better filename compatibility
    CleanText := CleanText.Replace(' ', '_');
    
    // Limit length
    CleanText := CopyStr(CleanText, 1, MaxLength);
    
    exit(CleanText);
end;
```

## Important Considerations

### Data Integrity

<div id="bkmrk-the%C2%A0documentattachme">- The `DocumentAttachment` record is passed by reference, so any changes will be persisted
- Consider field length limitations when modifying text fields

</div>### Performance

<div id="bkmrk-keep-processing-ligh">- Keep processing lightweight as this event is called for each origin file attachment
- Consider caching frequently accessed data

</div>### Error Handling

<div id="bkmrk-implement-proper-err">- Implement proper error handling to prevent the attachment save process from failing
- Use try-functions for risky operations

</div><div id="bkmrk-"></div>## Related Events

<div id="bkmrk-onaftersaveattachmen">- `OnAfterSaveAttachment`: Triggered after each attachment is completely saved
- Consider using this alternative event if you need to perform actions after the record is saved

</div><div id="bkmrk--0"></div>## Troubleshooting

If your event subscriber isn’t being triggered:

<div id="bkmrk-test-with-origin-fil">1. Test with origin files specifically (non-origin files won’t trigger this event)

</div>