Notifications
Announcements
No record found.
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?
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); }
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.
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?
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); }
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)
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.
As AI tools become more common, we’re introducing a Responsible AI Use…
We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…
These are the community rock stars!
Stay up to date on forum activity by subscribing.
Martin Dráb 503 Most Valuable Professional
André Arnaud de Cal... 434 Super User 2025 Season 2
BillurSamdancioglu 278 Most Valuable Professional