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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Count number of invoices generated from a sales order

(1) ShareShare
ReportReport
Posted on by

My team wants to get the number of invoices that have been generated from a sales order. We want to do this through a function that gets the number when request is made rather than having to manually filter the invoices from the sales order and then count one by one. Is there a function already in D365 F&O to do this? If not, how can we use X++ to create this function?

I have the same question (0)
  • Suggested answer
    nmaenpaa Profile Picture
    101,162 Moderator on at

    You can write an x statement to count CustInvoiceJour records where SalesId is your sales id.

    You can add this method to SalesTable:

    public int MYgetNrOfInvoices()
    {
        CustInvoiceJour custInvoiceJour;
        select count(RecId) from custInvoiceJour
            where custInvoiceJour.SalesId == this.SalesId;
            
        return int642int(custInvoiceJour.RecId);
    }

  • Suggested answer
    Martin Dráb Profile Picture
    237,965 Most Valuable Professional on at

    I don't think that looking just at CustInvoiceJour.SalesId is correct. If one invoice include multiple orders, only of them will be referenced in CustInvoiceJour.SalesId. A better approach is using CustInvoiceSalesLink table.

  • Community Member Profile Picture
    on at

    Sorry, could you please clarify that? Does it mean to replace every "custInvoiceJour" in Nikolaos's answer with "custInvoiceSalesLink"? Or does it mean to do something (which is?) in CustInvoiceSalesLink instead of SalesTable?

  • Verified answer
    Martin Dráb Profile Picture
    237,965 Most Valuable Professional on at

    Sorry, I didn't realize that it's not sufficient information for you. Here is the code:

    public int myGetNrOfInvoices()
    {
        CustInvoiceSalesLink invoiceLink;
        select count(RecId) from invoiceLink
            where invoiceLink.OrigSalesId == this.SalesId;
            
        return int642int(custInvoiceJour.RecId);
    }

  • Suggested answer
    ergun sahin Profile Picture
    8,826 Moderator on at

    Some country localizations have invoice cancellation structures. It is useful to be careful if you have it too (or you may not want to count invoices with credit note depending on the development demand)

  • Community Member Profile Picture
    on at

    Thank you!

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
Martin Dráb Profile Picture

Martin Dráb 503 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

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

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 278 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans