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...
Answered

Document attachment not taking new entry for same document no.

(0) ShareShare
ReportReport
Posted on by 281
Hello, I am attaching doc. Via process, but it takes only one document and doesn't insert on new document attachment. What should I change in code for doing this properly., i.e attach one more document for same doc. No. 
 

Procedure attachdoc(No: code[30]; name: text; Ext: text; Base64: Text)

 

    var

 

        DocAttach: Record "Document Attachment";

 

        DocAttach2: Record "Document Attachment";

 

        Base64cu: Codeunit "Base64 Convert";

 

        OutStream: OutStream;

 

        InStream: InStream;

 

        ltext: Text;

 

        tempblob: Codeunit "Temp Blob";

 

        FileMgmnt: Codeunit "File Management";

 

        SalesHdr: Record "Sales Header";

 

        WhseLine: Record "Warehouse Shipment Line";

 

        WhseHdr: Record "Warehouse Shipment Header";

 

        lineno: Integer;

 

    begin

 

        WhseHdr.Reset();

 

        WhseHdr.SetRange("No.", no);

 

        if WhseHdr.Findfirst then begin

 

            Whseline.Reset();

 

            Whseline.setrange("No.", Whsehdr."No.");

 

            if Whseline.FindFirst() then begin

 

                SalesHdr.Reset();

 

                SalesHdr.SetRange("No.", Whseline."Source No.");

 

                SalesHdr.SetRange("Document Type", SalesHdr."Document Type"::"Order");

 

                if SalesHdr.Findlast() then begin

 

                    DocAttach2.SetRange("Table ID", 36);

 

                    DocAttach2.SetRange("No.", SalesHdr."No.");

 

                    if DocAttach2.FindLast() then

 

                        lineno := DocAttach2."Line No." + 10000

 

                    Else

 

                        lineno := 10000;

 

                    DocAttach.Reset();

 

                    DocAttach.SetRange("Table ID", 36);

 

                    DocAttach.SetRange("No.", SalesHdr."No.");

 

                    if not DocAttach.FindFirst() then begin

 

                        DocAttach.Init();

 

                        DocAttach.ID := DocAttach.ID + 1;

 

                        DocAttach."Table ID" := 36;

 

                        DocAttach."Line No." := lineno;

 

                        DocAttach."No." := SalesHdr."No.";

 

                        DocAttach."Document Type" := SalesHdr."Document Type";

 

                        DocAttach."Attached By" := UserSecurityId();

 

                        DocAttach.User := UserId;

 

                        DocAttach."Attached Date" := CurrentDateTime;

 

                        if (ext = 'jpg') then

 

                            DocAttach."File Extension" := ext;

 

                        DocAttach."File Type" := DocAttach."File Type"::Image;

 

                        DocAttach."File Name" := FileMgmnt.GetFileNameWithoutExtension(name);

 

                        TempBlob.CreateOutStream(OutStream);

 

                        Base64cu.FromBase64(Base64, outStream);

 

                        TempBlob.CreateInStream(InStream);

 

                        CopyStream(OutStream, InStream);

 

                        DocAttach."Document Reference ID".ImportStream(InStream, '');

 

                        DocAttach.Insert();

 

                    end;

 

                end;

 

            end;

 

        end;

 

    end;

 
 
 
I have the same question (0)
  • Verified answer
    Holly Huffman Profile Picture
    6,538 Super User 2025 Season 2 on at
    Hi there!
     
    Looks like current code checks to see if a document is there, and if not - then attaches. 
    For code below, each new attachment will be given a new line number, ensuring multiple attachments can be added to the same document. 
     
    Hope this helps! 
     
    procedure AttachDoc(No: Code[30]; Name: Text; Ext: Text; Base64: Text)
    var
        DocAttach: Record "Document Attachment";
        DocAttach2: Record "Document Attachment";
        Base64cu: Codeunit "Base64 Convert";
        OutStream: OutStream;
        InStream: InStream;
        TempBlob: Codeunit "Temp Blob";
        FileMgmnt: Codeunit "File Management";
        SalesHdr: Record "Sales Header";
        WhseLine: Record "Warehouse Shipment Line";
        WhseHdr: Record "Warehouse Shipment Header";
        LineNo: Integer;
    begin
        WhseHdr.Reset();
        WhseHdr.SetRange("No.", No);
        if WhseHdr.FindFirst() then begin
            WhseLine.Reset();
            WhseLine.SetRange("No.", WhseHdr."No.");
            if WhseLine.FindFirst() then begin
                SalesHdr.Reset();
                SalesHdr.SetRange("No.", WhseLine."Source No.");
                SalesHdr.SetRange("Document Type", SalesHdr."Document Type"::Order);
                if SalesHdr.FindLast() then begin
                    DocAttach2.SetRange("Table ID", 36);
                    DocAttach2.SetRange("No.", SalesHdr."No.");
                    if DocAttach2.FindLast() then
                        LineNo := DocAttach2."Line No." + 10000
                    else
                        LineNo := 10000;
                    DocAttach.Init();
                    DocAttach.ID := DocAttach.ID + 1;
                    DocAttach."Table ID" := 36;
                    DocAttach."Line No." := LineNo;
                    DocAttach."No." := SalesHdr."No.";
                    DocAttach."Document Type" := SalesHdr."Document Type";
                    DocAttach."Attached By" := UserSecurityId();
                    DocAttach.User := UserId;
                    DocAttach."Attached Date" := CurrentDateTime;
                    if (Ext = 'jpg') then
                        DocAttach."File Extension" := Ext;
                    DocAttach."File Type" := DocAttach."File Type"::Image;
                    DocAttach."File Name" := FileMgmnt.GetFileNameWithoutExtension(Name);
                    TempBlob.CreateOutStream(OutStream);
                    Base64cu.FromBase64(Base64, OutStream);
                    TempBlob.CreateInStream(InStream);
                    CopyStream(OutStream, InStream);
                    DocAttach."Document Reference ID".ImportStream(InStream, '');
                    DocAttach.Insert();
                end;
            end;
        end;
    end;
     
  • Suggested answer
    Suresh Kulla Profile Picture
    50,245 Super User 2025 Season 2 on at
    The key fields for Document Attachment are Table ID, No. Document Type, Line No., ID so you need to make sure Line No. is updated for every entry if it is the same Document or Master Data.
     
     
  • Suggested answer
    Khushbu Rajvi. Profile Picture
    20,612 Super User 2025 Season 2 on at
    To allow multiple document attachments for the same document number, modify your code to use DocAttach.FindLast() to get the most recent attachment and increment the "Line No." for each new document. This ensures that each attachment gets a unique "Line No." and multiple documents can be attached to the same record.

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,135

#2
YUN ZHU Profile Picture

YUN ZHU 733 Super User 2025 Season 2

#3
Sumit Singh Profile Picture

Sumit Singh 612

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans