Friday, October 13, 2023

Code to get vendor Voucher transcations from Mainaccount in D365 X++

 My requirement is to get the vendor for main account transcations if the voucher is available.

        MainAccount                 mainAccount;

       GeneralJournalAccountEntry  generalJournalAccountEntry;

       GeneralJournalEntry         generalJournalEntry,generalJournalEntryloc;

       VendTrans                   vendTrans;

       while select generalJournalAccountEntry

           where generalJournalAccountEntry.MainAccount == mainAccount.RecId // get the current record of main account.

       {

           select generalJournalEntry

               where generalJournalEntry.RecId == generalJournalAccountEntry.GeneralJournalEntry;

 

           select vendTrans

               where vendTrans.Voucher == generalJournalEntry.SubledgerVoucher;

           if(vendTrans)

           {

               update_recordset generalJournalEntryloc

                       setting AccountNum = vendTrans.AccountNum

                   where generalJournalEntryloc.RecId == generalJournalEntry.RecId;

           }

       }

........................................................................................

Other Way :

[ExtensionOf(formDataSourceStr(LedgerTransAccount, GeneralJournalAccountEntry))]

internal final class DaxLedgerTransAccountForm_Extension

{

   public display VendAccount displayVendAccount(GeneralJournalAccountEntry _entry)

   {

       NoYes multipleTransWithinOneVoucher = LedgerParameters::find().AllowMultipleTransactionsWithinOneVoucher;

       VendTable vendorTable = this.findVendTable(_entry);

       VendAccount vendAccounts = vendorTable.AccountNum;

 

       if (multipleTransWithinOneVoucher == NoYes::Yes && vendorTable)

       {

           vendorTable = this.findVendTableWithGeneralJournalEntry(vendAccounts, _entry);

 

           if (vendorTable)

           {

               vendAccounts += "@GeneralLedger:LedgerTransVoucherCustVendMultiple";

           }

        }

       return vendAccounts;

   }

   private VendTable findVendTable(GeneralJournalAccountEntry _entry)

   {

       VendTable vendorTable;

       VendTrans vendTrans;

       GeneralJournalEntry vendGeneralJournalEntry;

       LedgerJournalVoucherChanged ledgerJournalVoucherChanged;

       LedgerJournalVoucherChanged ledgerJournalAndApprovalVoucherChanged;

           select firstonly AccountNum from vendorTable

               exists join vendGeneralJournalEntry

                   where vendGeneralJournalEntry.RecId == _entry.GeneralJournalEntry

               exists join vendTrans

                   where vendTrans.AccountNum == vendorTable.AccountNum

                       && vendGeneralJournalEntry.SubledgerVoucher == vendTrans.Voucher

                       && vendGeneralJournalEntry.SubledgerVoucherDataAreaId == vendTrans.DataAreaId;

 

       if (!vendorTable.AccountNum)

       {

           select firstonly AccountNum from vendorTable

                   exists join vendGeneralJournalEntry

                       where vendGeneralJournalEntry.RecId == _entry.GeneralJournalEntry

                   exists join ledgerJournalVoucherChanged

                       where ledgerJournalVoucherChanged.ToVoucher == vendGeneralJournalEntry.SubledgerVoucher

                           && ledgerJournalVoucherChanged.Todate == vendGeneralJournalEntry.AccountingDate

                   exists join vendTrans

                       where vendTrans.AccountNum == vendorTable.AccountNum

                           && ledgerJournalVoucherChanged.FromVoucher == vendTrans.Voucher

                           && ledgerJournalVoucherChanged.FromDate == vendTrans.TransDate

                           && vendGeneralJournalEntry.SubledgerVoucherDataAreaId == vendTrans.DataAreaId;

       }

       return vendorTable;

   }

   private VendTable findVendTableWithGeneralJournalEntry(VendAccount _vendAccounts, GeneralJournalAccountEntry _entry)

   {

       VendTable vendorTable;

       VendTrans vendTrans;

       GeneralJournalEntry vendGeneralJournalEntry;

           select firstonly AccountNum from vendorTable

                   exists join vendGeneralJournalEntry

                       where vendGeneralJournalEntry.RecId == _entry.GeneralJournalEntry

                   exists join vendTrans

                       where vendTrans.AccountNum == vendorTable.AccountNum

                           && vendGeneralJournalEntry.SubledgerVoucher == vendTrans.Voucher

                           && vendGeneralJournalEntry.SubledgerVoucherDataAreaId == vendTrans.DataAreaId

                           && vendorTable.AccountNum != _vendAccounts;

       return vendorTable;

   }

}


Output :

General Ledger >> Chart of Accounts >> Accounts >> Main Accounts

Click on the transactions

Account number data is from form init method

Vendor Account is from display method.









No comments:

Post a Comment