Friday, March 16, 2012

Using Expression in Query

Note: when string field is used in filteration then "" should be used. In the below example GroupId is string and when integer fields is used, no need to used "".
void queryFilter()
{
    AA_PriceDiscGroupBrand      priceDiscGroupBrand;
    str                         qbrValue,qbrGroupValue, qbrGroupValueAll;
    int                         relationVal;
    boolean                     recordFound = false;
    ;
    while select Type, GroupId from priceDiscGroupBrand  where
        (prevDate                >= priceDiscGroupBrand.FromDate  || ! priceDiscGroupBrand.FromDate)       &&
        (prevDate                  <= priceDiscGroupBrand.ToDate    || ! priceDiscGroupBrand.ToDate) &&
        priceDiscGroupBrand.CustAccount == custAccount

    {
        recordFound = true;
        if (priceDiscGroupBrand.Type == PriceGroupType::LineDiscGroup)
            relationVal = 5;
        if (priceDiscGroupBrand.Type == PriceGroupType::PriceGroup)
            relationVal = 4;

        qbrGroupValue = strFmt('(' +
                           '((%1.%2 == "%3") && (%1.%4 == %5) && (%1.%6 == %7))' +
                          ')',
                        this.query().dataSourceNo(1).name(),
                        fieldStr(PriceDiscTable,AccountRelation),
                        priceDiscGroupBrand.GroupId,
                        fieldStr(PriceDiscTable,AccountCode),
                        1,
                        fieldStr(PriceDiscTable,Relation),
                        relationVal
                        );
        if (!qbrGroupValueAll)
            qbrGroupValueAll = qbrGroupValue;
        else
            qbrGroupValueAll = qbrGroupValueAll + '||' + qbrGroupValue;
    }

    if (recordFound)
    {
        qbrGroupValueAll = '(' + qbrGroupValueAll + ')';
        qbrValue = strFmt('(' +
                          '((%1.%2 == "%3") && (%1.%4 == %5) && ((%1.%6 == %7) || (%1.%6 == %8)))'
                          + '||' +
                          '((%1.%2 == "%9") && (%1.%4 == %10) && ((%1.%6 == %7) || (%1.%6 == %8)))'
                          + '||' + qbrGroupValueAll +
                          ')',
                            this.query().dataSourceNo(1).name(),
                            fieldStr(PriceDiscTable,AccountRelation),
                            custAccount,
                            fieldStr(PriceDiscTable,AccountCode),
                            0,
                            fieldStr(PriceDiscTable,Relation),
                            4,5,
                            "",
                            2
                            );
    }
    else
    {

          qbrValue = strFmt('(' +
                          '((%1.%2 == "%3") && (%1.%4 == %5) && ((%1.%6 == %7) || (%1.%6 == %8)))'
                          + '||' +
                          '((%1.%2 == "%9") && (%1.%4 == %10) && ((%1.%6 == %7) || (%1.%6 == %8)))' + ')',
                            this.query().dataSourceNo(1).name(),
                            fieldStr(PriceDiscTable,AccountRelation),
                            custAccount,
                            fieldStr(PriceDiscTable,AccountCode),
                            0,
                            fieldStr(PriceDiscTable,Relation),
                            4,5,
                            "",
                            2
                            );
    }
    this.query().dataSourceNo(1).clearDynalinks();
    this.query().dataSourceNo(1).clearRanges();
    this.query().dataSourceTable(tableNum(PriceDiscTable)).addRange(fieldNum(PriceDiscTable,recid)).value(qbrValue);

}