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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Answered

Problems with Filters on a Custom Query

(0) ShareShare
ReportReport
Posted on by 50

I have created a new Role Centre based on code from Microsoft Learn, and I am now attempting to expand its functionality.

I have created a custom query; see the code below:

query 50130 "Custom Ledg. Entry Sales"
{
    QueryType = Normal;

    elements
    {
        dataitem(CustLedgerEntry; "Cust. Ledger Entry")
        {
            column(SumsalesLCY; "Sales (LCY)")
            {
                Method = Sum;
            }
            filter(DocumentTypeFilter; "Document Type")
            {
            }
            filter(PostingDateFilter; "Posting Date")
            {
            }
            filter(PaymentMethodFilter; "Payment Method Code")
            {
            }
        }
    }
}

The 'Document Type' is an options field, and 'Payment Method Code' is an optional field using a lookup table.

The code to set the filters is below:

Expr2 := 'FARMFIT';
    
        CustLedgEntrySales.SetRange(DocumentTypeFilter,
                                    CustLedgEntry."Document Type"::Invoice);
        CustLedgEntrySales.SetRange(PostingDateFilter,
                                   CalcDate('<-CM>', Workdate), Workdate);
        CustLedgEntrySales.SetFilter(PaymentMethodFilter,
                                     CustLedgEntry."Payment Method Code", '%1', Expr2);

The  filters for the PostingDateFilter and DocumentTypeFilter appear to work as expected.

However, I now want to select Invoices and Credit Memos using the DocumentTypeFilter.

Question: How do you write the code to select more than one document type? 

I have not been able to use the PaymentTypeFilter successfully; the calculation always includes all Payment Method Codes. I am not sure if this is related to the fact the field is a lookup field

Question: Is there a method for using filters on lookup fields?

Any assistance would be much appreciated.

Thank you in advance for your help.

  • Verified answer
    Andy Sather Profile Picture
    Microsoft Employee on at

    Hello  - We currently do not have dedicated Dev support via the Dynamics 365 Business Central forums, but I wanted to provide you some additional resources to assist.  If you need assistance with debugging or coding I would recommend discussing this on one of our communities.

    www.yammer.com/dynamicsnavdev

    I will open this up to the community in case they have something to add.

  • Suggested answer
    RobinsonsScotland Profile Picture
    50 on at

    Good evening Andy,

    Thank you very much for the advice.

    I have sent a request to join the Yammer community.

    Regards,

    Michael

  • Suggested answer
    Vinita_Kas Profile Picture
    336 on at

    Hi There

    Question: How do you write the code to select more than one document type? 

    Can you not use AND?  "Document Type"::Invoice  and "Document Type"::Order   and also try Pipe ||

    I have not been able to use the PaymentTypeFilter successfully; the calculation always includes all Payment Method Codes. I am not sure if this is related to the fact the field is a lookup field

    Question: Is there a method for using filters on lookup fields?

    Can you try this 

    LookUp('Member Contacts',MemberID=ThisItem.'Contact #'.MemberID,State).Name
    
    
    //ThisItem.'Contact #'MemberID will get the unique id in 'Member Contacts' entity
    Then you will get that item's State
    Status.Name is the Name value in State or Province entity

    Refer this solution on thsi link

    https://powerusers.microsoft.com/t5/Building-Power-Apps/how-to-overcome-multiple-levels-of-relationship-expansion/td-p/568143

    Regards

    Vinita 

  • RobinsonsScotland Profile Picture
    50 on at

    Thank you for taking the time to reply; I appreciate it.

    Your solutions appear to be Power Apps based while I am looking for AL based solutions.

  • Suggested answer
    IB-29041624-0 Profile Picture
    1,191 Moderator on at

    To select more than one document type you can use setfiler and seperate the document types with a pipe |

    SetFilter("Document Type", 'Invoice|Credit memo');
  • RobinsonsScotland Profile Picture
    50 on at

    Inge,

    Thank you for taking the time to respond to my request for help.

    I have tried the solution you suggested and it works.

    Thank you again.

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the August Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 765 Super User 2026 Season 2

#2
YUN ZHU Profile Picture

YUN ZHU 571 Super User 2026 Season 2

#3
Grigorios Mavrogeorgis Profile Picture

Grigorios Mavrogeorgis 299 Super User 2026 Season 2

Last 30 days Overall leaderboard

Featured topics

Microsoft Training Manuals

Product updates

Dynamics 365 release plans