Refreshing the Datasource of a Calling Form in X++

So you have form A. In form A there is a menu item that opens form B, and logic in form B will updated database records for a datasource in form A. Rather than trying to call a refresh inside of an event in form A, override the ‘close()’ or ‘closeOk()’ method in form B:

AX 2009:


public void close()
{
    FormRun callerForm;
    ;

    callerForm = element.args().caller();
    callerForm.dataSource().refresh();
    callerForm.dataSource().reread();
    callerForm.dataSource().research(true);

    super();
}

AX 2012 using a slightly different approach:


public void closeOk()
{
    #Task
    FormRun formRun;
    
    super();
    
    // Get an instance of the calling form.
    formRun = element.args().caller();
    
    // If the caller is a form, refresh that form.
    if(formRun)
    {
        formRun.task(#taskF5);
    }
}