Save SSRS report to pdf that uses Controller classes [Dynamics AX 2012]

Friends,

This is the most commonly asked requirement of saving the report to pdf and send it to through email.

We all know that SRSReportRun class is helpful in combination with printDestinationsettings to save to pdf file.

But tricky requirements are those which uses controller classes for the report. Below is the small snippet which will help to save the sales invoice report to pdf that actually uses controller classes.

 

static void SR_SaveReportToPDFFromController(Args _args)

{

    SalesInvoiceController  salesInvoiceController;

    SalesInvoiceContract    salesInvoiceContract;

    Args                    args = new Args();

    SrsReportRunImpl        srsReportRun;

    CustInvoiceJour         custInvoiceJour;

    ReportName              reportName = "SalesInvoice.Report";

    ;

 

    select firstOnly custInvoiceJour;

    args.record(custInvoiceJour);

   

    salesInvoiceController = new SalesInvoiceController();

    salesInvoiceController.parmReportName(reportName);

   

    salesInvoiceContract = salesInvoiceController.parmReportContract().parmRdpContract();

    salesInvoiceContract.parmRecordId(custInvoiceJour.RecId); // Record id must be passed otherwise the report will be empty

    salesInvoiceContract.parmCountryRegionISOCode(SysCountryRegionCode::countryInfo()); // comment this code if tested in pre release

    salesInvoiceController.parmArgs(args);

 

    srsReportRun = salesInvoiceController.parmReportRun() as SrsReportRunImpl;

   

    salesInvoiceController.parmReportRun(srsReportRun);

    salesInvoiceController.parmReportContract().parmPrintSettings().printMediumType(SRSPrintMediumType::File);

    salesInvoiceController.parmReportContract().parmPrintSettings().overwriteFile(true);

    salesInvoiceController.parmReportContract().parmPrintSettings().fileFormat(SRSReportFileFormat::PDF);

    salesInvoiceController.parmReportContract().parmPrintSettings().fileName(‘c:\\SR_SalesInvoice.pdf’);

    salesInvoiceController.runReport();

}

Below is the pdf file output:

image

Happy Dax6ng,

image

Advertisements

