Friday, September 25, 2009

How to run a report by code

After last post, how to open a form by code, we gonna do the same thing for reports.
First the short version

new MenuFunction(MenuItemOutPutStr(Cust),MenuItemType::Output).run();

The above code will start the Cust report (a list with the basic information for a selection of customers).
If you want some information passed to your report, you can use the args class.
Like this

static void RunReportByCodeA()
{ Args args = new Args();
;
args.parm('yourparmhere');
new MenuFunction(MenuItemOutPutStr(Cust),MenuItemType::Output).run(args);
}


Just like in the previous post, even more control over your report may be required. Then you can use the long version.

static void RunReportByCodeB()
{ ReportRun reportRun;
Args args = new Args(reportstr('Cust'));
;

// args.caller(this);
// args.record(parmRecord);
// args.parm(parmStr);

reportRun = new Reportrun(args);
reportRun.init();
reportRun.run();
}


The above version is exactly like the short version we saw earlier. But it shows where we are going with this.

static void RunReportByCodeB()
{ Object reportRun;
Args args = new Args(reportstr('Cust'));
;
reportRun = new Reportrun(args);
reportRun.init();
reportRun.yourmethodgoeshere();
reportRun.run();
}

We changed the type of reportRun to Object.
Now we have even more control over the report, as we can execute any method defined on it. Use it to provide extra information to the report (use a specific design depending on data, print straight to printer instead of screen, ...).

Again, there is one drawback: While programming, your method doesn't show up in the IntelliSense, showing all the available methods. So be carefull of typo's. (They don't give a compile error, but they will give you a run-time error.)

4 comments:

  1. Thanks, new to AX (moved from NAV) so this kind info is handy!

    ReplyDelete
  2. Hi Hans,

    Welcome to the Ax community!

    ReplyDelete
  3. Hello Willy,
    can you point me in the right direction? I want to open a look up from code and pass the pass the selected value back to my report.

    Thanks Bill

    ReplyDelete
  4. I am interested in the lookup code too, thank you.

    ReplyDelete