Wednesday, May 10, 2017

Run SSRS report using code in AX 2012

The following code lets you run reports using x++

       SrsReportRunController          controller  = new SrsReportRunController();
    WHSWorkInquiryPurchContract     contract    = new  ReportContract();
    SRSPrintDestinationSettings     settings;
    SalesParameters                 salesParameters =  SalesParameters::find();

    // Define report and report design to use
    controller.parmReportName(ssrsReportStr(SalesInvoice, Report));

    // Use execution mode appropriate to your situation
    controller.parmExecutionMode(SysOperationExecutionMode::Synchronous);

    // Suppress report dialog
    controller.parmShowDialog(false);

    // Explicitly provide all required parameters
    contract.parmPurchId(_purchId);
    controller.parmReportContract().parmRdpContract(contract);

    // Change print settings as needed
    settings = controller.parmReportContract().parmPrintSettings();


   if(salesParameters.PrintToScreen) // if print to screen
    {
        settings.printMediumType(SRSPrintMediumType::Screen);

        controller.startOperation();
    }
    else  // else print to printer using a printer name from parameters
    {
        if(salesParameters.POPrinterName)
        {
            settings.printerName(salesParameters.POPrinterName);
            settings.printMediumType(SRSPrintMediumType::Printer);

            controller.startOperation();
        }
   }