25 Responses to “Save SSRS report to pdf that uses Controller classes [Dynamics AX 2012]”

  1. dawson Says:

    Hi,
    thanks a lot for posting this article. I have been stuck on sending invoice to customers via email from ages. just need some more help from you as I am new to AX. Where should I put this code. what steps should I follow…

    any help would be highly appreciated

  2. Shyam Kesi Says:

    very good information, sreenath !! thank you ūüôā

    for purchpurchaseorder also has control classes “PurchPurchaseOrderController” and “PurchPurchaseOrderContract”.

    Regards
    Shyam Kesi

  3. Michael Brown Says:

    Hi Sreenath,

    Thanks for the great article.

    I have an issue though. When I tried the same thing with Sales Order Confirmation, I was getting an error. Below is the error log of SSRS. Can you help me identify what am I missing or whether it the correct approach at all? All I want to do is, whenever someone confirms an SO, we want to automatically email a PDF copy to Customer.

    Microsoft.ReportingServices.ReportProcessing.ReportProcessingException: Query execution failed for dataset ‘SalesConfirmHeaderDS’. —> System.Security.SecurityException: Permission denied setting the framework parameter AX_UserContext. This parameter must be null.

    Best Regards,
    Michael Brown

  4. Stefan Says:

    Hi

    is there any possibility to save an Report as pdf without using the Controller classes? I want an class can print every Report to pdf (for an archive System)
    lg
    Stefan

  5. SSK Says:

    Hi Sreenath,
    Very nice article and hlepful.
    Would you please tell how to change language of report at runtime accoriding to customer language Id?

  6. MikS Says:

    Hi,

    Why you use the variable args?

    salesInvoiceController.parmArgs(args);

    I’m asking this because I think that in this case the variable args is not used, because we pass the parameters directly at the contract class, isn’t it?

    Furthermore, I don’t understant the following:

    srsReportRun = salesInvoiceController.parmReportRun() as SrsReportRunImpl;
    salesInvoiceController.parmReportRun(srsReportRun);

    Why you do this?

    Thanks a lot and great article!

  7. document Says:

    Hey! Would you mind if I share your blog with my myspace group?

    There’s a lot of folks that I think would really enjoy your content. Please let me know. Thank you

  8. Eugene Says:

    Hi Sreenath,

    Thank you very much for that post, it is really helpfull for me. I followed the recommendations and get it working, but only for en-us language. could you help me to set the language properly according the customers preferrences? Thank you in advance,

  9. Ajay Says:

    Hi Sreenath,

    It is a very helpful for me too in a similar requirement on converting the Purchase order report into PDF can you please help me with telling where to put this code ..

    • Ajay Says:

      sorry i mentioned a wrong email address and specifying a write one in this comment.. please reply Sreenath..

      Thanks…

  10. Pranav Gupta Says:

    Hello Mr. Shreenath,
    Thank you for posting the Code here, I am getting an error while running the code in a job in Ax2012, the error is as follows:- “An attempt was made to set a report parameter ‘SalesInvoiceDS_FormLetterRecordId’ that is not defined in this report.”, what i have to do is to make the PDF of a Standard Invoice Report and then attach it to the outlook using X++, in Ax2012…so could you help me regarding this.

    thanks
    Pranav

  11. Save SSRS report as PDF when printing from AX2012 « coderscove Says:

    […] order to do so I used the info from this blog post¬†to modify the¬†¬†outputReport()¬†method of the¬†SalesInvoiceController¬†¬†class so the report is […]

  12. Save SSRS Reports as a PDF from AX2012 | AKA Enterprise Solutions Says:

    […] order to do so I used the info from¬†this blog post¬†to modify the¬†¬†outputReport()¬†method of¬†the¬†SalesInvoiceController¬†¬†class so the report is […]

  13. Nisha Says:

    Hi ,
    I am getting an error while following the above steps for PSAProjInvoice.Report.Error is “Parameter executionInfo cannot be null or empty.” .Can you please help us on this?

  14. loriryaneire Says:

    Is there anyway to suppress the report preview?

  15. Kashif Kazmi Says:

    HI Sreenath, I have used the same approach to print Sales order confirmation, using SalesConfirmController & SalesConfirmJour but the resulting report is empty. Can u direct me what Im missing.

  16. Hima Bindu Says:

    Hi Sreekant,

    Can you please Tell me how to Convert The SSRS Report to PDF with out using Purchpurchasecontroller while clicking the Confirmation button in Purchase Order.

    Please Give Response ASAP,Its very urgent.

    Advance Thanks,

    Hima Bindu

  17. Ganna Tsykhan Says:

    Hi,
    Thank you! Your article helped me!

    But I have the same problem like Michatl Brown.
    Error: Permission denied setting the framework parameter AX_UserContext. This parameter must be null.
    Can you help me?

    Best Regards,
    Ganna Tsyhan.

  18. Hakan Fatih YILDIRIM Says:

    This code piece is not woking in AX 2012 R2 version. I gives an error “Parameter executionInfo cannot be null or empty.”.Does anyone face this issue as well? Please help.

    • Jhansi Says:

      Hi,
      The post was very helpful. Thanks for the detailed explanation. But the same piece of code gives an error in R2 version. It needs destination file name to copy our custom file.
      The below two statements helped me solve the error. But I am not sure if this is the correct way to solve this issue.

      printMgmtExecutionInfo = this.parmReportContract().parmReportExecutionInfo() as SrsPrintMgmtExecutionInfo;
      printMgmtExecutionInfo.parmOriginalDestinationFileName(@newfilename);

      Thanks,
      Jhansi


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: