Sunday, June 29, 2025

Code to get customer and vendor posted and unposted invoice journals in D365F&O X++

   public void clicked()

        {

            VendInvoiceInfoLine         vendInvoiceInfoLine;

            ACCOUNTINGDISTRIBUTION      accoutingDistribution;

            LedgerAccount               ledgerAccount;

            VendInvoiceInfoTable        vendInvoiceInfoTable;

            Amount                      totalAmount;

            container                   con;

            VendInvoiceJour             vendInvoiceJour;

            VendInvoiceTrans            vendInvoiceTrans;

            LedgerJournalTable          ledgerJournalTable;

            LedgerJournalTrans          ledgerJournalTrans;

            RecId                       dynamicVendorAccount;

            GeneralJournalAccountEntry  generalJournalAccountEntry;

            GeneralJournalEntry         generalJournalEntry;

            CustInvoiceTable            custInvoiceTable;

            CustInvoiceLine             custInvoiceLine;

            CustInvoiceJour             custInvoiceJour;

            CustInvoiceTrans            custInvoiceTrans;

            MainAccount                 mainaccount;


            super();


            RPTLines.GLAccount =  GLAccount.valueStr();

            

            if(RPTLines.PartyType == PwC_PartyType::Vendor)

            {

                con =str2con(RPTLines.GLAccount,";",false);


                for(int c=1; c<=conLen(con);c++)

                {

                    Amount  unpostedTotal,postedTotal,transTotal;


                    //for unposted vendor invoices

                    while select vendInvoiceInfoLine

                        join vendInvoiceInfoTable

                            where vendInvoiceInfoTable.TableRefId == vendInvoiceInfoLine.TableRefId

                        join  accoutingDistribution

                             where accoutingDistribution.SourceDocumentLine ==  vendInvoiceInfoLine.SourceDocumentLine

                            && vendInvoiceInfoTable.ParmId  ==  vendInvoiceInfoLine.ParmId

                            && vendInvoiceInfoTable.DocumentDate >= RPTLines.FromDate

                            && vendInvoiceInfoTable.DocumentDate <= RPTLines.ToDate

                            &&  vendInvoiceInfoTable.ParmJobStatus != ParmJobStatus::Executed

                    {

                        ledgerAccount = MainAccount::findByLedgerDimension(accoutingDistribution.LedgerDimension).MainAccountId;


                        if(ledgerAccount == conPeek(con, c))

                        {

                            unpostedTotal += vendInvoiceInfoLine.LineAmount;

                        }

                    }


                    //posted vendor invoices

                    while select generalJournalAccountEntry

                            join generalJournalEntry

                                where generalJournalEntry.RecId == generalJournalAccountEntry.GeneralJournalEntry

                            join vendInvoiceJour

                                where vendInvoiceJour.LedgerVoucher == generalJournalEntry.SubledgerVoucher

                                &&  vendInvoiceJour.InvoiceDate >= RPTLines.FromDate

                                &&  vendInvoiceJour.InvoiceDate <= RPTLines.ToDate

                    {

                        ledgerAccount = MainAccount::findByLedgerDimension(generalJournalAccountEntry.LedgerDimension).MainAccountId;


                        if(ledgerAccount == conPeek(con, c))

                        {

                            postedTotal += generalJournalAccountEntry.AccountingCurrencyAmount;

                        }

                    }


                    dynamicVendorAccount = LedgerDynamicAccountHelper::getDynamicAccountFromAccountNumber(RPTLines.PartyNumber,LedgerJournalACType::Vend);


                    //GL entries for vendor

                    while select ledgerJournalTrans

                            join ledgerJournalTable

                            where ledgerJournalTable.JournalNum == ledgerJournalTrans.JournalNum

                            && ledgerJournalTrans.DocumentDate >= RPTLines.FromDate

                            &&  ledgerJournalTrans.DocumentDate <=  RPTLines.ToDate

                            && ledgerJournalTrans.LedgerDimension == dynamicVendorAccount

                            && ledgerJournalTrans.Invoice !=''

                            && ledgerJournalTable.Posted == NoYes::No

                    {

                        ledgerAccount = MainAccount::findByLedgerDimension(ledgerJournalTrans.OffsetLedgerDimension).MainAccountId;


                        if(ledgerAccount == conPeek(con, c))

                        {

                            if(ledgerJournalTrans.AmountCurDebit)

                            {

                                transTotal += ledgerJournalTrans.AmountCurDebit;

                            }

                            else if(ledgerJournalTrans.AmountCurCredit)

                            {

                                transTotal += ledgerJournalTrans.AmountCurCredit;

                            }

                        }

                    }

                            

                    totalAmount +=  unpostedTotal + postedTotal + transTotal;

                }


                RPTLines.TransactionAmount =  totalAmount;

                

            }


            else  if(RPTLines.PartyType == PwC_PartyType::Customer)

            {

                con =str2con(RPTLines.GLAccount,";",false);

                Amount custTotalAmount;

                for(int c=1; c<=conLen(con);c++)

                {

                    Amount  unposted,GLAmount;

                    mainaccount = MainAccount::findByMainAccountId(conPeek(con,c));


                    RecId  mainaccountValue = LedgerDefaultAccountHelper::getDefaultAccountFromMainAccountRecId(mainaccount.RecId);


                    //posted and unposted invoices

                    while select custInvoiceLine

                       join  custInvoiceTable

                        where custInvoiceTable.RecId == custInvoiceLine.ParentRecId

                        && custInvoiceTable.DocumentDate >= RPTLines.FromDate

                        &&  custInvoiceTable.DocumentDate <=  RPTLines.ToDate

                        && custInvoiceLine.LedgerDimension == mainaccountValue

                    {

                        unposted += custInvoiceLine.AmountCur;

                    }


                    dynamicVendorAccount = LedgerDynamicAccountHelper::getDynamicAccountFromAccountNumber(RPTLines.PartyNumber,LedgerJournalACType::Cust);


                    //GL entries for customer

                    while select ledgerJournalTrans

                            join ledgerJournalTable

                            where ledgerJournalTable.JournalNum == ledgerJournalTrans.JournalNum

                            && ledgerJournalTrans.DocumentDate >= RPTLines.FromDate

                            &&  ledgerJournalTrans.DocumentDate <=  RPTLines.ToDate

                            && ledgerJournalTrans.LedgerDimension == dynamicVendorAccount

                            && ledgerJournalTrans.Invoice !=''

                    {

                        ledgerAccount = MainAccount::findByLedgerDimension(ledgerJournalTrans.OffsetLedgerDimension).MainAccountId;


                        if(ledgerAccount == conPeek(con, c))

                        {

                            if(ledgerJournalTrans.AmountCurDebit)

                            {

                                GLAmount += ledgerJournalTrans.AmountCurDebit;

                            }

                            else if(ledgerJournalTrans.AmountCurCredit)

                            {

                                GLAmount += ledgerJournalTrans.AmountCurCredit;

                            }

                        }

                    }

                    custTotalAmount +=  unposted + GLAmount;

                }

                RPTLines.TransactionAmount =  custTotalAmount;

            }

            GLAccount.text('');

        }

