Sending SSRS Reports via Email in D365FO: Simplified Steps
Introduction
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));
ssrsController.parmExecutionMode(SysOperationExecutionMode::Synchronous);
ssrsController.parmShowDialog(false);
contract.parmRecordId(vendPurchOrderJour.RecId);
ssrsController.parmReportContract().parmRdpContract(contract);
// Link the printer settings to the controller
printerSettings = ssrsController.parmReportContract().parmPrintSettings();
printerSettings.printMediumType(SRSPrintMediumType::File);
printerSettings.fileFormat(SRSReportFileFormat::PDF);
printerSettings.overwriteFile(true);
printerSettings.fileName(@reportPath);
// Run and save the report
ssrsController.runReport();
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");
}
else
{
// Send email using the "PoEmail" template and delete the report file after sending
SysEmailTable::sendMail("PoEmail", companyinfo::languageId(), requester, parameterMap, attachmentFilename);
this.deleteReportFile(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");
else
WinAPI::deleteFile(@reportPath);
}
Conclusion
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
I want to do the same in AX2009 i.e. For Customer Dunning Letter.
ReplyDeleteActually 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!
Hi,
ReplyDeleteI 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.
Hi Binay,
DeleteThanks for writing. I would suggest to have some specific step where you are stucked or need help. I will help me to understand better.
Thanks Deepeak for your response.
DeleteCustomer 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-
element.printJobSettings().Format(printformat::PDF);
element.setTarget(PrintMedium::Mail);
element.printJobSettings().preferredTarget(PrintMedium::Mail);
element.printJobSettings().preferredFileFormat(Printformat::PDF);
element.printJobSettings().preferredMailFormat(printformat::PDF);
element.printJobSettings().mailTo(custTable.Email);
Above code is sending email to the last customer only and combined pdf.
Could you please suggest .
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.
DeletePlease suggest.
Hi Binay,
DeleteThis 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.
Hi Sir, I have the similar issue with Binay. Can you teach me how to run/include these steps on Customer account statement?
ReplyDeleteThank you in advance.
Regards,
Paul
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.
ReplyDeleteThank you so much,
Regards.
Hi Paul,
DeleteSurly I'll help. Please let me know at which step you stucked.
Hola Deepak
ReplyDeleteYou can help me I need do the same but the Dynamics is 4.0.
How I can do it?
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.
ReplyDeleteHow 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
ReplyDeleteI 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.
ReplyDeleteemail search
Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking. find email
ReplyDeleteThanks 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
ReplyDeletePositive 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
ReplyDeleteI want to send an email when PO is not recieved same way.
ReplyDeleteHow can i achieve this?
I want to create a job to send an email report in AX 2009.when PO from previous day has not recieved.
ReplyDeleteHi,
ReplyDeleteI 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
Hi Swarnim,
DeleteCan you pl elaborate the same, with your current system details.
I will try my best to answer.
Hi,
DeleteI have to prepare a job to send an email if PO from previous day is not recieved in AX 2009.
Hi Deepak,
ReplyDeleteI 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.
Hi I am getting this below error
ReplyDeleteI 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.