Thursday, 17 November 2011

Running report via code in Ax2012

This is a quick example of running an Ax2012 report via code - In this example we're outputting the Vendors report (Accounts Payable / Reports / Vendors / Vendors) directly to a PDF file.

static void RunSSRSReport(Args _args)
    SrsReportRunController  reportRunController;
    Map                     queryContracts;
    MapEnumerator           mapEnum;
    Query                   query;
    QueryBuildRange         range;
    // Create the report run controller
    reportRunController = new SrsReportRunController();
    // NB call to parmLoadFromSysLastValue must occur before any reference to 
    // parmReportContract, otherwise the previous values (query ranges etc)
    // are defaulted in.
    // Set printer settings (print to file, format, filename, etc).
    // Find/enumerate queries in the contract. The return from parmQueryContracts is
    // a map of type <ParameterName,Query(class)>    
    queryContracts = reportRunController.parmReportContract().parmQueryContracts();
    mapEnum = queryContracts.getEnumerator();    
        // Get the query and update the datasource as required
        query = mapEnum.currentValue();   
        range = SysQuery::findOrCreateRange(query.dataSourceTable(tableNum(VendTable)),fieldNum(VendTable,AccountNum));
    // Run the report

Note that this displays a message to the inflog once the file has been written. This may not be desirable if the file generation is part of a background process - If you need more control of this, have a look at adding an additional flag (eg supressInfoLog) to the class SrsReportRunPrinter.

1 comment:

  1. Hi ,
    My name is Venkatesh, working in Ax.

    Found your posts about Surrogate key useful, But still have an issue with one of my forms in Ax.

    If the FormControl is a reference Group refering a table1's surrogate and the ReplacementFieldGroup is also containing as RecID (surrogate) for another Table say table2 what kind of lookup should i expect.
    Checked for all the field groups but Nothing works and to complicate things further i have table inheritance indirectly inheriting DirPartyTable

    please mail me in this regard.