Skip to main content

Notifications

Finance | Project Operations, Human Resources, ...
Suggested answer

Query fails and returns every record in tables

Posted on by 10

A customer could suddenly not open the form Settle Open Transactions (VendOpenTrans) and the system would lock up. I looked at the query for the main datasource and when I rebuilt it in a job I got the same result.

The query with parts commented out will work and return records for the Party ID 5637155687 - VendTransOpen joined with VendTrans joined with VendTable:

qvto = query.addDataSource(tableNum(VendTransOpen));

qvtr = qvto.addDataSource(tableNum(VendTrans));
qvtr.addLink(fieldNum(VendTransOpen, AccountNum), fieldNum(VendTrans, AccountNum));
qvtr.addLink(fieldNum(VendTransOpen, RefRecId), fieldNum(VendTrans, RecId));
qvtr.addRange(fieldNum(VendTrans, Approved)).value(queryValue(1));

//qhw = qvtr.addDataSource(tableNum(HcmWorker));

//qhw.addLink(fieldNum(VendTrans, Approver), fieldNum(HcmWorker, RecId));

//qhw.joinMode(JoinMode::OuterJoin);
//qll = qvtr.addDataSource(tableNum(LogisticsLocation));
//qll.joinMode(JoinMode::OuterJoin);
//qll.addLink(fieldNum(VendTrans, RemittanceLocation), fieldNum(LogisticsLocation, RecId));
qvt1 = qvtr.addDataSource(tableNum(VendTable));

qvt1.addLink(fieldNum(VendTrans, AccountNum), fieldNum(VendTable, AccountNum));
qvt1.addRange(fieldNum(VendTable, Party)).value(queryValue(5637155687));
//qvt = qvto.addDataSource(tableNum(VendTable));
//qvt.addLink(fieldNum(VendTransOpen, AccountNum), fieldNum(VendTable, AccountNum));
//qdim = qvto.addDataSource(tableNum(DimensionAttributeValueCombination));
//qdim.joinMode(JoinMode::OuterJoin);
//qdim.addLink(fieldNum(VendTransOpen, CashDiscountLedgerDimension), fieldNum(DimensionAttributeValueCombination, RecId));

If I add ANY of the other datasources I will get every record with every join added - too much data to handle...

In debug I see VendTransOpen_q query forming as expected and as soon as one of the others are added it only shows "Query object" with no info in debugger. Any ideas on the source of this issue?

  • Suggested answer
    nmaenpaa Profile Picture
    nmaenpaa 101,156 on at
    RE: Query fails and returns every record in tables

    As Sergei pointed out, your Query object is not the same query as "select VendTransOpen join...". Otherwise you would get the same results. You can check the actual query for both scenarios in SQL Server Activity Monitor.

  • nmkloster Profile Picture
    nmkloster 10 on at
    RE: Query fails and returns every record in tables

    The same query written out as "select VendTransOpen join..." works fine. Index' should be rebuilt regularly but someone else is looking into that and will be trying to monitor SQL server and call the form.

  • Suggested answer
    Sergei Minozhenko Profile Picture
    Sergei Minozhenko 23,089 on at
    RE: Query fails and returns every record in tables

    Hi nmkloster,

    I guess your query is not designed correctly, as you join quite a lot of data sources to the same data source and the query doesn't work well if you don't fetch mode or don't use the parent data source parameter in addLink method. If you want to reproduce the issue, just write it in x++ with built-in SQL syntax.

    If something suddenly stopped working, it could be new customization deployed recently to the environment or something wrong with SQL server. Do you perform any kind of DB maintenance?

    I recommend you to open SQL server management studio and connected to SQL DB used for the environment, and start SQL server activity monitor. Then try to open the form again and check the query you catch from Current Expensive Queries. Most likely you need to rebuild indexes for the tables above or maybe there is a missing index.

    www.sqlshack.com/.../

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans