Skip to main content

Notifications

Small and medium business | Business Central, N...
Answered

WriteMode , temp blob, not able to generate report, email sending

Posted on by 83

Dear friends,

i am getting the following error when want to create report in pdf and send as attachment in business central.

"The current setting of the WriteMode property is incompatible with the operation."

my code

//----------------
        TempBlob.CreateOutStream(OutStr);
        TempBlob.CreateInStream(InStrm);
if bemailrec.FindSet then begin
REPORT.SAVEAS(ReportID, '', REPORTFORMAT::Pdf, Out, RecRef);
Base64EncodedString := Base64.ToBase64('My Message in Gas Prices 50100 ');
end;
//----------------
  • Hafiz@d365 Profile Picture
    Hafiz@d365 83 on at
    RE: WriteMode , temp blob, not able to generate report, email sending

    Thank you

  • Verified answer
    Gianfranco C. Profile Picture
    Gianfranco C. 370 on at
    RE: WriteMode , temp blob, not able to generate report, email sending

    as I told you before, the variable of type OutStream other than the one you instantiated, and the variable RecRef is not retrieved.

    Correct like this:

       bemailrec.SetRange("Price Date", bfromDt, Krundate);
    
    
    
            //----------------
    
            TempBlob.CreateOutStream(OutStr);
            //TempBlob.CreateInStream(InStrm);
            //----------------
            if bemailrec.FindSet then begin
                RecRef.GetTable(bemailrec);
                Message('Here Gas Prices -1');
                REPORT.SAVEAS(ReportID, '', REPORTFORMAT::Pdf, OutStr, RecRef);
                Base64EncodedString := Base64.ToBase64('My Message in Gas Prices 50100 ');
    
                //pdf report now is in TempBlob  ... 
                TempBlob.CreateInStream(InStrm);
                //now the pdf is in InStrm and you can use it 
                //
            end

    Base64EncodedString what it is for? it is not used...

    Don't forget to help the community by Verifying the answer and Like it if your question has been answered. It will let others know that the topic has verified answer.

    Thanks & Regards,
    Gian

  • Hafiz@d365 Profile Picture
    Hafiz@d365 83 on at
    RE: WriteMode , temp blob, not able to generate report, email sending

    //---

     action("Send Email")

               {

                   Image = Email;

                   trigger OnAction()

                   begin

                       CurrPage.Update;

                       noofemails := 0;

                       emailNote.Reset;

                       emailNote.SetRange("Price Date", rec."Gas Price Period");

                       //emailNote.SETRANGE("Email Count",0,salessetup."email count"+5);

                       emailNote.SETRANGE("Send Email", TRUE);

                       //emailNote.SETFILTER("Customer No",'BG-CS-10032');

                       if emailNote.FindSet then begin

                           repeat

                               Consmaster.Reset;

                               Consmaster.SetFilter("Consumer Id", emailNote."Consumer Id");

                               // Consmaster.SETRANGE(Active,TRUE);

                               if Consmaster.Find('-') then begin

                                   if Consmaster."E-Mail" <> '' then begin

                                       Clear(cu501);

                                       Message('At gas Price' + emailNote."Consumer Id" + ' Email ' + Consmaster."E-Mail");

                                       SendSalesShipment(emailNote, emailNote."Order No", emailNote."Customer No", emailNote."Price Date", emailNote.Segment, emailNote."Consumer Id");

                                       noofemails := noofemails + 1;

                                       emailNote."Email Count" := emailNote."Email Count" + 1;

                                       emailNote."Email Date" := WorkDate;

                                       emailNote.Modify;

                                   end;

                               end;

                           //  emailNote.MODIFY;

                           until emailNote.Next = 0;

                       end else begin

                           Error('No Records to send');

                       end;

                       Message('Number of Emails Sent ' + Format(noofemails));

                   end;

               }

    //---------------

    /procedures

    //---------

    procedure SendSalesShipment(SalesShipmentHeader: Record "BGS_Email Notices"; OrderNo: Code[20]; CustNo: Code[20]; srundate: Date; seg1: Code[20]; ConsNo: Code[20]): Boolean

       var

           Customer: Record "BGS_Consumer Master2";

           EmailBodyFilePath: Text;

           FileManagement: Codeunit "File Management";

           ReportID: Integer;

           BodyLayoutCode: Code[20];

           emailrec: Record "BGS_Email Notices";

           fromDt: Date;

           todt: Date;

       begin

           Customer.Reset;

           Customer.SetFilter("Consumer Id", ConsNo);

           if Customer.FindSet then begin

           end else begin

               exit;

           end;

           TempEmailItem."Send to" := Customer."CC Email";

           TempEmailItem."Send CC" := Customer."CC Email 2";

           TempEmailItem."Send BCC" := '';

           TempEmailItem."Send to" := Customer."E-Mail";

           if Customer."CC Email" <> '' then

               TempEmailItem."Send CC" := Customer."CC Email";

           if Customer."CC Email 2" <> '' then

               TempEmailItem."Send BCC" := Customer."CC Email 2";

           ConsName := SalesShipmentHeader."Customer Name";

           Consmaster.Reset;

           Consmaster.SetFilter("Consumer Id", ConsNo);

           if Consmaster.FindSet then

               ConsName := Consmaster."Consumer Name";

           TempEmailItem.Subject := StrSubstNo(ConsName + ' - Monthly Price Revision for month of ' + Format(srundate, 0, '<Month Text,3> <Year4>'));

           fromDt := CalcDate('<-1M>', srundate);

           fromDt := CalcDate('-CM', CalcDate('<-1M>', srundate));

           emailrec.Reset;

           emailrec.SetFilter("Customer No", CustNo);

           emailrec.SetFilter("Order No", OrderNo);

           emailrec.SetRange("Price Date", srundate);

           if emailrec.FindSet then begin

               Message('At Code Unit' + emailrec."Consumer Id" + ' Email ' + Consmaster."E-Mail");

               if emailrec.Segment = 'BULK' then begin

                   GetReportIDandLayoutCode(ReportID, BodyLayoutCode, emailrec.Segment);

                   //  "Attachment File Path" := GenerateDocumentEmailAttachment(emailrec, 50001, srundate, OrderNo, CustNo, seg1);

               end else begin

                   GetReportIDandLayoutCode(ReportID, BodyLayoutCode, emailrec.Segment);

                   // "Attachment File Path" := GenerateDocumentEmailAttachment(emailrec, 50000, srundate, OrderNo, CustNo, seg1);

               end;

               //"Attachment File Path" := GenerateDocumentEmailAttachment(emailrec,ReportID,srundate,OrderNo,CustNo,seg1);

               // "Attachment Name" := StrSubstNo('Monthly Price Note %1', ConsName + '.PDF');

               Clear(Rep50000);

               if emailrec.Segment <> 'BULK' then begin

                   Clear(Rep50000);

                   Rep50000.GetArgs(ConsNo, OrderNo, fromDt, srundate, seg1);

               end else begin

                   Clear(Rep50001);

                   Rep50001.GetArgs(ConsNo, OrderNo, fromDt, srundate, seg1);

               end;

               if emailrec.Segment <> 'BULK' then

                   GenerateShipmentEmailBody(emailrec, 50000, EmailBodyFilePath, BodyLayoutCode, OrderNo, CustNo, srundate, emailrec.Segment)

               else

                   GenerateShipmentEmailBody(emailrec, 50001, EmailBodyFilePath, BodyLayoutCode, OrderNo, CustNo, srundate, emailrec.Segment);

               TempEmailItem.Validate(TempEmailItem."Plaintext Formatted", false);

           end;

       end;

       local procedure GenerateShipmentEmailBody(DocumentVariant: Record "BGS_Email Notices"; ReportID: Integer; var BodyFilePath: Text; LayoutCode: Code[20]; Korderno: Code[20]; Kcustno: Code[20]; Krundate: Date; BulkorMMBS: Code[20])

       var

           FileManagement: Codeunit "File Management";

           ReportLayoutSelection: Record "Report Layout Selection";

           bemailrec: Record "BGS_Email Notices";

           bfromDt: Date;

           btodt: Date;

           TempBlob_lRec: Codeunit "Temp Blob";

           Out: OutStream;

           RecRef: RecordRef;

           attachment1:File;

           //-----------

           TempBlob: Codeunit "Temp Blob";

           OutStr: OutStream;

           InStrm: InStream;

           Base64EncodedString: Text;

           Base64: Codeunit "Base64 Convert";

       //-----------

       begin

           ReportLayoutSelection.SetTempLayoutSelected(LayoutCode);

           bfromDt := CalcDate('<-1M>', Krundate);

           Clear(Rep50000);

           if BulkorMMBS <> 'BULK' then

               Rep50000.GetArgs(Kcustno, Korderno, bfromDt, Krundate, BulkorMMBS)

           else

               Rep50001.GetArgs(Kcustno, Korderno, bfromDt, Krundate, BulkorMMBS);

           bemailrec.Reset;

           bemailrec.SetFilter("Customer No", Kcustno);

           bemailrec.SetFilter("Order No", Korderno);

           bemailrec.SetRange("Price Date", bfromDt, Krundate);

           //----------------

           TempBlob.CreateOutStream(OutStr);

           TempBlob.CreateInStream(InStrm);

           //----------------

           if bemailrec.FindSet then begin

               Message('Here Gas Prices -1');

               REPORT.SAVEAS(ReportID, '', REPORTFORMAT::Pdf, Out, RecRef);

               Base64EncodedString := Base64.ToBase64('My Message in Gas Prices 50100 ');

           end

       end;

       local procedure GenerateDocumentEmailAttachment(DocumentVariant: Record "BGS_Email Notices"; ReportID: Integer; zdate: Date; zorder: Code[20]; zcust: Code[20]; BM3: Code[20]): Text

       var

           FileManagement: Codeunit "File Management";

           FilePath: Text;

           Zemailrec: Record "BGS_Email Notices";

           zfromDt: Date;

           TempBlob_lRec: Codeunit "Temp Blob";

           Out: OutStream;

           RecRef: RecordRef;

           FileManagement_lCdu: Codeunit "File Management";

       begin

           // FilePath := CopyStr(FileManagement.ServerTempFileName('pdf'), 1, 250);

           zfromDt := CalcDate('<-1M>', zdate);

           Zemailrec.Reset;

           Zemailrec.SetFilter("Customer No", zcust);

           Zemailrec.SetFilter("Order No", zorder);

           Zemailrec.SetRange("Price Date", zfromDt, zdate);

           if Zemailrec.FindSet then begin

               Message('Here Gas Prices');

               if Zemailrec.Segment <> 'BULK' then

                   REPORT.SAVEAS(ReportID, '', REPORTFORMAT::Pdf, Out, RecRef)    // save report in TempBlob di recRef

               else

                   REPORT.SAVEAS(ReportID, '', REPORTFORMAT::Pdf, Out, RecRef);    // save report in TempBlob di recRef

               exit(FilePath);

           end;

       end;

       local procedure GetReportIDandLayoutCode(var ReportID: Integer; var LayoutCode: Code[20]; bulkMMBS: Code[20])

       var

           ReportSelections: Record "Report Selections";

       begin

           ReportSelections.SetRange(Usage, ReportSelections.Usage::"C.Statement");

           if ReportSelections.FindFirst then

               if bulkMMBS <> 'BULK' then begin

                   ReportID := 50100;

                   LayoutCode := '50000-000001';

               end else begin

                   ReportID := 50101;

                   LayoutCode := '50001-000001';

               end;

           ReportSelections.SetRange("Use for Email Body", true);

           // IF ReportSelections.FINDFIRST THEN

           //  LayoutCode := ReportSelections."Email Body Layout Code";

       end;

    }

    //--------

    please help

  • Suggested answer
    Gianfranco C. Profile Picture
    Gianfranco C. 370 on at
    RE: WriteMode , temp blob, not able to generate report, email sending

    Hi, is the variable 'Out' in Report.saveAs a typo?
    it should be OutStr like yours.

    the CreateInStr in that position is incorrect, it should be placed after the Report.SaveAs, to retrieve the report created and stored in the "temp blob" stream.

    if it is easier for you, paste your complete code and what you do after creating the pdf.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

New! Quick response templatesâš¡

Save time with the new custom templates!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,134 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 229,928 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans