Notifications
Announcements
Hi everyone,
I'm building a custom form in D365FO that summarizes sales by ItemId. Now I need to extend it to group by both ItemId and InvoiceAccount.
ItemId
InvoiceAccount
The issue is that InvoiceAccount is in CustInvoiceJour, and I’m trying to join it with CustInvoiceTrans using:
CustInvoiceTrans
CustInvoiceJour.SalesId == CustInvoiceTrans.SalesId CustInvoiceJour.InvoiceId == CustInvoiceTrans.InvoiceId CustInvoiceJour.InvoiceDate == CustInvoiceTrans.InvoiceDate CustInvoiceJour.NumberSequenceGroup == CustInvoiceTrans.NumberSequenceGroup
while select sum(LineAmount), sum(Qty), invoiceTrans.ItemId, invoiceJour.InvoiceAccount from invoiceTrans join invoiceJour where ... group by invoiceTrans.ItemId, invoiceJour.InvoiceAccount
How can I correctly write a grouped aggregation using sum() across two joined tables in X++? Thanks in advance!
sum()
internal final class community_selectExample { public static void main(Args _args) { CustInvoiceJour custInvoiceJour; CustInvoiceTrans custInvoiceTrans; while select InvoiceAccount from custInvoiceJour group by custInvoiceJour.InvoiceAccount, custInvoiceTrans.ItemId join sum(LineAmount), sum(Qty), ItemId from custInvoiceTrans where custInvoiceTrans.InvoiceId == custInvoiceJour.InvoiceId && custInvoiceTrans.SalesId == custInvoiceJour.SalesId && custInvoiceTrans.InvoiceDate == custInvoiceJour.InvoiceDate && custInvoiceTrans.numberSequenceGroup == custInvoiceJour.numberSequenceGroup { info(strFmt("Invoice Account %1, ItemId %2, qty %3", custInvoiceJour.InvoiceAccount, custInvoiceTrans.ItemId, custInvoiceTrans.Qty)); break; //Break early } } }
Hello Jonas, thanks a lot for your detailed reply!
I'm still new to X++ and I'm having some difficulty understanding exactly how to use QueryBuildDataSource in this context. Your explanation helped, but I'm unsure how to build the query dynamically in code, especially with aggregation (sum and group by).
QueryBuildDataSource
Would you happen to have a simple code example? Or maybe a standard form I could look at that uses QueryBuildDataSource for grouping and joining?
Just to give more context:
The form has two date filters (StartDate, EndDate)
StartDate
EndDate
I'm aggregating sales data by ItemId
I now need to include InvoiceAccount (from CustInvoiceJour) because the same item can be sold to different customers
CustInvoiceJour
Then I store those results in a temporary table (MET_TmpSalesInvoicesSum)
MET_TmpSalesInvoicesSum
Later, a second button uses that list of ItemId to find the related PriceCalcId and calls BOMCalcProvider::createDetails() to compute added value
PriceCalcId
BOMCalcProvider::createDetails()
Here is the current logic I use with a basic while select:
while select
while select sum(LineAmount), sum(Qty), ItemId from invoiceTrans group by ItemId, InvoiceId where invoiceTrans.InvoiceDate >= startDate && invoiceTrans.InvoiceDate <= endDate && invoiceTrans.ItemId == 'A/PROTO48/A00' { // Insert aggregated result into tmp table }
But since InvoiceAccount is in CustInvoiceJour, I understand that I now need to join it and properly group by both ItemId and InvoiceAccount. My current approach isn't working.
Any tips or examples on how to build this query dynamically with QueryBuildDataSource would be really appreciated!
Thanks again for your help!
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.
Abhilash Warrier 843 Super User 2025 Season 2
André Arnaud de Cal... 428 Super User 2025 Season 2
Martin Dráb 357 Most Valuable Professional