Skip to main content

Notifications

Microsoft Dynamics NAV (Archived)

Error when trying to send pdf file to email

Posted on by Microsoft Employee

Hey guys!

I have following problem.

If i go to Finance management--->Periodic Activities--->Reporting E-Mailing---> Report E-mail Queue

2043.Capture1.PNG

Looks like this


Then i select type, which is invoice, in my case, choose report id, name, receiver e-mail etc.

After im done i click Send Email and Status changes from Ready to Error and description says its Report error. After i click on Send Email once again, NAV sends an empty pdf to the receivers e-mail address.

All of the code is under Page Actions and it looks like this:

SendEmail - OnAction()
Window.OPEN(
  Text001 +
  '@1@@@@@@@@@@@@@@@@@@@@@@@@@\');
Window.UPDATE(1,0);

EmailLog.RESET;
IF EmailLog.FIND('+') THEN
  n:=EmailLog.ID
ELSE
  n:=0;

EmailQueue.RESET;
EmailQueue.SETRANGE(Send,TRUE);
EmailQueue.SETFILTER("Receiver E-mail",'<>%1','');
TotalRecNo:=EmailQueue.COUNT;
RecNo:=0;
IF EmailQueue.FIND('-') THEN
REPEAT

  RecNo+=1;
  Window.UPDATE(1,ROUND(RecNo / TotalRecNo * 10000,1));
  n+=1;

  SalesHeader.RESET;
//  ID1:=EmailQueue.ID;

  CASE EmailQueue.Type OF
    EmailQueue.Type::Invoice:
    BEGIN
      SalesHeader.RESET;
      SalesHeader.SETRANGE("No.",EmailQueue."Document No.");
      //02.10.2015 HD.KV HDST1.00->
      SalesHeader.FINDFIRST;
        IF SalesHeader."Posting No." = '' THEN
        RepOK:= REPORT.SAVEASPDF(EmailQueue."Report ID", TEMPORARYPATH + EmailQueue."Document No."+'.pdf', SalesHeader)
      ELSE
        RepOK:= REPORT.SAVEASPDF(EmailQueue."Report ID", TEMPORARYPATH + SalesHeader."Posting No."+'.pdf', SalesHeader);
 
            //RepOK:= REPORT.SAVEASPDF(EmailQueue."Report ID", TEMPORARYPATH + FORMAT(SESSIONID)+SalesHeader."Posting No."+'.pdf', SalesHeader);
      //<-02.10.2015 HD.KV HDST1.00
  //    MESSAGE(STRSUBSTNO('%1 %2', EmailQueue."Document No.", SalesHeader));
 // RepOK:= REPORT.SAVEASPDF(EmailQueue."Report ID", TEMPORARYPATH + EmailQueue."Document No."+'.pdf', SalesHeader)
    END;
 


    EmailQueue.Type::"Credit Memo":
    BEGIN
      SalesHeader.RESET;
      SalesHeader.SETRANGE("No.",EmailQueue."Document No.");
      //02.10.2015 HD.KV HDST1.00->
      SalesHeader.FINDFIRST;
      IF SalesHeader."Posting No." = '' THEN
        RepOK:= REPORT.SAVEASPDF(EmailQueue."Report ID", TEMPORARYPATH + EmailQueue."Document No."+'.pdf', SalesHeader)
      ELSE
        RepOK:= REPORT.SAVEASPDF(EmailQueue."Report ID", TEMPORARYPATH + SalesHeader."Posting No."+'.pdf', SalesHeader);
       //RepOK:= REPORT.SAVEASPDF(EmailQueue."Report ID", TEMPORARYPATH + FORMAT(SESSIONID)+SalesHeader."Posting No."+'.pdf', SalesHeader);
      //<-02.10.2015 HD.KV HDST1.00
    END;
    EmailQueue.Type::"Posted Invoice":
    BEGIN
      SalesInvoiceHeader.RESET;
      SalesInvoiceHeader.SETRANGE("No.",EmailQueue."Document No.");
      //02.10.2015 HD.KV HDST1.00-> RepOK:= REPORT.SAVEASPDF(EmailQueue."Report ID", TEMPORARYPATH + FORMAT(SESSIONID)+EmailQueue."Document No."+'.pdf', SalesInvoiceHeader);
      RepOK:= REPORT.SAVEASPDF(EmailQueue."Report ID", TEMPORARYPATH + EmailQueue."Document No."+'.pdf', SalesInvoiceHeader);         //02.10.2015 HD.KV HDST1.00
    END;
    EmailQueue.Type::"Posted Credit Memo":
    BEGIN
      SalesCrediMemoHeader.RESET;
      SalesCrediMemoHeader.SETRANGE("No.",EmailQueue."Document No.");
      //02.10.2015 HD.KV HDST1.00-> RepOK:= REPORT.SAVEASPDF(EmailQueue."Report ID", TEMPORARYPATH + FORMAT(SESSIONID)+EmailQueue."Document No."+'.pdf', SalesCrediMemoHeader);
      RepOK:= REPORT.SAVEASPDF(EmailQueue."Report ID", TEMPORARYPATH + EmailQueue."Document No."+'.pdf', SalesCrediMemoHeader);       //02.10.2015 HD.KV HDST1.00
    END;
    EmailQueue.Type::"Vendor Reconcilation":
    BEGIN
//      VendorLedgEntries.RESET;
//      VendorLedgEntries.SETRANGE("Vendor No.",EmailQueue."Document No.");

      VendorReconcil.SetParams(EmailQueue."Document Date",EmailQueue."Document No.");
      RepOK:=VendorReconcil.SAVEASPDF(TEMPORARYPATH + EmailQueue."Document No."+'.pdf');              //02.10.2015 HD.KV HDST1.00
      //02.10.2015 HD.KV HDST1.00->  RepOK:=VendorReconcil.SAVEASPDF(TEMPORARYPATH + FORMAT(SESSIONID)+EmailQueue."Document No."+'.pdf');
//      CLEAR(VendorReconcil);

//      VendorLedgEntries.SETFILTER("Posting Date",'..%1',EmailQueue."Document Date");
//      RepOK:= REPORT.SAVEASPDF(EmailQueue."Report ID", TEMPORARYPATH + FORMAT(SESSIONID)+EmailQueue."Document No."+'.pdf', VendorLedgEntries);
    END;
    EmailQueue.Type::"Customer Reconcilation":
    BEGIN
//      CustomerLedgEntries.RESET;
//      CustomerLedgEntries.SETRANGE("Customer No.",EmailQueue."Document No.");
//      CustomerLedgEntries.SETFILTER("Posting Date",'..%1',EmailQueue."Document Date");
      CustomerReconcil.SetParams(EmailQueue."Document Date",EmailQueue."Document No.");
      RepOK:=CustomerReconcil.SAVEASPDF(TEMPORARYPATH + EmailQueue."Document No."+'.pdf');          //02.10.2015 HD.KV HDST1.00
      //02.10.2015 HD.KV HDST1.00->  RepOK:=CustomerReconcil.SAVEASPDF(TEMPORARYPATH + FORMAT(SESSIONID)+EmailQueue."Document No."+'.pdf');//, CustomerLedgEntries);
//      RepOK:= REPORT.SAVEASPDF(EmailQueue."Report ID", TEMPORARYPATH + FORMAT(SESSIONID)+EmailQueue."Document No."+'.pdf', CustomerLedgEntries);
//      CLEAR(CustomerReconcil);
    END;

  END;
 

  SmtpMail.CreateMessage(CompanyInfo.Name,CompanyInfo."Smtp Report E-mail",EmailQueue."Receiver E-mail",
          EmailQueue."E-mail Subject",EmailQueue."E-mail Subject"{+' ' +EmailQueue."Document No."},FALSE);

  IF SendCopyToSender THEN
    SmtpMail.AddCC(CompanyInfo."Smtp Report E-mail");


  IF RepOK THEN BEGIN
    //02.10.2015 HD.KV HDST1.00->
   
    IF (SalesHeader."Posting No." <> '') AND
      ((EmailQueue.Type = EmailQueue.Type::Invoice) OR (EmailQueue.Type = EmailQueue.Type::"Credit Memo"))
    THEN
      SmtpMail.AddAttachment(TEMPORARYPATH + SalesHeader."Posting No."+'.pdf','')
    ELSE
      SmtpMail.AddAttachment(TEMPORARYPATH + EmailQueue."Document No."+'.pdf','');
      //SmtpMail.AddAttachment(TEMPORARYPATH +FORMAT(SESSIONID)+EmailQueue."Document No."+'.pdf','');
    //<-02.10.2015 HD.KV HDST1.00
   SmtpMail.Send();
  END;

  IF RepOK THEN BEGIN
    EmailLog.INIT;
    EmailLog.ID:=n;
    EmailLog.Type:=EmailQueue.Type;
    EmailLog."Report ID":=EmailQueue."Report ID";
    EmailLog."Document No.":=EmailQueue."Document No.";
    EmailLog."Document Date":=EmailQueue."Document Date";
    EmailLog."Receiver E-mail":=EmailQueue."Receiver E-mail";
    EmailLog."Sent Time":=CURRENTDATETIME;
    EmailLog."Receiver No.":=EmailQueue."Receiver No.";
    EmailLog."User ID":=USERID;
    EmailLog.INSERT;
    EmailQueue.DELETE;
   // MESSAGE(FORMAT(RepOK));
  END ELSE BEGIN
    EmailQueue.Status:=1;
    EmailQueue."Error Description":=ReportError;
    EmailQueue.MODIFY;
  END;
  COMMIT;

  IF RepOK THEN
    //02.10.2015 HD.KV HDST1.00->
    IF (SalesHeader."Posting No." <> '') AND
      ((EmailQueue.Type = EmailQueue.Type::Invoice) OR (EmailQueue.Type = EmailQueue.Type::"Credit Memo"))
    THEN
      ERASE(TEMPORARYPATH + SalesHeader."Posting No."+'.pdf')
    ELSE 
      ERASE(TEMPORARYPATH + EmailQueue."Document No."+'.pdf');
      //ERASE(TEMPORARYPATH +FORMAT(SESSIONID)+EmailQueue."Document No."+'.pdf');
    //<-02.10.2015 HD.KV HDST1.00
  CLEAR(VendorReconcil);
  CLEAR(CustomerReconcil);

RepOK:=FALSE;
UNTIL EmailQueue.NEXT=0;
Window.CLOSE;
CurrPage.UPDATE;

 

 

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Error when trying to send pdf file to email

    Hi, Alexander!

    It literally just goes from this

    5850.1.PNG

    to this

    5850.1.PNG

    So debugger doesnt work as well :/

    What exactly that TEMPORARYPATH should show? As i understood a directory where all the temporary NAV reports are kept, right? But how i supposed to know if its right or not? When i add it on the MESSAGE it doesnt show.

  • ManishS Profile Picture
    ManishS 6,578 on at
    RE: Error when trying to send pdf file to email

    Before generating the PDF check if data exists for generating the report.

  • Suggested answer
    Binesh Profile Picture
    Binesh 7,885 on at
    RE: Error when trying to send pdf file to email

    Hello,

    Agree with other members, check where is your application server, becoz TEMPORARYPATH  returns the Application server temp path, see the bellow screenshot

    2017_2D00_07_2D00_01_5F00_12_2D00_02_2D00_49.png

  • Suggested answer
    Alexander Ermakov Profile Picture
    Alexander Ermakov 28,094 on at
    RE: Error when trying to send pdf file to email

    Can you post a screenshot with the exact error?

  • Suggested answer
    dkatson Profile Picture
    dkatson 2,263 on at
    RE: Error when trying to send pdf file to email

    Try to comment all code after "SmtpMail.CreateMessage..." and check your created .pdf file. How it looks on first error, how it looks after secont send button.

    if pdf if the same and empty than check you report business logic.

    Also i will suggest you to refactor your code, cos now it looks not readible.

    You can watch clean code video from Mark about this. www.youtube.com/watch

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Complete!

🔔 Be sure to subscribe to the new forums you are interested in to stay up to date! 🔔

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 229,918 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans