January 19, 2015

Run, Save and Email Purchase order report through X++ code

Sending SSRS Reports via Email in D365FO: Simplified Steps

Sending SSRS Reports via Email in D365FO: Simplified Steps


In some cases, customers may require the ability to send SSRS reports directly to their email addresses, such as Purchase Order or Sales Order reports. In this blog post, we will demonstrate how to achieve this functionality using simple and straightforward steps.

Step 1: Running the Report and Saving it Locally as PDF

To begin, create a new class and add a method that runs the report and saves it to a local or shared path. Here is an example code snippet:

public str runAndSaveSSRSReport(PurchTable _purchTable)
    // Initialize variables
    SrsReportRunController ssrsController = new SrsReportRunController();
    PurchPurchaseOrderContract contract = new PurchPurchaseOrderContract();
    SRSPrintDestinationSettings printerSettings;
    VendPurchOrderJour vendPurchOrderJour;
    str reportPath;

    // Retrieve the latest purchase order for the specified PurchTable
    select firstOnly vendPurchOrderJour
        order by vendPurchOrderJour.createdDateTime DESC
        where vendPurchOrderJour.PurchId == _purchTable.PurchId;

    // Set the report path to save the PDF
    reportPath = "C:\\" + _purchTable.PurchId + ".pdf";

    // Configure the report controller
    ssrsController.parmReportName(ssrsReportStr(PurchPurchaseOrder, Report));

    // Link the printer settings to the controller
    printerSettings = ssrsController.parmReportContract().parmPrintSettings();

    // Run and save the report

    return reportPath; // Return the file location where the PDF is saved

Step 2: Emailing the Report

Once the report is saved locally, you can proceed with attaching and sending it via email. Add the following method to your class:

public void POConfirmationEmail(PurchTable _purchTable)
    // Initialize variables
    PurchTable purchTable;
    Map parameterMap = new Map(Types::String, Types::String);
    Email requester;
    SysEmailId ApprovalEmailTemplate;
    SysEmailId ReopenEmailTemplate;
    str companyDetails;
    FilenameOpen attachmentFilename;

    companyDetails = curext();
    parameterMap.insert('CompanyDetails', companyDetails);

    purchTable = _purchTable;
    attachmentFilename = this.runAndSaveSSRSReport(purchTable);

    // Set email parameters
    parameterMap.insert('VendorName', VendTable::find(purchTable.OrderAccount).name());
    parameterMap.insert('PurchaseID', purchTable.PurchId);
    requester = LogisticsElectronicAddress::findRecId(DirPartyTable::findRec(VendTable::find(purchTable.OrderAccount).Party).PrimaryContactEmail).Locator;

    if (!requester)
        throw error("No Email address is available");
        // Send email using the "PoEmail" template and delete the report file after sending
        SysEmailTable::sendMail("PoEmail", companyinfo::languageId(), requester, parameterMap, attachmentFilename);

Step 3: Deleting the Report File

Finally, add the following method to your class to delete the report file from the local drive:

public void deleteReportFile(str _reportPath)
    str reportPath = _reportPath;

    if (!reportPath)
        warning("No file in local drive to remove");


By following the above three steps, you can easily send SSRS reports via email in D365FO. Consolidating the code into a single class enhances reusability and simplifies maintenance. Feel free to leave your feedback, questions, or queries in the comments section.

Enjoy the streamlined process!

- Harry


  1. I want to do the same in AX2009 i.e. For Customer Dunning Letter.
    Actually the requirement is when user run the report,the selected customer should get the email with an attachment of the same report same through Batch also.
    Will you help me with the link that might help me to achieve this.
    Thanks in Advance!

  2. Hi,

    I have the same requirement in AX2009,It should send email to the selected customer email id when the Customer Statement report is run or schedule through batch.
    Could you please help me .
    As In AX2009 the report has been extended to runbase not ssrs controller.
    Please let me know if you need more details ,also correct me as I am a beginner in AX.

    1. Hi Binay,

      Thanks for writing. I would suggest to have some specific step where you are stucked or need help. I will help me to understand better.

    2. Thanks Deepeak for your response.
      Customer Account statement report should send email to all the selected customer in the report ,taking email id from its customer card.

      I have tried using print job setting ,but it is generating 1 PDF for all the customer account and sending it to the last customer account.
      What I want is to break the PDF and send separately to the customers.

      My Current logic -
      In fetch method-


      Above code is sending email to the last customer only and combined pdf.
      Could you please suggest .

    3. Can you help me to how I can break the PDF pages for the customer account and send email with Attached PDF separately to each selected customer account.
      Please suggest.

    4. Hi Binay,

      This whole process must execute setpe by step for each and every customer. In your case your are executing all setps once for all customers thats why you are getting single report.

      Try to write your logic for call all these method for each customer, not once for all.

      Hope it will help.

  3. Hi Sir, I have the similar issue with Binay. Can you teach me how to run/include these steps on Customer account statement?

    Thank you in advance.

  4. Hi Sir, I have the similar issue with Binay. Can you show me how to run/include these codes in customer account statement? I'm kinda new in AX.

    Thank you so much,

    1. Hi Paul,

      Surly I'll help. Please let me know at which step you stucked.

  5. Hola Deepak
    You can help me I need do the same but the Dynamics is 4.0.
    How I can do it?

  6. Hi Ensaya, I believe this code will work for AX4.0 as well, I would suggest use the same code and debug if its processing as expected. Troubleshoot where it stucking and share your feedback, than we can discuss where we need tweaks to achieve this.

  7. How easy is it for you to log on and access your email account? Some email programs require only that you have an internet browser and connection. Email made easy

  8. I have read your blog it is very helpful for me. I want to say thanks to you. I have bookmark your site for future updates.
    email search

  9. Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking. find email

  10. Thanks a lot for sharing this excellent info! I am looking forward to seeing more posts by you as soon as possible! I have judged that you do not compromise on quality. Login Tips

  11. Positive site, where did u come up with the information on this posting? I'm pleased I discovered it though, ill be checking back soon to find out what additional posts you include. Learn how to enter your email

  12. I want to send an email when PO is not recieved same way.
    How can i achieve this?

  13. I want to create a job to send an email report in AX 2009.when PO from previous day has not recieved.

  14. Hi,

    I have to create a batch job for sending email report when PO is not recieved to the business.
    How can i achieve this.PLease provide any link

    1. Hi Swarnim,

      Can you pl elaborate the same, with your current system details.
      I will try my best to answer.

    2. Hi,

      I have to prepare a job to send an email if PO from previous day is not recieved in AX 2009.

  15. Hi Deepak,

    I am using AX 2009.My requirement is to send an email alert to business, when a PO is not acknowledged so that it is to be resent.So i need to prepare a job for this.

    Please let me know how can i achieve this.

  16. Hi I am getting this below error
    I called our class in PurchFormLetter class after construct and initialize method

    No message was sent. Language en-us was not found.Function PurchTableAllVersions.initFromProformaPurchTableVersion has been incorrectly called.



Note: Only a member of this blog may post a comment.