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 :
Finance | Project Operations, Human Resources, ...
Suggested Answer

PO Lines to Voucher

(5) ShareShare
ReportReport
Posted on by 133

Hello everyone,

I need some help with a query I’m working on.

I’m trying to build an accounting report that mirrors the exact view shown in D365 under General ledger > Inquiries and reports > Voucher transactions. I was able to successfully recreate that view in SQL using the query below:

[SELECT DISTINCT
G.DATE,
G.LEDGERNAME,
G.MAINACCOUNTID, 
G.LEDGERACCOUNT, 
G.NAME,
G.JOURNALNUMBER,
G.VOUCHER,
G.DOCUMENTNUMBER,  
G.CREDIT, 
G.DEBIT, 
G.TAMOUNT, 
V.INVOICEACCOUNT AS VENDORACCOUNT, 
D.NAME AS VENDORNAME,
V.PURCHID AS PONUMBER,
G.TYPE, 
G.POSTINGTYPE, 
C.INVOICEACCOUNT AS CUSTOMERACCOUNT, 
C.INVOICINGNAME AS CUSTOMERNAME
FROM GeneralLedgerTable G
LEFT JOIN VENDINVOICEJOUR V ON V.LEDGERVOUCHER = G.VOUCHER
LEFT JOIN VENDINVOICETRANS T ON T.INVOICEID = V.INVOICEID
LEFT JOIN VENDTABLE VT ON VT.ACCOUNTNUM = V.INVOICEACCOUNT 
LEFT JOIN DIRPARTYTABLE D ON D.RECID = VT.PARTY
LEFT JOIN CUSTINVOICEJOUR C ON C.LEDGERVOUCHER = G.VOUCHER]

The complexity comes in with Posting Types 82 and 236. For these posting types, I want to display the related PO lines, including the item ID, item name, quantity, and line amount. Although I can join the PO line tables to my current query, this causes duplication because the join occurs at the MainAccount level. For example, voucher APO00900 has two voucher transaction lines, both with Posting Type 236. One line is posted to MainAccount 12350, and the other is posted to MainAccount 67890. Both lines reference the same PurchId (PO-09090).When reviewing the purchase order, it contains three lines. Two of those PO lines roll up to the total posted to MainAccount 12350, while the remaining PO line matches the transaction amount posted to MainAccount 67890.If I add the PO lines directly to my current query, each PO line ends up matching both main accounts. This results in six records being returned instead of the expected three. I have been trying to determine whether there is another entity or relationship that can be used to correctly associate the voucher transaction, the main account, and the corresponding PO line(s) but found none. 

Please let me know if I can provide any additional information. I would really appreciate any guidance!

[I created a view to make things cleaner for the GL Data:

SELECT GJ.LEDGERACCOUNT, SUBSTRING(GJ.LEDGERACCOUNT,0,CHARINDEX('-',GJ.LEDGERACCOUNT,0)) AS MAINACCOUNTID,GJ.TEXT AS DESCRIPTION, GJ.TRANSACTIONCURRENCYAMOUNT AS TAMOUNT, 
CASE WHEN GJ.ISCREDIT = 1 THEN GJ.TRANSACTIONCURRENCYAMOUNT END AS CREDIT,
CASE WHEN GJ.ISCREDIT = 0 THEN GJ.TRANSACTIONCURRENCYAMOUNT END AS DEBIT,
GJ.POSTINGTYPE AS TYPE, GJ.POSTINGTYPE_$LABEL AS POSTINGTYPE,
G.ACCOUNTINGDATE AS DATE, G.JOURNALCATEGORY, G.DOCUMENTNUMBER, G.JOURNALNUMBER, G.SUBLEDGERVOUCHER AS VOUCHER, UPPER(G.SUBLEDGERVOUCHERDATAAREAID) AS LEDGERNAME, G.JOURNALCATEGORY_$LABEL, G.RECID,
MA.NAME
FROM GENERALJOURNALACCOUNTENTRY GJ
JOIN GENERALJOURNALENTRY G ON G.RECID = GJ.GENERALJOURNALENTRY
JOIN MAINACCOUNT MA ON MA.MAINACCOUNTID = SUBSTRING(GJ.LEDGERACCOUNT,0,CHARINDEX('-',GJ.LEDGERACCOUNT,0))
WHERE SUBSTRING(GJ.LEDGERACCOUNT,0,CHARINDEX('-',GJ.LEDGERACCOUNT,0)) > 'x9999' ]

Categories:
I have the same question (0)
  • Sohaib Cheema Profile Picture
    49,659 Super User 2026 Season 1 on at
    Hi,
    I cannot see anywhere, your use of PurchLine in the query. Could you show where it is ?
  • Kulsum Profile Picture
    133 on at
    @Sohaib Cheema I am actually not right now, while I can join PurchLine to either VendInvoiceJour or VendInvoiceTrans, but I was able to get the information that I need (i..e.. PurchID, ItemID, Name, Qty, and LineAmount) from VendInvoiceTrans, but I can be wrong. This is just what I concluded. But then again if I were to then I would still just append the join in my current query using PURCHID. These are the joins I would do if I had to include PurchTable and PurchLine:
     
    PurchTable.PURCHID <----- VendInvoiceJour.PURCHID
    PurchTable.PURCHID <----- PurchLine.PURCHID 
     
    I just am unable to get the joins setup in a way that it actually makes sense. I feel like a key or something that helps structure the join better in order for the data to match up correctly. 
  • Suggested answer
    Giorgio Bonacorsi Profile Picture
    1,465 on at
    Hello,

    There is no direct link between the voucher line and the purchase order line (or vendor invoice line).
    During posting, the system aggregates all lines that use the same main account.

    That’s why you cannot simply “recover” the original split — you have to reconstitute it.
    To do this properly, you will need to:
    - Check which main account is tied to each item: go to Inventory posting profiles
    - Based on the amounts shown on the vendor invoice lines, split/reallocate the total amount from the voucher line across the different items;
     
    Good luck!
    Giorgio

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

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

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 590 Super User 2026 Season 1

#2
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 349

#3
Diego Mancassola Profile Picture

Diego Mancassola 263

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans