30 August 2016

How to update a caller form when a new record is created in a separate form in AX 2012

Today I want to share a code sample in standard AX application to update a caller form.

Problem description
A form (caller) has a menu item to open a separate form (dialog) to create a new record. After the new record is created and the dialog form is closed, the caller form is updated and the new record is made the current one. What are the ways to achieve it?

Hints
Analyse \Forms\EcoResProductCreate\Methods\updateCallers method.

Solution
The most common solution is to call 2 methods in the dialog form on caller form's datasource
callerFormRun.dataSource().research(); callerFormRun.dataSource().findRecord(newRecord);

Sometimes it does not work. I can not reproduce the problem because the environment is not available anymore, but the solution was the same as in \Forms\EcoResProductCreate\Methods\updateCallers method.

args.lookupRecord() method is mainly used for View details functionality or in jumpRef() methods. So it was slightly unusual to apply it for caller update:
callerFormRun.args().lookupRecord(recordToFind); callerFormRun.dataSource().research();

In general, the \Forms\EcoResProductCreate\Methods\updateCallers method is a very good example of how to update a caller form.
public void updateCallers()
{
    FormRun         callerFormRun;
    Common          recordToFind;
    Args            args = this.args();
    Args            callerFormArgs;

    //set lookup record for caller's args.
    //Lookup record set here will result in refreshing the caller and 
    //positioning on the newly created record
    if (args && args.caller() is FormRun)
    {
        callerFormRun = args.caller() as FormRun;
        if (callerFormRun.closed())
        {
            return;
        }
        //in the Products per Company list page 
        //the InventTable table is the master data source
        //so we need an InventTable record in order to position correctly
        if (callerFormRun.dataSource().cursor() is InventTable)
        {
            recordToFind = InventTable::findByProduct(ecoResProduct.RecId);
        }
        else
        {
            //standalone instance of EcoResProduct table 
            //has to be passed to lookupRecord
            recordToFind = EcoResProduct::find(ecoResProduct.RecId);
        }
        callerFormArgs = callerFormRun.args();
        if (callerFormArgs)
        {
            callerFormRun.args().lookupRecord(recordToFind);
            callerFormRun.dataSource().research();
        }
    }
}

No comments:

Post a Comment