DownloadFromStream Method in Business Central.

Hello,

I'm write my first blog on Business Central.

DownloadFromStream Method is sends a file from server computer to the client computer. 

The client computer is the computer that is running the Windows client or the web client.

#NAV #BC 

Example :

Sales Order List to Download Standard Sales Order.

1. Add Action on Sales Order List Page using Extension

    



2. Create Codeunit to donwload report..

codeunit 50000 "SalesCode"
{
    trigger OnRun()
    begin

    end;

    procedure PrePostedDocument_gFnc(var SalesHeader_iRec: Record "Sales Header")
    var
        TempBlob_lCdu: Codeunit "Temp Blob";
        Out: OutStream;
        Instr: InStream;
        RecRef: RecordRef;
        FileManagement_lCdu: Codeunit "File Management";
        ReportSelection_lRec: Record "Report Selections";
        ReportID: Integer;
        SalesHeader_lRec: Record "Sales Header";
        MyPath: Text;
        ReprotLayoutSelection_lRec: Record "Report Layout Selection";
        CustomReportLayout_lRec: Record "Custom Report Layout";
    begin

        SalesHeader_lRec.Reset;
        SalesHeader_lRec.SetRange("Document Type", SalesHeader_iRec."Document Type");
        SalesHeader_lRec.SetFilter("No.", SalesHeader_iRec."No.");
        SalesHeader_lRec.FindFirst;

        CASE SalesHeader_lRec."Document Type" OF
            SalesHeader_lRec."Document Type"::Order:
                begin
                    ReportSelection_lRec.reset;
                    ReportSelection_lRec.SETRANGE(Usage, ReportSelection_lRec.Usage::"S.Order");
                    IF ReportSelection_lRec.FindFirst then begin
                        ReportID := ReportSelection_lRec."Report ID";
                    end;
                end;
            SalesHeader_lRec."Document Type"::Quote:
                begin
                    begin
                        ReportSelection_lRec.reset;
                        ReportSelection_lRec.SETRANGE(Usage, ReportSelection_lRec.Usage::"S.Quote");
                        IF ReportSelection_lRec.FindFirst then begin
                            ReportID := ReportSelection_lRec."Report ID";
                        end;
                    end;
                end;
            SalesHeader_lRec."Document Type"::"Blanket Order":
                begin
                    begin
                        ReportSelection_lRec.reset;
                        ReportSelection_lRec.SETRANGE(Usage, ReportSelection_lRec.Usage::"S.Blanket");
                        IF ReportSelection_lRec.FindFirst then begin
                            ReportID := ReportSelection_lRec."Report ID";
                        end;
                    end;
                end;
            SalesHeader_lRec."Document Type"::Invoice:
                begin
                    begin
                        ReportSelection_lRec.reset;
                        ReportSelection_lRec.SETRANGE(Usage, ReportSelection_lRec.Usage::"Pro Forma S. Invoice");
                        IF ReportSelection_lRec.FindFirst then begin
                            ReportID := ReportSelection_lRec."Report ID";
                        end;

                    end;
                end;
            SalesHeader_lRec."Document Type"::"Return Order":
                begin
                    begin
                        ReportSelection_lRec.reset;
                        ReportSelection_lRec.SETRANGE(Usage, ReportSelection_lRec.Usage::"S.Return");
                        IF ReportSelection_lRec.FindFirst then begin
                            ReportID := ReportSelection_lRec."Report ID";
                        end;
                    end;
                end;
            SalesHeader_lRec."Document Type"::"Credit Memo":
                begin
                    begin
                        ReportSelection_lRec.reset;
                        ReportSelection_lRec.SETRANGE(Usage, ReportSelection_lRec.Usage::"S.Test");
                        IF ReportSelection_lRec.FindFirst then begin
                            ReportID := ReportSelection_lRec."Report ID";
                        end;
                    end;
                end;
        end;

        IF ReportID > 0 then begin
            ReprotLayoutSelection_lRec.Reset;
            ReprotLayoutSelection_lRec.SetRange("Report ID", ReportID);
            if ReprotLayoutSelection_lRec.FindFirst then begin
                case ReprotLayoutSelection_lRec.Type of
                    ReprotLayoutSelection_lRec.Type::"RDLC (built-in)":
                        begin
                            TempBlob_lCdu.CreateOutStream(Out);
                            RecRef.GetTable(SalesHeader_lRec);
                            REPORT.SaveAs(ReportID, ''REPORTFORMAT::Pdf, Out, RecRef);
                            TempBlob_lCdu.CREATEINSTREAM(Instr);
                            MyPath := STRSUBSTNO('%2_%1.pdf', SalesHeader_lRec."No.", SalesHeader_lRec."Document Type");
                            DOWNLOADFROMSTREAM(Instr, '''''', MyPath);
                        end;
                    ReprotLayoutSelection_lRec.Type::"Word (built-in)":
                        begin
                            TempBlob_lCdu.CreateOutStream(Out);
                            RecRef.GetTable(SalesHeader_lRec);
                            REPORT.SaveAs(ReportID, ''REPORTFORMAT::Word, Out, RecRef);
                            TempBlob_lCdu.CREATEINSTREAM(Instr);
                            MyPath := STRSUBSTNO('%2_%1.docx', SalesHeader_lRec."No.", SalesHeader_lRec."Document Type");
                            DOWNLOADFROMSTREAM(Instr, '''''', MyPath);
                        end;
                    ReprotLayoutSelection_lRec.Type::"Custom Layout":
                        begin
                            ReprotLayoutSelection_lRec.CalcFields("Report Layout Description");
                            CustomReportLayout_lRec.Reset();
                            CustomReportLayout_lRec.SetRange(Code, ReprotLayoutSelection_lRec."Custom Report Layout Code");
                            if CustomReportLayout_lRec.FindFirst then begin
                                case CustomReportLayout_lRec.Type OF
                                    CustomReportLayout_lRec.Type::RDLC:
                                        begin
                                            TempBlob_lCdu.CreateOutStream(Out);
                                            RecRef.GetTable(SalesHeader_lRec);
                                            REPORT.SaveAs(ReportID, ''REPORTFORMAT::Pdf, Out, RecRef);
                                            TempBlob_lCdu.CREATEINSTREAM(Instr);
                                            MyPath := STRSUBSTNO('%2_%1.pdf', SalesHeader_lRec."No.", SalesHeader_lRec."Document Type");
                                            DOWNLOADFROMSTREAM(Instr, '''''', MyPath);
                                        end;
                                    CustomReportLayout_lRec.Type::Word:
                                        begin
                                            TempBlob_lCdu.CreateOutStream(Out);
                                            RecRef.GetTable(SalesHeader_lRec);
                                            REPORT.SaveAs(ReportID, ''REPORTFORMAT::Word, Out, RecRef);
                                            TempBlob_lCdu.CREATEINSTREAM(Instr);
                                            MyPath := STRSUBSTNO('%2_%1.docx', SalesHeader_lRec."No.", SalesHeader_lRec."Document Type");
                                            DOWNLOADFROMSTREAM(Instr, '''''', MyPath);
                                        end
                                end
                            END
                        end;
                end;
            END ELSE begin
                TempBlob_lCdu.CreateOutStream(Out);
                RecRef.GetTable(SalesHeader_lRec);
                REPORT.SaveAs(ReportID, ''REPORTFORMAT::Pdf, Out, RecRef);
                TempBlob_lCdu.CREATEINSTREAM(Instr);
                MyPath := STRSUBSTNO('%2_%1.pdf', SalesHeader_lRec."No.", SalesHeader_lRec."Document Type");
                DOWNLOADFROMSTREAM(Instr, '''''', MyPath);
            end;
        end;
    end;
end;

If you have any question, add comment..

Share you views.    
    


Comments