web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

'The specified data source cannot be found' when using AddLink

(0) ShareShare
ReportReport
Posted on by 1,457

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();
}

pastedimage1623883059609v1.png

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.

I have the same question (0)
  • Suggested answer
    Martin Dráb Profile Picture
    237,878 Most Valuable Professional on at

    Which line of code is throwing the error?

    I think I see the bug. You're trying to add a join condition for PurchTable data source, but PurchTable isn't joined with PurchLine, therefore such a data source indeed doesn't exist in the query you're modifying.

    By the way, please use Insert > Insert Code (in the rich-formatting view) to paste source code. It preserves line indentation, which makes code much easier to read. For example:

    [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)));
    }

  • Retha Profile Picture
    1,457 on at

    Martin, PurchTable is the main form datasource of the PurchTable form. Of cause PurchTable is joined with PurchLine.

    The query of the formdatasource Sender (Query qr = sender.query();) on my OnInitialized event-handler even starts with the following:

    ********

    {Query OBJECT 2649a9d0:

    SELECT FIRSTFAST *

    FROM PurchLine(PurchLine) USING INDEX PurchLineIdx

    WHERE ((IsDeleted = 0))

     AND PurchTable.PurchId=PurchLine.PurchId .........

    **********

    pastedimage1623943166452v1.png

    If it is because it stated that the linkType is delayed, then it should have worked when I added a call to the OnExecuting event handler on PurchTable's OnActivated. That is what I think anyways.

    If you look at the standard code for the query that is build for table InventDistinctProductExpanded in the same form: PurchTable, you will see the code in the end links it to both InventDim and PurchLine.

    It has the same hierarchy in that InventDim is the "parent" of InventDistinctProductExpanded and Purchline is the "parent" of InventDim. The only difference is that the link types are outerJoin to InventDim and innerJoin to PurchLine. While in my case purchLine to PurchTable is linktype "Delayed".

    It doesn't make sense that I need to add  a 2nd buffer for PurchTable in my OnInitialized or OnQueryExecuting just to be able to reference the OrderAccount field while the formDataSource is there, it's the main datasource for the form. Or does that link not exist in the extension-code, only in the standard code?

    I even removed the "static" of the event handler and added the event handler to an extension class so that it can see all the form's variables and datasources.

    ****

    [ExtensionOf(formStr(PurchTable))]
    final class TMC_PurchTableForm_Extension

    ****

    So it should in my opinion see the main datasource of the form.

  • Verified answer
    Martin Dráb Profile Picture
    237,878 Most Valuable Professional on at

    No, PurchTable isn't joined to PurchLine, even if you say that it 'of course' is. They're linked via a Delayed link (not a join), therefore they form two separate queries. When you select a record in PurchTable, a query for PurchLine gets executed, filtered by PurchId of the selected PurchTable.

    Regrarding InventDistinctProductExpanded, you seem to be showing that a data source can have join conditions to two other data sources, but nobody disputes that. What I'm saying that it works if you're doing it for joined data sources, but it'll fail if you're trying to use a data source which isn't in the query at all. That's your current problem.

    I see two ways how you can implement your requirement: using a dynamic link (addDynalink()) or a query range.

  • Retha Profile Picture
    1,457 on at

    Now that is something to remember for future use regarding the difference between links and joins.

    That is why it's working for  InventDistinctProductExpanded then because it has joins to InventDim and PurchLine and not a delayed links.

    Thanks Martin

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 646 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 529 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 285 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans