Reports to Email from X++

Many of us may be in need to share Reports directly to Email.

Dynamics 365 for Operations uses SSRS to execute reports (same as AX 2012). Bellow is a runnable class example that will help you implement what you need.

For the Example below consider you have a Customer report that uses a dataset based on CustTable:

class ReportToEmailExampleRunnableClass  
{        

    public static void main(Args _args)
    {        

        Map                             queryContracts;
        MapEnumerator                   meQueryContracts;
        Query                           currentQuery;
        QueryBuildDataSource            dataSource;
        SrsReportDataContract           contract;

        SrsReportRunController controller = new SrsReportRunController();
        SRSPrintDestinationSettings printSettings;
        SrsReportEMailDataContract emailContract;

        // set report name
        controller.parmReportName(ssrsReportStr(TestCustomReport, PrecisionDesign1));

        // create email contract
        emailContract = new SrsReportEMailDataContract();

        // fill in the email contract details
        emailContract.parmAttachmentFileFormat(SRSReportFileFormat::PDF);
        emailContract.parmSubject("Test Report");
        emailContract.parmTo("someone@email.com");



        // get print settings from contract
        printSettings = controller.parmReportContract().parmPrintSettings();

        // update print settings with email contract and use pdf format in the attachment
        printSettings.printMediumType(SRSPrintMediumType::Email);
        printSettings.parmEMailContract(emailContract);
        printSettings.fileFormat(SRSReportFileFormat::PDF);



        // suppress the parameter dialog
        controller.parmShowDialog(false);

        // Set any required parameter values here by first getting the report contract.
        contract = controller.parmReportContract();
        // Retrieve the query map and set dynamic parameter values
        queryContracts = contract.parmQueryContracts();
        if(queryContracts)
        {
            meQueryContracts = queryContracts.getEnumerator();
            while(meQueryContracts.moveNext())
            {
                currentQuery = meQueryContracts.currentValue();
         // Set the datasource for query parameter
                dataSource = currentQuery.dataSourceName("CustTable");
                if(dataSource)
                {
                    SysQuery::findOrCreateRange(dataSource,fieldNum(CustTable, AccountNum))
                         .value("yourcustomeracountnum");
                }
                else
                {

               }
            }
        }



        // start operation
        controller.startOperation();

    }

}

Articles used for reference:

AX 2012 - How To: Directing Reports to Email

Dynamics community - SRSReportServerWarmup - Setting dynamic parameter value(s)