In form PurchTable, on the lines, I need to show the external itemId used by the vendors. Thus the entry in table CustVendExternamItem.
I can add a data method and it would work, but data methods aren't searchable and the user needs to be able to filter/search on this new column.
To be able to accomplish this, I added a query which I added as a property to a new view and I use this view as a formDataSource (FDS) on PurchTable by extending the form.
I linked the view form datasource to PurchLine.
However I need the order account on the table PurchTable to make sure I pull the external number for the correct vendor.
OnQueryExecuting method on my new view FDS is not being called when I open a purchase order in form PurchTable. However OnInitialized is called. I added the following code to my new FDS's OnInitialized event handler:
[FormDataSourceEventHandler(formDataSourceStr(PurchTable, TMC_ProductVendorExternalNumberView), FormDataSourceEventType::Initialized)]
public void TMC_ProductVendorExternalNumberView_OnInitialized(FormDataSource sender, FormDataSourceEventArgs e)
{
QueryBuildDataSource qbds;
FormDataSource fdsPurch = sender.formRun().dataSource(formDataSourceStr(PurchTable, PurchTable));
qbds = sender.queryBuildDataSource();
qbds.clearLinks();
qbds.fetchMode(QueryFetchMode::One2One);
qbds.addLink(fieldNum(PurchLine, ItemId), fieldNum(TMC_ProductVendorExternalNumberView,ItemId), PurchLine_ds.name());
qbds.addLink(fieldNum(PurchTable, OrderAccount), fieldNum(TMC_ProductVendorExternalNumberView,CustVendRelation),fdsPurch.name());
SysQuery::findOrCreateRange(qbds,fieldNum(TMC_ProductVendorExternalNumberView, ModuleType)).value(strFmt('%1', enum2Str(ModuleInventPurchSalesVendCustGroup::Vend)));
}
It doesn't give me an error at compile time, but when I run it and it calls this handler, I get the following error the moment it tries to add the link to PurchTable:
$exception {"The specified data source cannot be found."} Microsoft.Dynamics.Ax.Xpp.ErrorException
I can use PurchTable_ds or like above assigning it to a formdatasource variable, it doesn't matter. I still get the error.
If I comment the OnInitialized event handler out and add the code to OnQueryExecuting and add a call to to PurchTable's FDS OnInitialized eventhandler to call the executeQuery, I still get the same error when it tries to add the link to PurchTable:
[FormDataSourceEventHandler(formDataSourceStr(PurchTable, PurchTable), FormDataSourceEventType::Activated)]
public static void PurchTable_OnActivated(FormDataSource sender, FormDataSourceEventArgs e)
{
FormDataSource fdsView = sender.formRun().dataSource(formDataSourceStr(PurchTable, TMC_ProductVendorExternalNumberView));
fdsView.executeQuery();
}

I also have tried to add the link to PurchTable as follow:
***
qbr = qbds.addRange(fieldNum(TMC_ProductVendorExternalNumberView,CustVendRelation));
qbr.value(strFmt("(%1.CustVendRelation == %2.%3)",
qbds.name(), qbdsP.name(), fieldStr(PurchTable, OrderAccount)));
***
This will create the query correctly but then I get the error that PurchTable.OrderAccount is an invalid Datasource.Field pair.
So any guidance on how I can add PurchTable to the query of the view's FDS is much appreciated.
So in short:
The parent of view TMC_ProductVendorExternalNumberView is PurchLine.
The parent of PurchLine is PurchTable.
I need to link to both for the view to retrieve the correct external itemId used by the specific vendor on the purchase order for the specific inventTable itemId on the purchase line.And I need it to be one2one so that it doesn't repeat the purchase lines.