Tuesday, November 10, 2015

CacheDataMethod (AX2012 feature) property vs CacheAddMethod

A someone little known feature of AX 2012 is a new property called "CacheDataMethod".

Previously, when you wanted to improve performance by caching your display/edit methods, you would place a line of code like this after the super() in the datasource's init() method:

this.cacheAddMethod(tableMethodStr(CustTable, MyDisplayMethod));

In AX 2012, you can just change the "CacheDataMethod" property on the object without adding code.  I prefer this method where possible mainly because it keeps everything packaged together and I don't have to intermingle my code with base code.

Monday, November 9, 2015

The better way to pass containers between objects/forms using the Args() class, and not by converting to a string

If you need to pass a container between objects/forms using the Args class, don't convert it to a string and then back, use parmObject() and ContainerClass()!  I see many suggestions about converting it to a string, which can have much more unpredictable results and is not as versatile.

You wrap your container in the class ContainerClass() and then unwrap it at the other end.

Make your call like this:
args.parmObject(new ContainerClass(["Real container", 1234, "Not con2str container"]));
And retrieve it like this:
containerClass = element.args().parmObject() as ContainerClass;
myContainer = containerClass.value();

To test this, create a form (Form1) and overwrite the init method and put in this code:

public void init()
    ContainerClass      containerClass;
    container           conValue;
    if (!(element.args() && element.args().parmObject() && element.args().parmObject() is ContainerClass))
        throw error("@SYS22539");
    containerClass = element.args().parmObject() as ContainerClass;
    conValue = containerClass.value();
    info(strFmt("The container contains '%1'", con2Str(conValue)));

Then create a Job and put in this code:

static void JobForm1(Args _args)
    Args        args;
    FormRun     formRun;
    args = new Args();;
    args.parmObject(new ContainerClass(['Real containers', 1234, 'Not con2str containers']));
    formRun = classFactory.formRunClass(args);

And then run the job!