Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested answer

How to retrieve files in incoming document table to attach to a mail

(0) ShareShare
ReportReport
Posted on by 290

Hi,

i am trying  to retrieve files attached to Gen. Journal Lines in a batch,  and attach those files to a email.

below the code i used for retrieve the main attachment, but i do not understand how to create InStream from mainattachment so that i can attach it to Email Mesage .

when i run below the code it shows an error  "The Tenant Media does not exist. Identification fields and values: ID='{00000000-0000-0000-0000-000000000000}' "

appreciate if someone could help me 

 genJnlLine.SetRange("Journal Batch Name", genBatch.Name);

            if genJnlLine.FindSet() then begin
                repeat begin
                    incomeDoc.SetRange("Related Record ID", genJnlLine.RecordId);
                    if incomeDoc.FindFirst() then begin
                        if incomeDoc.GetMainAttachment(incomeDocAttach) then begin                            
                            FullFileName := incomeDocAttach.Name + '.' + incomeDocAttach."File Extension";                            
                             incomeDocAttach.Content.CreateInStream(AttachmentStream);
                            EmailMesageCU.AddAttachment(FullFileName, incomeDocAttach."File Extension", AttachmentStream);
                        end;
                    end;
                end;
                until genJnlLine.Next() = 0;
            end;
Thanks,
Nishantha.
  • Nishan80 Profile Picture
    290 on at
    RE: How to retrieve files in incoming document table to attach to a mail

    Hi Nitin,

    Thank you for your code suggestion.

    I could resolve the issue and get attached all the documents to a mail by using below the code.  issue was that i haven't used  incomeDocAttach.CalcFields(Content);  in my previous code. that's why i got previous error. but after referring article  in this url  https://yzhums.com/26326/ ,i manage to get my code working.

    procedure SendEMailWithAttahment(genBatch: Record "Gen. Journal Batch")

       var

           EmailMesageCU: Codeunit "Email Message";

           EmailSendCU: Codeunit Email;

           EmailAccount: Record "Email Account";      

           BodyMessage: Text;

           AddBodyMessage: Text;

           Recipients: list of [text];

           AttachmentStream: InStream;

           incomeDocAttach: Record "Incoming Document Attachment";

           incomeDocAdditionalAttach: Record "Incoming Document Attachment";

           incomeDoc: Record "Incoming Document";

           genJnlLine: Record "Gen. Journal Line";

       begin

           EmailAccount.Reset();

           EmailAccount.SetFilter(Connector, 'SMTP');

           if EmailAccount.FindLast() then;

           Clear(BodyMessage);

           Clear(AddBodyMessage);

           Clear(Recipients);

           Recipients.Add('abc@test.com');

           BodyMessage := 'Document Posted with out Queue';

           AddBodyMessage := BodyMessage;

           EmailMesageCU.Create(Recipients, 'Test Mail', AddBodyMessage, true);

           incomeDoc.Reset();

           genJnlLine.SetRange("Journal Batch Name", genBatch.Name);

           if genJnlLine.FindSet() then begin

               repeat begin

                   incomeDoc.SetRange("Related Record ID", genJnlLine.RecordId);

                   if incomeDoc.FindFirst() then begin

                       if incomeDoc.GetMainAttachment(incomeDocAttach) then begin

                           incomeDocAttach.CalcFields(Content);

                           incomeDocAttach.Content.CreateInStream(AttachmentStream);

                           EmailMesageCU.AddAttachment(incomeDocAttach.GetFullName(), incomeDocAttach."File Extension", AttachmentStream);

                       end;

                       if incomeDoc.GetAdditionalAttachments(incomeDocAdditionalAttach) then begin

                           repeat begin

                               incomeDocAdditionalAttach.CalcFields(Content);

                               incomeDocAdditionalAttach.Content.CreateInStream(AttachmentStream);

                               EmailMesageCU.AddAttachment(incomeDocAdditionalAttach.GetFullName(), incomeDocAdditionalAttach."File Extension", AttachmentStream);

                           end until incomeDocAdditionalAttach.Next() = 0;

                       end;

                   end;

               end;

               until genJnlLine.Next() = 0;

           end;

           EmailSendCU.Enqueue(EmailMesageCU, Enum::"Email Scenario"::Default);

       End;

    Thanks,

    Nishantha.

  • Suggested answer
    Nitin Verma Profile Picture
    21,569 Moderator on at
    RE: How to retrieve files in incoming document table to attach to a mail

    Hi,

    You can use the below code

     local procedure ImportAttachment(FileContent: text)
        var
            BaseConvert: Codeunit "Base64 Convert";
            TempBlob: Codeunit "Temp Blob";
            OutStr: OutStream;
            RecRef: RecordRef;
        begin
            if FileContent = '' then
                exit;
            RecRef.Open(Database::"Incoming Document Attachment");
            RecRef.GetTable(incomingDocumentAtt);
            TempBlob.CreateOutStream(OutStr);
            BaseConvert.FromBase64(FileContent, OutStr);

            TempBlob.ToRecordRef(RecRef, incomingDocumentAtt.FieldNo(Content));
            RecRef.SetTable(incomingDocumentAtt);
            RecRef.Close();
        end;

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Ramesh Kumar – Community Spotlight

We are honored to recognize Ramesh Kumar as our July 2025 Community…

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
Sohail Ahmed Profile Picture

Sohail Ahmed 2,655

#2
Mansi Soni Profile Picture

Mansi Soni 1,574

#3
YUN ZHU Profile Picture

YUN ZHU 1,453 Super User 2025 Season 1

Featured topics

Product updates

Dynamics 365 release plans