Monday, June 9, 2025

Get size of tables from SQL Data base for D365FO

 In order to check the tables size, you need to open the SQL server Management Studio and execute the below query

  1. In the Management Studio, click New Query or expand the Databases list and right click the needed database > New Query
  2. In the query editor, execute the below query by putting the correct database name

    USE <DatabaseName>
    GO

    SELECT
    t.Name AS TableName,
    s.Name AS SchemaName,
    p.Rows AS RowCounts,
    SUM(a.total_pages) * 8 AS TotalSpaceKB,
    SUM(a.used_pages) * 8 AS UsedSpaceKB,
    (SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceKB
    FROM
    sys.tables t
    INNER JOIN sys.indexes i ON t.object_id = i.object_id
    INNER JOIN sys.partitions p ON i.object_id = p.object_id AND i.index_id = p.index_id
    INNER JOIN sys.allocation_units a ON p.partition_id = a.container_id
    LEFT OUTER JOIN sys.schemas s ON t.schema_id = s.schema_id
    WHERE t.Name NOT LIKE 'dt%'
    AND t.is_ms_shipped = 0
    AND i.object_id > 255
    GROUP BY t.Name, s.Name, p.Rows
    ORDER BY SUM(a.used_pages) * 8 DESC;
    GO
3.Once the query is executed, you will get the list of tables sorted by its used space
Tables size

Another one :

EXEC sp_spaceused 'generaljournalentry'

//for a specific table