Wednesday 6 October 2021

Send a sales order as PDF attachment in Microsoft Dynamics NAV 2017

 Hello reader here is the code snippet to achieve the above


//Check the sender's email

IF UserSetup.GET(USERID) THEN

  EmailID := UserSetup."E-Mail"//Get the sender's email adress from Approval user setup table

ELSE

  ERROR(MSL0001,USERID);//Display error is the address above is not found

SMTPMailSetup.GET;// table 409

SaleTable.RESET;// Sales header table 36

SaleTable.SETRANGE("Document Type",SaleTable."Document Type"::Order);

SaleTable.SETRANGE("No.",SalesOrder."No.");


IF UserSetup.GET(USERID) THEN

  EmailID := UserSetup."E-Mail"

ELSE

  ERROR(MSL0001,USERID);


IF SaleTable.FINDFIRST THEN BEGIN

  SalesOrderDoc.SETTABLEVIEW(SaleTable);

  FileName := FileMgt.ServerTempFileName('pdf');//Defining the report attachment type [FileName is Text variable]

  REPORT.SAVEASPDF(50071,FileName,SaleTable);// 50071 is the report ID for the sales order confirmation report

  CLEAR(SMTPMail);// Code Unit 400

  SMTPMail.CreateMessage(USERID,SMTPMailSetup."User ID",EmailID,'Booked Sales Order','Please find order confirmation '+SalesOrder."No.",TRUE);

  SMTPMail.AddAttachment(FileName,'Sales Order '+SalesOrder."No."+'.pdf');// Adds the report as attachment


  SMTPMail.AppendBody('<br>');

  SMTPMail.AppendBody('Kindly find the attachment ');

  SMTPMail.AppendBody('<HR>');

  SMTPMail.AppendBody('This is a system generated mail. Please do not reply to this mail!');

  SMTPMail.Send;

  MESSAGE('Mail Sent');

END;



Thats it. Good luck

Credit CloudFonts

No comments:

Post a Comment