How to get Form's caller in Dynamics AX?


Hello readers,

In Dynamics AX, when we need to find the parent of a form, we call the caller() method on the args object of the formRun object of the form. The syntax is,

FormRun callerForm = element.args().caller();

In the same way, we can find the parent of the caller form as well.
Something like,

FormRun callerForm = element.args().caller().args().caller();

Try that and Dynamics AX will throw an error that "The variable is not of the type CLASS."

We can call only upto the 3rd method after which the AX compiler fails to return an object.

We will have to first store the args() object in a seperate variable and then call the caller() method.

Let us look at an example of getting all the ancestors for a form.

I've a simple form, with just two methods - init() and parmFormName()

Declare a variable formName of type Name in the classDeclaration.

public Name parmFormName(Name _formName = formName)
{
    formName = _formName;

    return formName;
}
public void init()
{
    Object  caller;    
    Args    args;

    super();

    info(element.parmFormName());

    caller = element.args().caller();

    while (caller)
    {

        info(caller.parmFormName());
        args = caller.args();
        caller = args.caller();
    }
}
Thats the form. No extra design. Just a plain vanilla form.

Now I'll call this form from a job, but only after ensuring it has a great grand parent.
static void callForm(Args _args)
{
    Args        args;
    Object      formRun;
    int i;

    args = new Args(formStr(SampleForm));
    formRun = classFactory.formRunClass(args);
    if (formHasMethod(formRun, identifierStr(parmFormName)))
    {
        formRun.parmFormName("Form number 1");
    }

    for (i=2; i<=4; i++)
    {
        args = new Args(formStr(SampleForm));
        
		// Set the form's caller or parent
		args.caller(formRun);
        formRun = classFactory.formRunClass(args);
        
		// It is a best practice to first check if the method exists on the form before calling it.
		if (formHasMethod(formRun, identifierStr(parmFormName)))
        {
            // Sets the form's name variable
			formRun.parmFormName(strFmt("Form number %1",i));
        }
    }

    formRun.run();    
}
What I have done is first initialized a formRun object for our SampleForm. Next I'm running a loop 3 times to create three more instances of SampleForm and setting the form's parent to the previous instance. Then I run the form. Lets run the job now. Here is the output.
Dynamics AX infolog
The code in the SampleForm's init() method iterates through the callers until it reaches the first instance of the form. It calls the parmFormName() method on the form to print the form's name. The order is Form 4 is child of Form 3, Form 3 is child of Form 2 and Form 2 is the child of Form 1.
Dynamics AX caller form
When calling methods on forms or classes, using their generic form like formRun, always declare the variable as Object. This way you will not get a compilation error. Ofcourse, it is the developer's responsibility to ensure that there is no runtime error as well. For this purpose, use the intrinsic method formHasMethod() to make sure that the method indeed exists on the form. If you do not perform this check, and the method has been deleted or renamed, then you will recieve a runtime error. I hope this post was helpful. Please feel free to send me your questions, comments or any feedback using the Contact me form. You can download the project used from here.

Thats all for today folks, but do check back soon as we continue to learn more.

Popular posts from this blog

How to add empty ranges in query

The field with ID '0' does not exist in table - Cause and resolution.

Get selected records in Dynamics AX 2012