Dynamics 365 Business Central: Automatically add default attachments in email sent (Email Editor) – Customization

Dynamics 365 Business Central

Hi, Readers.
Today I saw an interesting question in Business Central forum, when a user clicks Send by Email in Posted Sales Invoices, is it possible to automatically add the files in Attachments to the attachments in the Email Editor?

For example,

More details: Email Editor (13, Document) – Customise attachment behaviour.

As you know, this step can actually be done manually. After opening the Email Editor (13, Document) page, we can choose Add file from source document to select the documents associated with the Sales invoice header and Sales invoice line.

In addition, we can also choose Add files to add a completely new document, or choose Add files from default selection to add the default documents. We have discussed these related features before. More details:

But this cannot be automated in the standard. So if users need to operate a large number of documents every day, automation is of course necessary. This can be done through customization.
Below is the investigation process. If you are not interested, you can jump directly to the solution at the bottom.
page 143 “Posted Sales Invoices”:

table 112 “Sales Invoice Header”:

table 60 “Document Sending Profile”:

codeunit 260 “Document-Mailing”:

Using OnBeforeEmailFileInternal event:

Let me do a simple example. (You can add controls or filter by document type etc.)

Test video:

Very simple, 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)

codeunit 50113 SendEmailHandler
{
    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Document-Mailing", OnBeforeEmailFileInternal, '', false, false)]
    local procedure OnBeforeEmailFileInternal(var TempEmailItem: Record "Email Item" temporary; var PostedDocNo: Code[20])
    var
        SalesInvoiceHeader: Record "Sales Invoice Header";
        DocAttach: Record "Document Attachment";
        TempBlob: Codeunit "Temp Blob";
        DocumentOutStream: OutStream;
        DocumentInStream: InStream;
    begin
        if SalesInvoiceHeader.Get(PostedDocNo) then begin
            DocAttach.SetRange("Table ID", Database::"Sales Invoice Header");
            DocAttach.SetRange("No.", SalesInvoiceHeader."No.");
            if DocAttach.FindSet() then
                repeat
                    if DocAttach."Document Reference ID".HasValue then begin
                        TempBlob.CreateOutStream(DocumentOutStream);
                        DocAttach."Document Reference ID".ExportStream(DocumentOutStream);
                        TempBlob.CreateInStream(DocumentInStream);
                        TempEmailItem.AddAttachment(DocumentInStream, DocAttach."File Name");
                    end;
                until DocAttach.Next() = 0;
        end;
    end;
}

PS:
Dynamics 365 Business Central: How to add a user signature when sending mail from BC (E-mail Signature)

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL