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

Find the Journal number against Vendor account number using X++ code

(0) ShareShare
ReportReport
Posted on by 10

Hi Guys,

I have been learning the MS Dynamics AX 2012 for the past few days and am having difficulty in getting the Journal number against the Vendor account number using the X++ code. It would be really great if you can find a possible solution so that I can improve myself.

Thanks in Advance,

KK.

I have the same question (0)
  • André Arnaud de Calavon Profile Picture
    301,035 Super User 2025 Season 2 on at

    Hi Keerthikeyan,

    Can you explain what exactly you want to achieve?

  • Keerthikeyan. k Profile Picture
    10 on at

    Hi André,

    Thanks for your prompt response.

    I'm currently working on a task where I need to get a vendor Account number from the user. Fetch the last invoiced purchase order details against the vendor account number and also get related data from the "Date of amount settled", Total amount settled for the invoice, mode of payment, payment journal number with date, last invoiced purchase order total amount with invoiceId.

    So far, I have completed the following :

    * Last invoiced purchase order Details against the vendor account number.

    * Date and total amount settled for the invoice.

    * Mode of payment for Invoice.

    * Last invoiced purchase order total amount with invoiceId.

    So, I'm having difficulty getting the payment journal number with the date. Below I've inserted the code for your reference,

    public void vendinvoice1(Args args)

       {

           PurchTable  purchTable;

           PurchLine   purchLine;

           VendTable   vendTable;

           VendTrans   vendTrans;

           InventTable   inventTable;

           Inventsum   inventsum;

           InventDim   inventDim;

           InventSite   inventSite;

           VendInvoiceInfoTable    vendInvoiceInfoTable;

           VendInvoiceJour vendInvoiceJour;

           VendPaymModeTable   vendPaymModeTable;

           LedgerJournalTable  ledgerJournalTable;

           LedgerJournalTrans  ledgerJournalTrans;

           UserInfo    userInfo;

           VendAccount vendAccount;

           Voucher _Voucher;

           PurchId   purchid;

           LedgerJournalId ledgerJournalId;

           DialogField A;    

           Dialog    dialog;

            dialog = new Dialog("Vendor Details");     // Field - EDT, Label

            A = dialog.addField(extendedTypeStr(VendAccount), "Vendor Id : ");    

           dialog.run();

    select firstonly purchLine order by CreatedDateTime desc join vendTable where purchLine.VendAccount == A.value() && PurchLine.PurchStatus == PurchStatus::Invoiced;

       info(strFmt("VendAccount : %1 -- PurchId : %2 -- PurchStatus : %3", purchLine.VendAccount, purchLine.PurchId, purchLine.PurchStatus));

    select vendTrans join vendInvoiceJour where vendTrans.Voucher == vendInvoiceJour.LedgerVoucher && vendTrans.AccountNum == A.value();

       info(strFmt("VoucherNum : %1",vendInvoiceJour.LedgerVoucher));

       _Voucher = "10APINV000010951";

    select firstOnly ledgerJournalTable join vendTrans where ledgerJournalTable.OriginalJournalNum == ledgerJournalTable.JournalNum && vendTrans.Voucher ==  "10APINV000010951";

       info(strFmt("JournalNum : %1",ledgerJournalTable.JournalNum));

    purchid = "10P029245";

           if(purchTable::find(purchid).PurchStatus == PurchStatus::Invoiced)

           {

              select firstOnly vendInvoiceJour where vendInvoiceJour.PurchId == PurchId;

              info(strFmt("InvoiceAmount: %1 -- InvoiceDate: %2 -- InvoiceId: %3 -- PurchId: %4",vendInvoiceJour.InvoiceAmount, vendInvoiceJour.InvoiceDate, vendInvoiceJour.InvoiceId, vendInvoiceJour.PurchId));

              //select firstOnly purchLine where purchLine.PurchId == PurchId ;

              //info(strFmt("PurchId: %1 -- ItemId: %2 -- QtyOrdered: %3",purchLine.PurchId, purchLine.ItemId, purchLine.QtyOrdered));

              info(strFmt("SiteId: %1 -- User: %2 -- AccountNo: %3 -- PaymentMode: %4 -- WarehouseId: %5",InventDim::find(purchLine::find(purchid,1).InventDimId).InventSiteId, HcmWorker::worker2Name(HcmWorker::userId2Worker(purchLine::find(purchid,1).createdBy))

              , purchLine::find(purchid,1).Vendaccount, vendTable::find(purchLine::find(purchid,1).VendAccount).PaymMode, InventDim::find(purchLine::find(purchid,1).InventDimId).wMSLocationId));

              info(strFmt("PurchId: %1 -- ItemId: %2 -- QtyOrdered: %3",purchLine::find(Purchid,1).PurchId, purchLine::find(Purchid,1).ItemId, purchLine::find(Purchid,1).QtyOrdered));

            }

           else

           {

               throw error("Given Purchase Order is not yet invoiced");

           }

       }

    Please let me know your thoughts to take things forward.

    Thanks in Advance,

    KK.

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

    First of all, let me simplify your code by removing unused variables (which are almost all), split code to methods etc. Then I'll post it in the right way: with Insert > Code. This will prevent the duplicate line spacing, it adds line numbers (so we can easily refer to lines of code) and so on.

    public void vendInvoice1()
    {
    	VendAccount vendAccount = this.promptVendAccount();
    
    	this.showLastInvoicePurchLine(vendAccount);
    	this.showVendInvoiceJour(vendAccount);
    	this.showLedgerJour(vendAccount);
    	this.showPurchTable('10P029245');
    }
    
    
    private VendAccount promptVendAccount()
    {
    	Dialog dialog = new Dialog("Vendor Details");
    	DialogField vendAccountField = dialog.addField(extendedTypeStr(VendAccount), "Vendor Id : ");    
    
    	dialog.run();
    	return vendAccountField.value();
    }
    
    private void showLastInvoicePurchLine(VendAccount _vendAccunt)
    {
    	PurchLine purchLine;
    
    	select firstonly purchLine
    		order by CreatedDateTime desc
    		where purchLine.PurchStatus == PurchStatus::Invoiced
               && purchLine.VendAccount == vendAccount;
    
    	info(strFmt("VendAccount : %1 -- PurchId : %2 -- PurchStatus : %3", purchLine.VendAccount, purchLine.PurchId, purchLine.PurchStatus));
    }
    
    private void showVendInvoiceJour(VendAccount _vendAccunt)
    {
    	VendTrans vendTrans;
    	VendInvoiceJour vendInvoiceJour;
    
    	select vendTrans
    		where vendTrans.AccountNum == vendAccount
    		join vendInvoiceJour
    			where vendInvoiceJour.LedgerVoucher == vendTrans.Voucher;
    
    	info(strFmt("VoucherNum : %1", vendInvoiceJour.LedgerVoucher));
    }
    
    
    private void showLedgerJour(VendAccount _vendAccunt)
    {
    	VendInvoiceJour vendInvoiceJour;
    	VendTrans vendTrans;
    
    	// Here you're missing a join condition between ledgerJournalTable and vendTrans
    	select firstOnly ledgerJournalTable
    		where ledgerJournalTable.OriginalJournalNum == ledgerJournalTable.JournalNum
    		join vendTrans
    			where vendTrans.Voucher ==  "10APINV000010951";
    
    	info(strFmt("JournalNum : %1", ledgerJournalTable.JournalNum));
    }
    
    private void showPurchTable(PurchId _purchId)
    {
    	VendInvoiceJour vendInvoiceJour;
    	PurchLine 		purchLine;
    	VendTable 		vendTable;
    	InventDim 		inventDim;
    
    	if (PurchTable::find(_purchId).PurchStatus != PurchStatus::Invoiced)
    	{
    		throw error("Given Purchase Order is not yet invoiced");
    	}
    	
    	select firstOnly vendInvoiceJour
    		where vendInvoiceJour.PurchId == _purchId;
    		
    	info(strFmt("InvoiceAmount: %1 -- InvoiceDate: %2 -- InvoiceId: %3 -- PurchId: %4", 
    		vendInvoiceJour.InvoiceAmount,
    		vendInvoiceJour.InvoiceDate,
    		vendInvoiceJour.InvoiceId,
    		vendInvoiceJour.PurchId));
    		
    	select firstOnly purchLine
    		order by LineNum asc
    		where purchLine.PurchId == _purchId
    		join PaymMode from vendTable
    			where vendTable.AccountNum == purchLine.VendAccount
    		join InventSiteId, wMSLocationId from inventDim
    			where inventDim.InventDimId == purchLine.InventDimId;
    		
    	info(strFmt("SiteId: %1 -- User: %2 -- AccountNo: %3 -- PaymentMode: %4 -- WarehouseId: %5",
    		inventDim.InventSiteId,
    		HcmWorker::worker2Name(HcmWorker::userId2Worker(purchLine.CreatedBy)),
    		purchLine.VendAccount, 
    		vendTable.PaymMode,
    		inventDim.wMSLocationId));
    
    	info(strFmt("PurchId: %1 -- ItemId: %2 -- QtyOrdered: %3",
    		purchLine.PurchId,
    		purchLine.ItemId,
    		purchLine.QtyOrdered));
    }

    Notice the comment in showLedgerJour() pointing out to a bad join.

    Also note that your join between PurchTable and VendInvoiceJour won't work in all cases. The right approach is using VendInvoicePurchLink table.

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 565 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

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

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 250 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans