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 :
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

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

    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?

  • Community Member Profile Picture
    on at

    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.

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

    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;
    }
  • Suggested answer
    Hossein.K Profile Picture
    6,648 on at

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


  • Hossein.K Profile Picture
    6,648 on at

    Hi Praveen,

    Your problem is solved?right?

  • Martin Dráb Profile Picture
    237,801 Most Valuable Professional on at

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

  • Suggested answer
    Osama Omer Profile Picture
    10 on at

    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
    237,801 Most Valuable Professional on at

    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.

  • Osama Omer Profile Picture
    10 on at

    Hi Martin,

    I have updated my answer.

    Thanks.

    Syed Osama Bin Omer.

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 > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans