web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested answer

Clear filters of query in x++

(0) ShareShare
ReportReport
Posted on by 113

I have created a batch for posting Movement and Transfer Journals. In Batch standard query filter option used in which user can select the filters .
The filters are not clearing when I open filter page again.

I have used  this but not working - 
queryBuildDataSourceBook.clearRanges();
queryBuildDataSourceBook.clearRange(fieldNum(InventJournalTable,JournalType));

1715.Capture1.PNG

8078.Capture2.PNG

 private void initializeQuery()
    {
        Query query = new Query();       
        QueryBuildDataSource queryBuildDataSourceBook = query.addDataSource(tablenum(InventJournalTable));

        queryBuildDataSourceBook.clearRanges();
        queryBuildDataSourceBook.clearRange(fieldNum(InventJournalTable,JournalType));

        queryBuildDataSourceBook.addSortField(fieldnum(InventJournalTable, JournalId));

        queryBuildDataSourceBook.addRange(fieldnum(InventJournalTable, JournalType));
        queryBuildDataSourceBook.addRange(fieldnum(InventJournalTable, JournalId));

        QueryBuildRange queryBuildRange = queryBuildDataSourceBook.addRange(fieldnum(InventJournalTable, Posted));
        queryBuildRange.status(RangeStatus::Locked);
        queryBuildRange.value(SysQuery::value(NoYes::No));

        queryRun = new QueryRun(query);
    }

I have the same question (0)
  • GirishS Profile Picture
    27,825 Moderator on at
    RE: Clear filters of query in x++

    Hi Harish,

    I think every time you are opening the form initializeQuery method is called and range get added to the filter query form.

    Can you tell me when you are calling the initializeQuery method?

    Thanks,

    Girish S.

  • Suggested answer
    Bharani Preetham Peraka Profile Picture
    3,634 Moderator on at
    RE: Clear filters of query in x++

    Hi, when you open the advanced query, it opens the query which is already defined via code or aot. So which are already given only those ranges it will show. The standard process to reset is to use the reset button in the query. For reference you can check which query that is using and validate those ranges.

  • Martin Dráb Profile Picture
    236,570 Most Valuable Professional on at
    RE: Clear filters of query in x++

    I expect something else than  Girish - that initializeQuery() isn't called at all, because the query is loaded from usage data and not initialized as a new one.

    The code there is useless - there are no ranges to clear on a freshly created data source. You problem is somewhere else.

    First of all, check whether the method is called or not. If it does, the query is overwritten later.

  • HARISH MEHRA Profile Picture
    113 on at
    RE: Clear filters of query in x++

    Hi Girish,

    Calling Initialize method on new.-

    protected void new()

       {

           journalsToBeUpdated = new Set(Types::String);

           super();

           this.initializeQuery();

       }

  • HARISH MEHRA Profile Picture
    113 on at
    RE: Clear filters of query in x++

    /// <summary>
    /// Batch to post the movement and Transfer Journals.
    /// </summary>
    public class InventoryMovementPostBatch_CAP extends RunBaseBatch implements BatchRetryable
    {
    // JournalTransferErrors transferError ;
    NoYes transferError = NoYes::Yes ;
    InventJournalType journalType ;
    InventJournalId journalId ;
    InventJournalNameId journalName ;
    DialogField InvntJrnlTyp;
    DialogField InvntJrnlId;
    DialogField InvntJrnlName;
    DialogField dialogTransferErrors ;
    Set journalsToBeUpdated;
    QueryRun queryRun;
    JournalCheckPost journalCheckPost;
    boolean exceptionFound;
    const int CurrentVersion = 1;

    [Hookable(false)]
    public boolean isRetryable()
    {
    return false;
    }

    public Object dialog(DialogRunbase dialog = null, boolean forceOnClient = false)
    {
    dialog = super();

    dialog.addGroup("@CAP:JournalType");
    InvntJrnlTyp = dialog.addFieldValue(enumStr(InventJournalType),journalType,"@CAP:JournalType");

    dialog.addGroup("@CAP:JournalName");
    InvntJrnlName = dialog.addFieldValue(extendedTypeStr(InventJournalNameId),journalName,"@CAP:JournalName");

    dialog.addGroup("@CAP:JournalId");
    InvntJrnlId = dialog.addFieldValue(extendedTypeStr(InventJournalId),journalId,"@CAP:JournalId");

    dialogTransferErrors = dialog.addFieldValue(enumStr(NoYes),transferError,"@CAP:TransferAllPostingErrorsToANewJournal");

    return dialog;
    }

    public boolean getFromDialog()
    {
    boolean ret;

    ret = super();

    if (ret)
    {
    transferError = dialogTransferErrors.value();
    journalType = InvntJrnlTyp.value();
    journalId = InvntJrnlId.value();
    journalName = InvntJrnlName.value();
    this.updateQueryWithDialogValue();
    }
    return ret;
    }

    protected void new()
    {
    journalsToBeUpdated = new Set(Types::String);

    super();
    this.initializeQuery();
    }

    private void initializeQuery()
    {
    Query query = new Query();
    QueryBuildDataSource queryBuildDataSourceBook = query.addDataSource(tablenum(InventJournalTable));

    queryBuildDataSourceBook.clearRanges();
    queryBuildDataSourceBook.clearRange(fieldNum(InventJournalTable,JournalType));

    queryBuildDataSourceBook.addSortField(fieldnum(InventJournalTable, JournalId));

    queryBuildDataSourceBook.addRange(fieldnum(InventJournalTable, JournalType));
    // queryBuildDataSourceBook.addRange(fieldnum(InventJournalTable, JournalType)).value(queryvalue(0));
    queryBuildDataSourceBook.addRange(fieldnum(InventJournalTable, JournalId));

    QueryBuildRange queryBuildRange = queryBuildDataSourceBook.addRange(fieldnum(InventJournalTable, Posted));
    queryBuildRange.status(RangeStatus::Locked);
    queryBuildRange.value(SysQuery::value(NoYes::No));

    queryRun = new QueryRun(query);
    }

    private void updateQueryWithDialogValue()
    {
    Query qr;
    qr = queryRun.query();

    if (journalType)
    qr.dataSourceTable(tableNum(InventJournalTable)).addRange(fieldNum(InventJournalTable, JournalType)).value(SysQuery::value(journalType));

    if (journalId)
    qr.dataSourceTable(tableNum(InventJournalTable)).addRange(fieldNum(InventJournalTable, JournalId)).value(SysQuery::value(journalId));

    if (journalName)
    qr.dataSourceTable(tableNum(InventJournalTable)).addRange(fieldNum(InventJournalTable, JournalNameId)).value(SysQuery::value(journalName));


    queryRun.query(qr);
    }

    public InventJournalType parmjournalType( InventJournalType _journalType = journalType)
    {
    journalType = _journalType;
    return journalType;
    }

    public InventJournalNameId parmjournalId( InventJournalId _journalId = journalId)
    {
    journalId = _journalId;
    return journalId;
    }

    public QueryRun queryRun()
    {
    return queryRun;
    }

    public void run()
    {
    InventJournalTable inventJournalTable;

    try

    {
    While(queryRun.Next())
    {
    inventJournalTable = queryRun.get(tablenum(InventJournalTable));
    journalCheckPost = InventJournalCheckPost::newPostJournal(inventJournalTable);

    if (transferError)
    {
    journalCheckPost.parmThrowCheckFailed(false);

    journalCheckPost.parmTransferErrors(transferError);
    }

    if (journalCheckPost.validate())
    {
    journalCheckPost.run();
    }
    }
    }
    catch
    {
    info("@CAP:Posted");
    }

    }

    public container pack()
    {
    return [CurrentVersion, queryRun.pack()];
    }

    public boolean unpack(container packedClass)
    {
    Integer version = conpeek(packedClass,1);
    PackedQueryRun packedQuery;

    switch (version)
    {
    case CurrentVersion:
    [version, packedQuery] = packedClass;
    if (SysQuery::isPackedOk(packedQuery))
    {
    queryRun = new QueryRun(packedQuery);
    }
    break;
    default :
    return false;
    }

    return true;
    }

    public boolean validate(Object _calledFrom = null)
    {
    boolean ret;

    ret = super();

    return ret;
    }

    public static InventoryMovementPostBatch_CAP construct()
    {
    return new InventoryMovementPostBatch_CAP();
    }

    public static void main(Args args)
    {
    InventoryMovementPostBatch_CAP inventoryMovementBatch = InventoryMovementPostBatch_CAP::construct();

    inventoryMovementBatch.getLast();

    if (inventoryMovementBatch.prompt())
    {
    inventoryMovementBatch.runOperation();
    }
    }

    public boolean showQueryValues()
    {
    return true;
    }

    protected boolean canRunInNewSession()
    {
    return true;
    }

    }

  • Martin Dráb Profile Picture
    236,570 Most Valuable Professional on at
    RE: Clear filters of query in x++

    In that case, your method is called, but it doesn't have any effect, because its result is later overwritten by the query loaded from usage data. You need to either disable saving to usage data, or overwrite the query after it was loaded.

    By the way, your code is difficult to read because you pasted it in a wrong way. Always use Insert > Code in the rich-formatting view. You did it correctly at the beginning (except of the extra indentation) but somehow forgot it in your replies.

  • HARISH MEHRA Profile Picture
    113 on at
    RE: Clear filters of query in x++

    Hi Martin ,

    I tried to insert code from there but ok button was not working for me  so I pasted it that way.

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
CA Neeraj Kumar Profile Picture

CA Neeraj Kumar 1,771

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 806 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 542 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans