If your datasource on a form has relations to multiple tables with the same field, the form may choose the undesired table to reference.
You can do this either on the datasource field or the form control. Once you have overridden the method add code similar to the example below.
Args args;
FormRun formRun;
args = new Args(formstr(VendTable));
args.record(VendTable::find("1000629"));
formRun = classfactory.formRunClass(args);
formRun.init();
formRun.run();
formRun.wait();
formRun.detach();
Notice the record parameter, it is the actual table record you want to open.
Imagine a situation where you open a form B by clicking a button on form A. The clicked method of the button had the above code. And you want to perform some operation after the form B is closed. You could use the closedOk method of form B and call any method on form A like below.
if (formRun.closedOk())
MMSTmpVendPrincipalDistributor_ds.executeQuery();
Instead of formRun, a more secure and neater approach would be to use the MenuFunction like below. Its secure because it uses the menu item on which we can control access. copyCallerQuery() is optional. In some instances, when you have a grid of a different datasource than the main datasource with no relation to main datasource table, its records wont be populated if you don't use copyCallerQuery.
menuFunction = new MenuFunction(MenuItemDisplayStr(VendTable), MenuItemType::Display);
menuFunction.copyCallerQuery(CopyCallerQuery::Yes);
menuFunction.run(args);
AX also provides two classes, smmUtility and MenuFunction, which provide out of the box methods to achieve above functionality. Some examples picked up from standard AX.
smmUtility: openMenuItemForm and performJumpRef
smmUtility::openMenuItemForm(menuitemDisplayStr(DirPartyTable),vendTable,element,false);
smmUtility::peformJumpRef(tablenum(smmQuotationReasonGroup), smmQuotationReasonGroup::find(this.text()).RecId);
MenuFunction: runClient
public void jumpRef()
{
Args argsProductForm;
argsProductForm = new Args();
argsProductForm.record(EcoResProduct);
argsProductForm.caller(element);
argsProductForm.copyCallerQuery(CopyCallerQuery::No);
MenuFunction::runClient(menuitemDisplayStr(EcoResProductDetails),MenuItemType::Display,false,argsProductForm);
}