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

Heygar picture Heygar · Apr 26, 2013 · Viewed 19k times · Source

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

Answer

j.a.estevan picture j.a.estevan · Apr 29, 2013

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.