web
You’re offline. This is a read only version of the page.
close
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 292

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.
I have the same question (0)
  • Suggested answer
    Nitin Verma Profile Picture
    21,708 Moderator on at

    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;
  • Nishan80 Profile Picture
    292 on at

    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.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,238

#2
YUN ZHU Profile Picture

YUN ZHU 773 Super User 2025 Season 2

#3
Sumit Singh Profile Picture

Sumit Singh 630

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans