简体   繁体   中英

How to use AND/OR operators when building query in ax 2012 x++

I couldn't really find anything about this, and i couldn't really figure it out. Anyways, I have created a view which i need to filter using query/QueryRun etc. in x++.

The select statement for what i am trying to do looks like this

  while select salestable order by PtsWebDeliveryDate, salesId
    where 
(SalesTable.SalesStatus == SalesStatus::Delivered && !SalesTable.PtsProdNorwood && SalesTable.CustAccount != acc && SalesTable.InvoiceAccount != acc &&
           salestable.PtsWebDeliveryDate >= today() && salestable.PtsWebDeliveryDate <= today()+daysahead)
           ||
          (
            SalesTable.SalesStatus == SalesStatus::Backorder && SalesTable.SalesType == SalesType::Sales && !SalesTable.PtsProdNorwood &&
             SalesTable.CustAccount != acc && SalesTable.InvoiceAccount != acc &&
          (
                (salesTable.PtsSalesorderPacked  && salestable.PtsWebDeliveryDate >= today() && salestable.PtsWebDeliveryDate <= today()+daysAhead)
            ||
                (!salesTable.PtsSalesorderPacked && salestable.PtsWebDeliveryDate >= d && salestable.PtsWebDeliveryDate <= today()+daysahead))
          )
{
//Do stuff
}

As you can see i have som OR operators which i need to use. i have startet building the query in x++ and this is what i got:

q = new Query();
q.addDataSource(TableNum("packlistview"));
q.dataSourceNo(1).addSortField(fn[_OrderBy], _direction);

q.dataSourceNo(1).addRange(fieldNum(PackListView, SalesStatus)).value(queryValue(SalesStatus::Delivered));
q.dataSourceNo(1).addRange(fieldNum(PackListView, PtsProdnorwood)).value(queryValue(NoYes::No));
q.dataSourceNo(1).addRange(fieldNum(PackListView, CustAccount)).value(queryValue(!acc));
q.dataSourceNo(1).addRange(fieldNum(PackListView, PtsWebDeliveryDate)).value(queryrange(today(),today()+daysahead));

//OR
q.dataSourceNo(1).addRange(fieldNum(PackListView, SalesStatus)).value(queryValue(SalesStatus::Backorder));
q.dataSourceNo(1).addRange(fieldNum(PackListView, SalesType)).value(queryValue(SalesType::Sales));
q.dataSourceNo(1).addRange(fieldNum(PackListView, PtsProdnorwood)).value(queryValue(false));
q.dataSourceNo(1).addRange(fieldNum(PackListView, CustAccount)).value(queryValue(!acc));
q.dataSourceNo(1).addRange(fieldNum(PackListView, InvoiceAccount)).value(queryValue(!acc));
q.dataSourceNo(1).addRange(fieldNum(PackListView, PtsSalesorderPacked)).value(queryValue(false));
q.dataSourceNo(1).addRange(fieldNum(PackListView, PtsWebDeliveryDate)).value(queryrange(d, today()+daysahead));

//OR
q.dataSourceNo(1).addRange(fieldNum(PackListView, PtsSalesorderPacked)).value(queryValue(false));
q.dataSourceNo(1).addRange(fieldNum(PackListView, PtsWebDeliveryDate)).value(queryrange(d, today()+daysahead));


    qr = new queryRun(q);

        while( qr.next())// && counter < 100
        {
             //Do stuff
        }

So how do I do incorporate the OR operators with this?

Cheers guys :)

You can generate the querystring with your criteria like this:

.value(strfmt("((Field1 == %1 && Field2 == %2) || Field1 != %3)", 
 var1, var2, var3, varX, ...));

May be it's not very smart but its the way the standard does. You can use queryValue function with some variables like base enums or dates to get the proper query string for each value.

您可以使用逗号分隔值并将 csv 传递给值函数,例如 q.dataSourceNo(1).addRange(fieldNum(PackListView, SalesStatus)).value(queryValue(SalesStatus::Delivered) + "," + "Open order "));

I decided to make a temporary table out of it, doing all the filtering in a standard select statement, and later i used a x++ querybuilding to add sorting and the different stuff needed. this worked out great and felt more easy to do rather than injecting long string queries into the queryobjects.

But thanks for the answers :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM