Dynamics 365 Business Central: How to limit max file size for uploading attachment (Customization)

Dynamics 365 Business Central

Hi, Readers.
Today I would like to talk about how to limit max file size for uploading attachment 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 vendor’s invoice as a PDF file on the related purchase invoice in Business Central.

However, please note that the database capacity in Business Central Cloud (SaaS) is not unlimited and free.

By default, Business Central customers can use up to 80 GB of database storage capacity across all their environments (production and sandbox). This limit means that the sum of database capacity usage across all of their environments must not exceed 80 GB. If a tenant exceeds this limit, Microsoft restricts administrative actions that create additional environments. Exceeding the storage limit will not interrupt transaction processing within the existing environments.

Some businesses have unique scenarios that may require additional storage. For those organizations that need more space, there’s an option to purchase extra database capacity.

More details: Dynamics 365 Business Central database capacity changes as of July 1, 2021

Of course, Microsoft has also noticed this, and the current maximum size is 350 MB.
More details: Database limits

If you upload more than 350 MB in BC, you will see the message below.

The file could not be uploaded because it was too large. The maximum file size that can be uploaded is 350 MB.

And, this limitation can be changed in the On-Premise version, but for Cloud (SaaS) we cannot change it directly.

So if I want to add more limit in SaaS, for example, change the maximum limit to 20 MB, is there any way?

Yes, but this is not a standard feature, we need to do a little customization. In this post, I will share a simple way that I hope will help you.

The event we will use this time is ‘OnBeforeSaveAttachment‘ in table 1173 “Document Attachment”.

And the main method is procedure Length() in codeunit 4100 “Temp Blob”.

Determines the length of the data stored in the TempBlob.
Returns: The number of bytes stored in the BLOB.

Let’s do a test first.
When uploading an attachment, display the number of bytes of the attachment.

Yeah, it worked well.

Test Video:

Source Code:

codeunit 50111 LimitMaxFileSize
{
    [EventSubscriber(ObjectType::Table, Database::"Document Attachment", 'OnBeforeSaveAttachment', '', false, false)]
    local procedure OnBeforeSaveAttachment(var TempBlob: Codeunit "Temp Blob");
    var
        Msg: Label 'The number of bytes is %1';
    begin
        Message(Msg, TempBlob.Length());
    end;
}

OK, let’s get back to the topic. Because the length is in bytes, it needs to be converted to MB.

Test:

Test Video:

Source Code:

codeunit 50111 LimitMaxFileSize
{
    [EventSubscriber(ObjectType::Table, Database::"Document Attachment", 'OnBeforeSaveAttachment', '', false, false)]
    local procedure OnBeforeSaveAttachment(var TempBlob: Codeunit "Temp Blob");
    var
        Msg: Label 'The attachment exceeds 20 MB and cannot be uploaded. The current size is %1 MB.';
    begin
        if TempBlob.Length() > 20 * 1024 * 1024 then
            Error(Msg, Round(TempBlob.Length() / 1024 / 1024, 0.01, '='));
    end;
}

END

Hope this will help.

Thanks for your reading.

ZHU

コメント

タイトルとURLをコピーしました