web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested answer

create and post general journal using code

(0) ShareShare
ReportReport
Posted on by 1,790
Hi Everyone,
 
I was trying to create and post general journal from ledger to vendor but  in debugger i am getting the offsetledger dimension  value zero can you please check my code and let me know what i am doing wrong .
public class journalPost
{
    public static void main(Args _args)
    {
        ClaimHeader journalTable;
        ClaimLine   journalTableLine;
        journalTable = _args.record();

        if (!journalTable)
        {
            throw error("No journal selected.");
        }

        LedgerJournalTable       ledgerJournalTable;
        LedgerJournalTrans       ledgerJournalTrans;
        LedgerJournalCheckPost   journalCheckPost;
        LedgerJournalName        ledgerJournalName;
        TrvParameters            trvParameters;
        Ledger                   currentLedger;
        Ledger                   ledgerFromFind;
        VendTable                vendTable;
      	ClaimType  		 claimtype;
        TransDate                transDate;
        NumberSeq                numberseq;
        ExchRate                 exchangeRate;
        Voucher                  voucher;
        RecId                    ledgerRecId;
        CurrencyCode             fromCurrency,toCurrency;


        select JournalName from  trvParameters;

        // Header
        ledgerJournalTable = null;
        ledgerJournalTable.company(curext());
        ledgerJournalTable.JournalName = trvParameters.JournalName;
        ledgerJournalTable.initFromLedgerJournalName();
        ledgerJournalTable.NumberSequenceTable = ledgerJournalName.NumberSequenceTable;
        ledgerJournalTable.insert();

        if (ledgerJournalTable.RecId)
        {
            // Line
            ledgerJournalTrans.clear();
            ledgerJournalTrans.Company = curext();
            ledgerJournalTrans.JournalNum = ledgerJournalTable.JournalNum;
            ledgerJournalTrans.TransDate = journalTable.EventDate;
            ledgerJournalTrans.LineNum = LedgerJournalTrans::lastLineNum(ledgerJournalTrans.JournalNum);

            voucher = new JournalVoucherNum(JournalTableData::newTable(ledgerJournalTable)).getNew(false);

            ledgerRecId = Ledger::current();

            ledgerFromFind = Ledger::find(ledgerRecId);
            fromCurrency = ledgerFromFind.AccountingCurrency;

            vendTable = VendTable::find("1001");
            toCurrency = vendTable.Currency;

            transDate = systemDateGet();

           exchangeRate = ExchangeRateHelper::MCRGetExchangeRate( fromCurrency,
                                                                  toCurrency,
                                                                  transDate
                                                                );

            ledgerJournalTrans.Voucher = voucher;
            ledgerJournalTrans.CurrencyCode = ledgerFromFind.AccountingCurrency;

            select Amount from journalTableLine 
                where journalTableLine.ClaimHeader == journalTable.RecId;

            ledgerJournalTrans.AmountCurDebit = journalTableLine.Amount;
            ledgerJournalTrans.ExchRate =  exchangeRate;

            select firstonly claimtype
                where claimtype.ClaimType == journalTable.ClaimType;

            ledgerJournalTrans.AccountType = claimtype.AccountType;
            ledgerJournalTrans.LedgerDimension = claimtype.LedgerDimension;

            ledgerJournalTrans.OffsetAccountType = claimtype.OffsetAccountType;

            // Get MainAccountNum
            MainAccount  mainAccount = mainAccount::findByMainAccountId(claimtype.OffsetAccount);
            RecId  vendorMainAccount = mainAccount.RecId;

            if ( claimtype.OffsetAccountType == LedgerJournalACType::Vend)
            {
                ledgerJournalTrans.OffsetLedgerDimension = LedgerDimensionFacade::serviceCreateLedgerDimension(vendorMainAccount);
                                                                                                               
            }
            else
            {
                ledgerJournalTrans.OffsetLedgerDimension = claimtype.OffsetLedgerDimension;
            }

            ledgerJournalTrans.insert();

            if (ledgerJournalTrans.RecId)
            {
                // Posting
                journalCheckPost = LedgerJournalCheckPost::newLedgerJournalTable(ledgerJournalTable, NoYes::Yes);
                journalCheckPost.runOperation();

                ledgerJournalTable.reread();
                if (ledgerJournalTable.Posted == NoYes::Yes)
                {
                    info(strFmt("Journal %1 has been created and posted.", ledgerJournalTable.JournalNum));
                }
            }

        }
    }

}
 
I have the same question (0)
  • André Arnaud de Calavon Profile Picture
    297,242 Super User 2025 Season 2 on at
    create and post general journal using code
    Hi Dinesh,

    I think the ClaimType object is a custom table in your environment? Can you tell us is this is a shared table or per company? What exact value are you storing in the field OffSetLedgerDImension? Is it a main account, default dimensions, or main account with dimensions?
     
    During the debugger, did you find a record in the ClaimTable? Are all offset ledger account values zero or only for vendors or ledger?
     
     
  • Dineshkarlekar Profile Picture
    1,790 on at
    create and post general journal using code
     thanks for reply ,
     
    I have check in sql the value for the mainAccount.OffsetLedgerDimension and vendtable.OffsetLedgerDimension is zero so if i use it , it will not hget any value to pass .
     
    Thanks,
  • Suggested answer
    Sagar121 Profile Picture
    283 on at
    create and post general journal using code
    Hi , Can you please try this. 
     

    if ( claimtype.OffsetAccountType == LedgerJournalACType::Vend)

                {               

    ledgerJournalTrans.OffsetLedgerDimension = LedgerDimensionFacade::serviceCreateLedgerDimension(mainAccount.OffsetLedgerDimension);                                                                                                           

                }

                else

                {                ledgerJournalTrans.OffsetLedgerDimension = claimtype.OffsetLedgerDimension;

     

                }


     
     
  • Dineshkarlekar Profile Picture
    1,790 on at
    create and post general journal using code
    thanks for reply @Rahul T,
     
    is it possible to create new dimension for vendor , or get dimension from vendor , i cannot pass it from claim type if i am passing it for vendor.
     
    Thanks.
  • Suggested answer
    Rahul T Profile Picture
    29 on at
    create and post general journal using code
    Hi Dinesh,
     
    Regarding the else part of the code, there is currently no validation to check whether the claimtype.OffsetLedgerDimension is null, unset, or outdated. If this value is null, it may lead to an invalid or incorrect offset ledger dimension on the journal entry line.
     
    else
    {
      ledgerJournalTrans.OffsetLedgerDimension = claimtype.OffsetLedgerDimension;
    }
     
    It is advisable to implement a validation step to ensure that claimtype.OffsetLedgerDimension is not null and that it accurately corresponds to a valid financial dimension before assigning it. This will help prevent potential errors in the journal posting process.
  • CU02090655-2 Profile Picture
    on at
    create and post general journal using code
    The primary reason for the zero OffsetLedgerDimension is that you are passing the RecId of the vendor's main account to LedgerDimensionFacade::serviceCreateLedgerDimension(), which expects the string value of the account number. You should change the parameter to claimtype.OffsetAccount. Additionally, your code tries to use trvParameters.JournalName before you have selected the record from the database, which will cause the journal header creation to fail.

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…

Mansi Soni – Community Spotlight

We are honored to recognize Mansi Soni as our August 2025 Community…

Congratulations to the July Top 10 Community Leaders!

These are the community rock stars!

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

#1
Sohaib Cheema Profile Picture

Sohaib Cheema 708 User Group Leader

#2
Martin Dráb Profile Picture

Martin Dráb 589 Most Valuable Professional

#3
Yng Lih Profile Picture

Yng Lih 553

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans