Search code examples
dynamics-business-central

How to apply SetFilter in a query object in while loop body after query.open() in AL Business Central


I just want to apply another filter while we are in the body of query during reading it. For Example: Here is my code.

customAttributeQuery.SetFilter(Value_ID, attributeValueID);
                customAttributeQuery.Open();
                while customAttributeQuery.Read() do begin
                    counter := 0;

                   // HERE IN BODY I WANT TO APPLY ANOTHER FILTER 
                     // customAttributeQuery.SetFilter(Value, attributeValue);
                   end;
The issue is when  i apply another filter in the body of while loop and open the query and try to read it .This code always breaks my whole code.


Solution

  • I'm pretty sure you cannot do that, if you apply a filter, most probably you break the loop and jump out. Try to incorporate your filter inside the Query Object if possible, to that extent you can use something like:

    Table 1 as t1 
    {
        Table 2 a t2
        {
            Table 1 as t3
            {}
        }
    }
    

    if you use this correctly, by setting a filter on t2, you get your desired filter on t3, which is pointing to the Table in the t1 loop.