Writing a report to file, through code
I was working with another co-worker on a way to generate reports in AX as a PDF, and then Automatically email these out. One of the first things that needed to be done was call the report and save it to a file, without having user interaction.
Below is code that will call the SalesInvoice and generate it as a PDF file:
CustInvoiceJour InvJTbl;
SalesId Id;
ReportRun report;
RecordSortedList List = new RecordSortedList(62);
Id = "SO-0000123";
Select InvJTbl Where InvJTbl.SalesId == Id;
List.ins(InvJTbl);
report = new ReportRun(new Args(ReportStr(SalesInvoice)));
report.args().caller(null);
report.args().parmEnum(1);
report.args().parmEnumType(920);
report.args().object(List);
report.args().name("KeepSettings");
report.printJobSettings().setTarget(PrintMedium::File);
report.printJobSettings().format(PrintFormat::PDF);
report.printJobSettings().fileName("C:\\Temp\\Test.pdf");
report.prompt();
report.run();
This code can be expanded on from here. Hopefully someone can get some use out of this, as we did. To give a better idea, we used this to generate EI's or Electronic Invoices. This was done so that One invoice would exists, instead of having to maintain an Axapta Invoice, and then a Sepearate one for the HTML emailed version that some customers got.
All in all it worked out pretty good, there are some differences though that you have to deal with. The PDF may come out slightly different looking (ie: Company Logo) than what it looks like from within AX.
Find a job at: www.DynamicsAXJobs.com
Below is code that will call the SalesInvoice and generate it as a PDF file:
CustInvoiceJour InvJTbl;
SalesId Id;
ReportRun report;
RecordSortedList List = new RecordSortedList(62);
Id = "SO-0000123";
Select InvJTbl Where InvJTbl.SalesId == Id;
List.ins(InvJTbl);
report = new ReportRun(new Args(ReportStr(SalesInvoice)));
report.args().caller(null);
report.args().parmEnum(1);
report.args().parmEnumType(920);
report.args().object(List);
report.args().name("KeepSettings");
report.printJobSettings().setTarget(PrintMedium::File);
report.printJobSettings().format(PrintFormat::PDF);
report.printJobSettings().fileName("C:\\Temp\\Test.pdf");
report.prompt();
report.run();
This code can be expanded on from here. Hopefully someone can get some use out of this, as we did. To give a better idea, we used this to generate EI's or Electronic Invoices. This was done so that One invoice would exists, instead of having to maintain an Axapta Invoice, and then a Sepearate one for the HTML emailed version that some customers got.
All in all it worked out pretty good, there are some differences though that you have to deal with. The PDF may come out slightly different looking (ie: Company Logo) than what it looks like from within AX.
Find a job at: www.DynamicsAXJobs.com
11 Comments:
Please help, I can't get the pdf-output file!
I copy/paste your code in a new job, add the declarations like:
custInvoiceJour InvJTbl;
SalesId salesId = "00014_036";
FileName filename = "c:\\temp\\output.pdf";
RecordSortedList List = new RecordSortedList(tableNum(CustInvoiceJour));
ReportRun report;
;
// your code comes here
What's wrong?
thanks
This is all fine and dandy, but if I'm not mistaken the output is created on the file system of the client. So the question is how do you get this to work in a three tier environment where the clients have no access to the file system of the server? My guess is that you want the files created on the server rather than on different clients throughout the organization.
Regards,
Tobias
home equity loans
Couldn't get this to work as is - had to pass the PrintJobSettings in a SalesFormLetter_Invoice class like this (NOTE: when in doubt, set a break point and step into every call - you'll see in the .init() where it throws away all your settings and switches to PrintMedium::Screen... grrrr):
(add to Declarations)
SalesFormLetter_Invoice SalesFormLetter = new SalesFormLetter_Invoice(false);
(modify the .caller())
report.args().caller(SalesFormLetter);
(add this before you call .run())
SalesFormLetter.updatePrinterSettingsFormLetter(report.packPrintJobSettings());
Hope this helps all you frustrated x++ guys/gals out there!
Great blog! Thank You ;)
Thanks for the code! It's very helpfull!
it's good to see this information in your post, i was looking the same but there was not any proper resource, thanx now i have the link which i was looking for my research.
http://axwonders.blogspot.com/2011/02/save-microsoft-dynamics-ax-2009-report.html
The code in this article did not work for me. The file was never saved into the location I was trying to save it to. However, the report was printed to the screen and from there I was able to save it manually.
This post helped me resolve the issue of saving the file as PDF into a local folder.
http://axwonders.blogspot.com/2011/02/save-microsoft-dynamics-ax-2009-report.html
I guess the main difference is that in the example shown in the link above is that it uses PrintJobSettings instead of the Report instances.
I hope this helps someone.
You dont need to create a PrintJobSettings object to pass through the arguments, you can just set the one in the reportRun object. But you cannot call the init AFTER you create those settings as the init resets the printJobSettings to default. Like this (after you've initialized your ReportRun object):
oReportRun.setTarget(PrintMedium::File);
oReportRun.init(); oReportRun.printJobSettings().setTarget(PrintMedium::File);
oReportRun.printJobSettings().format(PrintFormat::PDF);
oReportRun.printJobSettings().fileName(fileName);
oReportRun.run();
Hi,
I found a problem(Ax2009 SP 1 RU7) which lies in the salesFormLetterReport.loadPrintSettings method in the fetch method of the SalesInvoice report.
in the loadPrintSettings method there is a line if(!this.parmUseUserDefinedDestination)
If this is not set the printmanagement will revert back to Screen.
Best regards,
Morgan Sundqvist
Post a Comment
<< Home