Purpose
The purpose of this blog post is to demonstrate how can we generate a financial report in X++ and download it as a file.
Product
Dynamics 365 Finance.
Description
Please find the code snippet below which can be used to generate a financial report in X++. The code generates the report in XML format. The same code can be used to export the report in Excel and XPS file formats. Once generated the code allows the user to download the generated report.
public static void main(Args _args)
{
financialReportVersion financialReportVersion;
FinancialReports financialReports;
PeriodEnd periodEndDate;
str reportName;
str fileContent;
const str fileName = 'FinancialReport.xml';
reportName = "12 Month Rolling Single Column Income Statement - Default";
periodEndDate = FiscalCalendarYear::findYearByCalendarDate(
Ledger::fiscalCalendar(CompanyInfo::find().RecId), systemDateGet()).EndDate;
select firstonly financialReportVersion
order by recid desc
join financialReports
where financialReports.DesignId==financialReportVersion.DesignId
&& financialReports.reportName == reportName;
FinancialReportingIReportExport reportExporter = new FinancialReportingReportExport();
System.Text.Encoding encodingGB18030 = System.Text.Encoding::GetEncoding("utf-8");
if (!financialReports.DesignId)
{
throw error(strfmt("Design not found for Report %1", reportName));
}
System.IO.Stream stream = reportExporter.GenerateAndExportReport(
financialReports.DesignId,
curExt(),
periodEndDate,
FinancialReportingReportExportFormat::Xml,
encodingGB18030);
if (stream == null)
{
throw error(strfmt("Stream is empty for Report %1 period %2",
reportName, periodEndDate));
}
System.IO.StreamReader sReader = new System.IO.StreamReader(stream);
fileContent = sReader.ReadToEnd();
File::SendStringAsFileToUser(fileContent, fileName);
}
Hi Muhammad,
Thanks for this post. I’m trying to achieve the same. But by using your posted code I’m not able to get the output as in excel format. Also I have a requirement to only export the Account level details report (the 2nd tab from the report). Could you please help if you have solution for this.
Thank you,
Pranay