Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX (Archived)

Dynamic Query works based on OR condition ?

Posted on by 286

Hi, Everyone.

I want records based on query act like or condition. I have two queries, The case is 1st query have the records it pulls out otherwise act 2nd query and get the records. Below is my query Please review it. That's correct or not.

public void executeQuery()

{
    Date                    _date;
    ProcessLineProdLoad     pickLastDate,_processLineProdLoad;
    Query                   q = new Query();
    Query                   q2 = new Query();
    QueryBuildDataSource    qbr1,qbds2;
    QueryBuildRange         qbr,qbr2;
    QueryRun                qr,qr2;
    NoYes                   _unrackedFlag,_rackedFlag;
    boolean                     deviatedQuery;
    ;

    _unrackedFlag = NoYes::No;
    _rackedFlag   = NoYes::Yes;

    while select pickLastDate
        order by RackStartDate desc
        {
            if(pickLastDate.RackStartDate != datenull())
            {
            _date = pickLastDate.RackStartDate;
            }
        }

        qbr1 = q.addDataSource(tablenum(ProcessLineProdLoad));

       this.query().dataSourceNo(1).clearRanges();



   if(ProductionPool.valueStr()||SchedDate.dateValue()||ProcessLine.valueStr()||UnLoadedFlag.valueStr())
   {
     
   // 1st query started

    qbr =qbr1.addRange(fieldNum(ProcessLineProdLoad,ProcessLineId));
    qbr.value(ProcessLine.valueStr());

    qbr =    qbr1.addRange(fieldNum(ProcessLineProdLoad,SchedDate));
    qbr.value(sysquery::range(SchedDate.dateValue(),SchedDate.dateValue()));

    qbr =qbr1.addRange(fieldNum(ProcessLineProdLoad,ProdPoolId));
    qbr.value(ProductionPool.valueStr());

    qbr =qbr1.addRange(fieldNum(ProcessLineProdLoad,UnRackedFlag));
    qbr.value(UnLoadedFlag.valueStr());

    qbr = qbr1.addRange(fieldnum(ProcessLineProdLoad,RackedFlag));
    qbr = qbr1.addRange(fieldNum(ProcessLineProdLoad,RackStartDate));
    qbr =  qbr1.addRange(fieldNum(ProcessLineProdLoad, RackedQty));
    
    qbr = qbr1.addRange(fieldnum(ProcessLineProdLoad,Division));


    qbr.value(strfmt('(( (%1 == %2)) || ((%3 >= %4)||(%3<=%5))&& (%6 > %7) && (%8 == %9))',    

    fieldstr(ProcessLineProdLoad,RackedFlag),
    queryValue("1"),

    fieldstr(ProcessLineProdLoad,RackStartDate),
    queryValue(_date),
    queryValue(today()),

    fieldstr(ProcessLineProdLoad,RackedQty),
    queryValue("0"),    

    fieldstr(ProcesslineProdLoad,Division),
    queryValue("22")
    ));

    qbr1.addSortField(fieldnum(ProcessLineProdLoad,ProcessLineId),SortOrder::Ascending);
    qbr1.addSortField(fieldnum(ProcessLineProdLoad,SchedDate),SortOrder::Ascending);
    qbr1.addSortField(fieldnum(ProcessLineProdLoad,LoadRunOrder),SortOrder::Ascending);
    qbr1.addSortField(fieldnum(ProcessLineProdLoad,LoadNum),SortOrder::Ascending);
    qbr1.addSortField(fieldnum(ProcessLineProdLoad,ProdId),SortOrder::Ascending);

    qr = new QueryRun(q);

    while(qr.next())
    {
        _processLineProdLoad = qr.get(tablenum(ProcessLineProdLoad));

        if(_processLineProdLoad)
        {
            this.query(q);
deviatedQuery = true; } } // 1st query ended // 2nd query started if(!deviatedQuery) { qbds2 = q2.addDataSource(tablenum(ProcessLineProdLoad)); qbr2 = qbds2.addRange(fieldnum(ProcessLineProdLoad,ProcessLineId)); qbr2.value(ProcessLine.valueStr()); qbr2 = qbds2.addRange(fieldnum(ProcessLineProdLoad,UnrackedFlag)); qbr2.value(UnLoadedFlag.valueStr()); qbr2 = qbds2.addRange(fieldnum(ProcessLineProdLoad,RackedFlag)); qbr2.value("1"); qr2 = new QueryRun(q2); while(qr2.next()) { _processLineProdLoad = qr2.get(tablenum(ProcessLineProdLoad)); if(_processLineProdLoad) { this.query(q2); } } } // 2nd query ended } super(); }

*This post is locked for comments

  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 230,198 Most Valuable Professional on at
    RE: Dynamic Query works based on OR condition ?

    For reference, we've had a discussion on this topic in DUG forum.

  • Peter John Profile Picture
    Peter John 286 on at
    RE: Dynamic Query works based on OR condition ?

    Hi, Everyone.

    Added some points about above questions.

    Line 80 has the process to proceed 1st query in case it will fail then line 96 will be executed or not? This is my doubt

    Rather than how can I write OR conditions in Dynamic Query...

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans