What is the parmLastActivatedForm method used for in Dynamics AX?

Hello readers,

In my earlier post, we had seen how to get all of a form's callers in Dynamics AX. One of my readers brought up a very good and interesting question around this.

Some action forms opened like the Document handling form will not show the caller. Is there a way to find the caller for these forms?

This is indeed correct. If you open the document handling form from any form, and look for the caller name, it will be empty. See the below image to understand what I mean.

Dynamics AX_no caller form
Well, this shouldn't stop us Dynamics AX developers from getting our way. In this post we will learn how to get the caller in such scenarios.

The answer lies in a method in the Info class - parmLastActivatedForm()

Basically, the parmLastActivatedForm() method returns the last form that was activated.

So let us see all this in action.

My code is in Dynamics AX 2012, but you can easily apply it in Dynamics AX 2009 as well.

Let us modify the init() method of the DocuView form. This is the document handling form.

    formRun                             callerForm;
    FormDataSource                      formDataSource;
    InventTable                         inventTable;
    
    callerForm = infolog.parmLastActivatedForm().object();
    
    info(strFmt("Caller form name is %1", callerForm.name()));
    
    formDataSource = callerForm.dataSource(1);
    if (formDataSource && formDataSource.cursor().TableId == tableNum(inventTable))
    {
        inventTable = formDataSource.cursor();
    }
    
    info(strFmt("Selected item on the list page is %1",inventTable.ItemId));

Now I'll open the document handling form from the Released Products list page after selecting a record where item name is Pack-Ribbon. This is the output.

As you can see from the image, we extracted not only the form's name but also the exact record which was selected.

How did I get the selected record with out using args().record()? Let us look closely at the changes we have made.
    formDataSource = callerForm.dataSource(1);
    if (formDataSource && formDataSource.cursor().TableId == tableNum(InventTable))
    {
        inventTable = formDataSource.cursor();
    }

The callerForm.dataSource(1) call will return me the first datasource on callerForm. I then check if a datasource was indeed returned and if it is for InventTable. If yes, I just cast it to an InventTable buffer and print it.

Now some of you may have a nagging question in your mind due to a statement I made.

Basically, the parmLastActivatedForm() method returns the last form that was activated.

If parmLastActivatedForm() method returns the last activated form, shouldn't it return DocuView as the name because that was the name of the last active form. Well? your doubt is absolutely correct, it should return that. But it doesnt because there is a check in code in the activate() method of Info class to stop that.
void activate(FormRun   formRun)
{
    if (formRun.name() != formstr(DocuView) &&
        formRun.name() != formstr(sysFormSearch)                
       )
    {
        this.setLastActivatedForm(formRun);
    }
}

As you can see from the above code, if the form name is DocuView or sysFormSearch, it is not stored. This is standard code. So this is the reason, whenever you open the DocuView form, a call to parmLastActivatedForm will return the name of the form that called the DocuView form.

In this post we learnt how to find caller and caller record for forms which are opened through the toolbar. I hope this post was useful and informative.

Thanks for all the response. Please continue to send me questions, feedback using the Contact me form. You can also leave me comments.

Thats all for today, but do check back again as we keep learning.

Comments

Petr Siroky psiroky@email.cz said…
Thank you so much for this post, it has saved me after desperate searching.
I have one comment. Standard Ax uses the docCursor() method, so you can write:

callerForm.docCursor()

instead of:

callerForm.datasource(1).cursor()

I think by using docCursor() you may be sure that this is the table on which document handling is really based.

Popular posts from this blog

How to add empty ranges in query

Get selected records in Dynamics AX 2012

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