Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

How to find if a datasource exists in an x++ query

(0) ShareShare
ReportReport
Posted on by

Hi Friends,

I have an x++ query which comes from some form at run time.

How to find if that query has a particular table as datasource. 

Query qr;

qr = new Query(_parmquery);

// I have issue here, the below line always skips as if datasource is not there event when its there

if(qr.dataSourceTable(tableNum(CustTable).existsMeanOrExists()) 

then add some range

else

something

query.datasource().? what to write here.

I tried enabled and it works only if that tables exists and will through a stack trace error if its not available

query.datasource().enabled()

Please help.

Thanks,

Praveen

*This post is locked for comments

  • Osama Omer Profile Picture
    10 on at
    RE: How to find if a datasource exists in an x++ query

    Hi Martin,

    I have updated my answer.

    Thanks.

    Syed Osama Bin Omer.

  • Martin Dráb Profile Picture
    234,100 Most Valuable Professional on at
    RE: How to find if a datasource exists in an x++ query

    QueryRun.get() is used to fetch data after executing the query (and making a request to database).

    Also, if you don't get anything, it doesn't necessarily mean that the table isn't used in the query. It might simply happen that there isn't any record meeting conditions specified in the query.

  • Suggested answer
    Osama Omer Profile Picture
    10 on at
    RE: How to find if a datasource exists in an x++ query

    Hi, 

    You can try something like this:

     
    if (qr.dataSourceTable(tableNum(CustTable)))
    {
        // CustTable datasource is found, do something.
        // e.g add some range
    }
    
    if (qr.dataSourceTable(tableNum(VendTable)))
    {
        // VendTable datasource is found, do something.
    }




    Thanks.

    Syed Osama Bin Omer.

  • Martin Dráb Profile Picture
    234,100 Most Valuable Professional on at
    RE: How to find if a datasource exists in an x++ query

    countLoops() counts how many records a query returns; it's not going to help with finding whether a query uses a particular table.

  • Hossein.K Profile Picture
    6,648 on at
    RE: How to find if a datasource exists in an x++ query

    Hi Praveen,

    Your problem is solved?right?

  • Suggested answer
    Hossein.K Profile Picture
    6,648 on at
    RE: How to find if a datasource exists in an x++ query

    Hi Praveen,

    You can below code:

    boolean dsExist()
    {
        Query                query;    
        QueryRun             queryRun;  
        QueryBuildDatasource datasource;
        boolean              result;   
        ;
    
        result = false;
    
        query = new Query();
        datasource = query.addDataSource(tableNum(CustTable));
          
        queryRun = new QueryRun(query);   
        if(SysQuery::countLoops(queryRun) > 0);
        {
            return true;
        } 
    }


  • Verified answer
    Martin Dráb Profile Picture
    234,100 Most Valuable Professional on at
    RE: How to find if a datasource exists in an x++ query

    You can use something like this:

    QueryBuildDataSource findFirstDsForTable(Query _q, TableId _tableId)
    {
        QueryBuildDataSource ds;
        int i;
    
        for (i = 1; i <= _q.dataSourceCount(); i++)
        {
            ds = _q.dataSourceNo(i);
            if (ds.table() == _tableId)
            {
                return ds;
            }
        }
    
        return null;
    }
  • Community Member Profile Picture
    on at
    RE: How to find if a datasource exists in an x++ query

    Thanks for replying Martin.

    I did not think through all the points you mentioned.  

    In my requirement I'm not concerned about the tables position whether it is at the root level or at child level or the number of times it occurs. But if you can provide me the solution for simply finding the table from the query using query classed, that would meet my need. If you can give a brief solution on how to get it from root level or child level and number of occurrences, that will be awesome.

  • Martin Dráb Profile Picture
    234,100 Most Valuable Professional on at
    RE: How to find if a datasource exists in an x++ query

    I don't think your code can even compile and I don't see what useful it would do anyway, so let's rather forgot code for a sec and focus on your requirements.

    As I understand, you want to find a datasource for a particular table inside a query. Do you know at which level it is? Most importantly, do you want to look only at the root level, or also at joined datasources?

    Also, what if the table is used by more than one datasource? Are you fine with simply taking the first one?

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

Jainam Kothari – Community Spotlight

We are honored to recognize Jainam Kothari as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 100 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 50

#3
shanawaz davood basha Profile Picture

shanawaz davood basha 6

Featured topics

Product updates

Dynamics 365 release plans