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)
Answered

A very dumb question related to X++ conditional select where statement

(0) ShareShare
ReportReport
Posted on by

Hi guys,

I was wondering is there anyway Dynamics 365 (Current AX) could accept a code like below.

I have already tried it inside a runnable test class, but IDE gave me errors even before I could run it, due to syntax error.

Is there anyway to produce something like below, in a smart way instead of repeating the whole select statement just to find another record inside a table?

_21714C98_.jpg

*This post is locked for comments

I have the same question (0)
  • Community Member Profile Picture
    on at

    Where is your code you said  that produce "something like below"?

  • Community Member Profile Picture
    on at

    Sorry, I just pasted the screenshot directly from the paint programme, which I should upload it instead of copy and pasting it.

  • Community Member Profile Picture
    on at

    can you explain what are you trying to achieve? I believe you need to use  ||   (Or operator) in where condition to get customers you want to find from the table. like  below

    where custtable.accountnum == "cust1" || custtable.accountnum == "Cust2"

  • Community Member Profile Picture
    on at

    Thanks for the reply VDY.

    I am trying to find a particular record given a certain condition, inside a table.

    For example:

    CustTable custTable;
    boolean   a = false; //this boolean a might be changed to true
    
    //pseudo code
    select custTable
     where (if a is equal to true, then execute custTable.AccountNum == "DEMO-0001");
           (Or else, if a is equal to false, then execute custTable.AccountNum == "DEMO-0002")
           
    
           

    I hope you get an idea what I am trying to achieve with the above pseudo code.


      

  • Verified answer
    Chaitanya Golla Profile Picture
    17,225 on at

    Hi,

    Please use below code.

        CustTable custTable;
        boolean   a = false; //this boolean a might be changed to true
        Query query;
        QueryRun queryRun;
        Querybuilddatasource qbds;
        QuerybuildRange qbr;
    
        Query = new Query();
        qbds = query.addDatasource(tablenum(CustTable));
        qbr = SysQuery::findOrCreateRange(qbds, (fieldnum(CustTable, AccountNum)));
    
        if (a == true)
        {
            qbr.value(queryvalue("DEMO-0001"));
        }
        else
        {
            qbr.value(queryvalue("DEMO-0002"));
        }
     
        QueryRun = new QueryRun(query);
    
        while (queryRun.next())
        {
            custTable = queryRun.get(tablenum(CustTable));
            info(strfmt("AccountNum %1", custtable.AccountNum));
        } 
    
  • Community Member Profile Picture
    on at

    Thanks for the reply Chaitanya Golla

    I guess from your reply, it is impossible to make such a conditional statement with select statement.

    Query is the only way to make such a conditional statement huh...

    In any case, thank you so much for the reply!

    Best regards

    Lincoln Poh

  • Verified answer
    Community Member Profile Picture
    on at

    try this.

    select from custtable where  (a && custtable.accountnum == "cust1") || (!a && custtable.accountnum == "cust2")

  • Community Member Profile Picture
    on at

    Hi VDY

    Thanks for the reply again.

    WOW, so we can use select statement with conditional statement like that?!

    That was mind blowing...

    So, according to what you gave,

    select from custtable where  (a && custtable.accountnum == "cust1") || (!a && custtable.accountnum == "cust2")

    will result in checking if "a" boolean is true. Then if it's true, grab a value of "cust1" inside the field AccountNum of CustTable.

    If it does not exists, then next, check if boolean "a" is false. If it is, then grab a particular record where custtable.accountnum == "cust2".

    Mind blown!

    Thanks VDY!

    Best regards

    Lincoln Poh

  • Verified answer
    Rudi Hansen Profile Picture
    4,075 on at

    You could also simply do this.

        CustTable   custTable;
        AccountNum  accountNum;
        boolean     a = false; //this boolean a might be changed to true
        if(a)
        {
            accountNum = "DEMO-0001";
        }
        else
        {
            accountNum = "DEMO-0002";
        }
        
        while select custTable
            where custTable.AccountNum == accountNum
        {
            info(strfmt("AccountNum %1", custtable.AccountNum));
        }
    


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

    ... and in this particular case, you could simply call CustTable::find(a ? "DEMO-0001" : "DEMO-0002"). :-)

    But in more complicated cases, using the Query class usually makes the best sense. I sometimes see developers doing crazy things with select statements just because they don't know how to build queries dynamically.

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