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

Announcements

News and Announcements icon
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
    239,747 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
    239,747 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,650 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,650 on at

    Hi Praveen,

    Your problem is solved?right?

  • Martin Dráb Profile Picture
    239,747 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
    239,747 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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
CP04-islander Profile Picture

CP04-islander 16

#2
Nagendra Varma K Profile Picture

Nagendra Varma K 4

#2
Harisgillani Profile Picture

Harisgillani 4

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans