Dynamics 365 Business Central: How to prevent users from uploading duplicate files as attachments (Duplicate file names)

Dynamics 365 Business Central

Hi, Readers.
Today I would like to talk about how to prevent users from uploading duplicate files as attachments in Business Central. This is a question I saw on the BC Forum before, Attachments in Business Central.

In the FactBox on most cards and documents, you can attach any type of file, containing text, image, or video, to a card or document. This is useful, for example, when you want to store a customer’s invoice as a PDF file on the related sales invoice in Business Central.

PS: Business Central 2024 wave 2 (BC25): Use drag and drop on file upload dialog to attach multiple files (New Documents Factbox)

But there is a small problem here. In the standard Business Central, it is allowed to upload the same file multiple times, which means duplicate files. For example,
Uploading the same file twice:

Upload the same file three times:

So, can we prevent this behavior? First, let’s take a look at this part of the standard code.

This is controlled by the following code in table 1173 “Document Attachment”, which has a variable AllowDuplicateFileName: Boolean. (The same processing is performed in different procedures)

There is a duplicate name check in the OnValidate() trigger of “File Name” field, but the standard code modifies the name when uploading the file.

And, there is no Allow Duplicate FileName setting in the BC, and there is no event to force the value of this variable.

So this can’t be done in the standard, and requires some simple customization. Let’s look at a simple test.

First, I added a setting in the General Ledger Setup.

I then control this standard behavior via the OnInsertAttachmentOnBeforeImportStream event.

Test video:

This is just a simple example. You can find a more suitable solution according to your needs. Give it a try!!!😁

Source code: Github (Please note that the source code is for reference only, you can improve it according to your own needs)

tableextension 50121 GeneralLedgerSetupExt extends "General Ledger Setup"
{
    fields
    {
        field(50100; "Allow Duplicate FileName"; Boolean)
        {
            Caption = 'Allow Duplicate FileName';
            DataClassification = CustomerContent;
        }
    }
}

pageextension 50121 GeneralLedgerSetupExt extends "General Ledger Setup"
{
    layout
    {
        addafter("Allow Posting To")
        {
            field("Allow Duplicate FileName"; Rec."Allow Duplicate FileName")
            {
                ApplicationArea = All;
                ToolTip = 'Specifies whether duplicate file names are allowed for document attachments.';
            }
        }
    }
}

codeunit 50121 AttachmentsHandler
{
    [EventSubscriber(ObjectType::Table, Database::"Document Attachment", OnInsertAttachmentOnBeforeImportStream, '', false, false)]
    local procedure "Document Attachment_OnInsertAttachmentOnBeforeImportStream"(var DocumentAttachment: Record "Document Attachment"; FileName: Text)
    var
        DocumentAttachmentMgmt: Codeunit "Document Attachment Mgmt";
        FileManagement: Codeunit "File Management";
        GeneralLedgerSetup: Record "General Ledger Setup";
    begin
        if GeneralLedgerSetup.Get() then
            if not GeneralLedgerSetup."Allow Duplicate FileName" then
                if DocumentAttachmentMgmt.IsDuplicateFile(DocumentAttachment."Table ID", DocumentAttachment."No.", DocumentAttachment."Document Type", DocumentAttachment."Line No.", FileManagement.GetFileNameWithoutExtension(FileName), FileManagement.GetExtension(FileName)) then
                    Error('A document with the same name already exists.');
    end;
}

END

Hope this will help.

Thanks for reading.

ZHU

コメント