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

LedgerJournalTable issue in D365FO

(0) ShareShare
ReportReport
Posted on by 1,215

Hi all,

This mentioned task is already done in 2012. We are upgrading it in d365. The task is In procurement sourcing module-->All purchase order. In PO list page form place a button called create in action pane. for example once all process is done for a particular PO(like approved,invoiced and posted) , then clicking the create button(customized) we have to give the main account id from the voucher form(action pane-->invoice button-->Overview-->voucher button). from that voucher records if we can choose any main account id and give that in the dialog form of the create button. Once generate is clicked, a header and two lines should be created in general journal form(general ledger module).There they will edit the record according to thier wish.
Here in d365 when i choose a main account id and give generate . it throws an error currency code must be specified. The header table called "ledgerjournaltable" and line called "ledgerjournaltrans". In that the currency code is mandatory so i added the currency code field in the code. And it throws this below mentioned error.
pastedImagebase640.png
so i changed the insert to doinsert then one header and one line alone is created and for the second line it throws error that the record already exists so i tried give line number through code(refer notepad) then it again throws the change not allowed error(also tried giving the line number in hardcore also). How to overcome this error? Any solution? Please refer my notepad for code . This code is from 2012 instance and the only change i have done in d365 is 
            //journalTable = new AxLedgerJournalTable();
           // journalTrans = new AxLedgerJournalTrans();
           // journalTrans1 = new AxLedgerJournalTrans();    
The above mentioned classes are not in d365 so i used the table directly. I asked my functional to request change the PO record so that i can create the record but he said it is not done in 2012. so we can't do that in d365.
Below my code.
public void init()
    {
        PurchTable _purchTable;
        PurchId    _purchId;
        _purchTable = element.args().record();
        super();
        _purchId = _purchTable.PurchId;
        ParentPurchId.text(_purchId);
    
    }

    [Control("CommandButton")]
    class OKButton
    {
        void clicked()
        {
            PurchTable updatePurchT,purchtableloc;
            PurchTable selectedDebit;
            PurchLine _purchLIne;
            Name dep,divi,city,region,area;
            Amount _totalAmount;
            PurchId  ID;
            MainAccountNum debitAccount,creditAccount;
            LedgerJournalTable          journalTable;
            LedgerJournalTrans          journalTrans,journalTrans1;
            NumberSeq                   numberSeq;
            NumberSequenceTable numSeqTable;
            Voucher voucher;
           /// container                   accCon;
          //  container                   offSetCon;
            DimensionDynamicAccount     ledgerDim;
            int ROW;
            str description;
            //AxLedgerJournalTable journalTable;
           // AxLedgerJournalTrans journalTrans,journalTrans1;
            container            accCon;
            container            offSetCon;
            container       purchnumber;
        
            Args        args = new Args();
            PurchId _localDialogPurchId = ParentPurchId.valueStr();
            _totalAmount = PurchLine::find(_localDialogPurchId).sumpurchLine();
            debitAccount = MainAccount.text();
            creditAccount = PurchLine::find(_localDialogPurchId).displayMainAccount();
            dep = NQGetDimension::getDimensionValue(PurchLine::find(_localDialogPurchId).DefaultDimension,"ADepartment");
            divi = NQGetDimension::getDimensionValue(PurchLine::find(_localDialogPurchId).DefaultDimension,"BDivisionals");
            city = NQGetDimension::getDimensionValue(PurchLine::find(_localDialogPurchId).DefaultDimension,"DCity");
            region = NQGetDimension::getDimensionValue(PurchLine::find(_localDialogPurchId).DefaultDimension,"CRegional");
            area = NQGetDimension::getDimensionValue(PurchLine::find(_localDialogPurchId).DefaultDimension,"EArea");
            if(!_localDialogPurchId || !_totalAmount || !debitAccount || !creditAccount)
            {
                info(strFmt( " PurchId : %1  TotalAmount: %2  Debit Acount: %3  Credit Account: %4",_localDialogPurchId,_totalAmount,debitAccount,creditAccount));
                throw error("Above field is missing");
            }
            super();
        
            //journalTable = new AxLedgerJournalTable();
           // journalTrans = new AxLedgerJournalTrans();
           // journalTrans1 = new AxLedgerJournalTrans();
            //Journal Name

            journalTable.journalnum = JournalTableData::newTable(journalTable).nextJournalId();

            journalTable.JournalName = "RC-13";         
            journalTable.CurrencyCode = "SAR";          
           // journalTable.CurrencyCode = Ledger::accountingCurrency(CompanyInfo::current());
            journalTable.Name = "Reclassification";
            journalTable.doinsert();

            //Debit
            journalTrans.JournalNum = journalTable.JournalNum;
            journalTrans.LineNum =  LedgerJournalTrans::lastLineNum(journalTrans.JournalNum)   1;
            journalTrans.TransDate = DateTimeUtil::getSystemDate(DateTimeUtil::getUserPreferredTimeZone());
            journalTrans.AccountType = LedgerJournalACType::Ledger;
            accCon = [debitAccount, debitAccount,5,"ADepartment",dep,"BDivisionals",divi,"CRegional",region,"DCity",city,"EArea",area];
            //journalTrans.LedgerDimension = AxdDimensionUtil::getLedgerAccountId(accCon);
            journalTrans.AmountCurDebit = _totalAmount;
            journalTrans.AmountCurCredit = 0;
            journalTrans.Txt = strfmt("%1-Reclassfication",_localDialogPurchId);
            journalTrans.CurrencyCode =  "SAR";
            journalTrans.doinsert();

            //Credit
            offSetCon = [creditAccount, creditAccount, 5,"ADepartment",dep,"BDivisionals",divi,"CRegional",region,"DCity",city,"EArea",area];
            journalTrans1.JournalNum = journalTable.JournalNum;
            journalTrans1.LineNum =  LedgerJournalTrans::lastLineNum(journalTrans.JournalNum)   1;
            journalTrans1.TransDate = DateTimeUtil::getSystemDate(DateTimeUtil::getUserPreferredTimeZone());
            journalTrans1.AccountType = LedgerJournalACType::Ledger;
            journalTrans1.CurrencyCode =  "SAR";
            //journalTrans1.LedgerDimension = AxdDimensionUtil::getLedgerAccountId(offSetCon);
            journalTrans1.AmountCurDebit = 0;
            journalTrans1.AmountCurCredit = _totalAmount;
         
            journalTrans.Txt = strfmt("%1-Reclassfication",_localDialogPurchId); 

            
            journalTrans1.doinsert();
        
            //--------------------------------------
            //for updating journal Num into PurchTable
            select forUpdate updatePurchT where updatePurchT.PurchId == _localDialogPurchId;
            if(updatePurchT)
            {
                ttsBegin;
                updatePurchT.JournalNum = "" ;//journalTable.ledgerJournalTable().JournalNum;
                updatePurchT.doupdate();
                ttsCommit;
            }
        
            args.record(LedgerJournalTable::find(journalTable.JournalNum));
              new MenuFunction(MenuItemDisplayStr(LedgerJournalTable3),MenuItemType::Display).run(Args);
            //  info(strFmt("Journal %1 created total line %1 ", journalTable.ledgerJournalTable().JournalNum,Row));
        }

    }

Regards,
Riyas
I have the same question (0)
  • Verified answer
    Martin Dráb Profile Picture
    237,967 Most Valuable Professional on at

    Your code is difficult to follow. You should use single-responsibiity methods with descriptive name (not just a bunch of code in clicked(), remove unused variable, use "_" prefix just for parameters and not local variable and so on.

    Let me at least throw away unused variables (= most variables) and commented-out code and move variable declarations closer to where variables are used:

    PurchId _localDialogPurchId = ParentPurchId.valueStr();
    Amount _totalAmount = PurchLine::find(_localDialogPurchId).sumpurchLine();
    MainAccountNum debitAccount = MainAccount.text();
    MainAccountNum creditAccount = PurchLine::find(_localDialogPurchId).displayMainAccount();
    Name dep = NQGetDimension::getDimensionValue(PurchLine::find(_localDialogPurchId).DefaultDimension,"ADepartment");
    Name divi = NQGetDimension::getDimensionValue(PurchLine::find(_localDialogPurchId).DefaultDimension,"BDivisionals");
    Name city = NQGetDimension::getDimensionValue(PurchLine::find(_localDialogPurchId).DefaultDimension,"DCity");
    Name region = NQGetDimension::getDimensionValue(PurchLine::find(_localDialogPurchId).DefaultDimension,"CRegional");
    Name area = NQGetDimension::getDimensionValue(PurchLine::find(_localDialogPurchId).DefaultDimension,"EArea");
    
    if(!_localDialogPurchId || !_totalAmount || !debitAccount || !creditAccount)
    {
    	info(strFmt( " PurchId : %1  TotalAmount: %2  Debit Acount: %3  Credit Account: %4",_localDialogPurchId,_totalAmount,debitAccount,creditAccount));
    	throw error("Above field is missing");
    }
    
    super();
    
    LedgerJournalTable journalTable;
    journalTable.JournalNum = JournalTableData::newTable(journalTable).nextJournalId();
    journalTable.JournalName = "RC-13";         
    journalTable.Name = "Reclassification";
    journalTable.CurrencyCode = "SAR";          
    journalTable.doinsert();
    
    //Debit
    LedgerJournalTrans journalTrans;
    journalTrans.JournalNum = journalTable.JournalNum;
    journalTrans.LineNum =  LedgerJournalTrans::lastLineNum(journalTrans.JournalNum)   1;
    journalTrans.TransDate = DateTimeUtil::getSystemDate(DateTimeUtil::getUserPreferredTimeZone());
    journalTrans.AccountType = LedgerJournalACType::Ledger;
    container container accCon = [debitAccount, debitAccount,5,"ADepartment",dep,"BDivisionals",divi,"CRegional",region,"DCity",city,"EArea",area];
    //journalTrans.LedgerDimension = AxdDimensionUtil::getLedgerAccountId(accCon);
    journalTrans.AmountCurDebit = _totalAmount;
    journalTrans.AmountCurCredit = 0;
    journalTrans.Txt = strfmt("%1-Reclassfication",_localDialogPurchId);
    journalTrans.CurrencyCode =  "SAR";
    journalTrans.doinsert();
    
    //Credit
    LedgerJournalTrans journalTrans1;
    offSetCon = [creditAccount, creditAccount, 5,"ADepartment",dep,"BDivisionals",divi,"CRegional",region,"DCity",city,"EArea",area];
    journalTrans1.JournalNum = journalTable.JournalNum;
    journalTrans1.LineNum =  LedgerJournalTrans::lastLineNum(journalTrans.JournalNum)   1;
    journalTrans1.TransDate = DateTimeUtil::getSystemDate(DateTimeUtil::getUserPreferredTimeZone());
    journalTrans1.AccountType = LedgerJournalACType::Ledger;
    journalTrans1.CurrencyCode =  "SAR";
    //journalTrans1.LedgerDimension = AxdDimensionUtil::getLedgerAccountId(offSetCon);
    journalTrans1.AmountCurDebit = 0;
    journalTrans1.AmountCurCredit = _totalAmount;
    
    journalTrans.Txt = strfmt("%1-Reclassfication",_localDialogPurchId); 
    journalTrans1.doinsert();
    
    //--------------------------------------
    //for updating journal Num into PurchTable
    PurchTable updatePurchT;
    select forUpdate updatePurchT
    	where updatePurchT.PurchId == _localDialogPurchId;
    	
    if (updatePurchT)
    {
    	ttsBegin;
    	updatePurchT.JournalNum = "" ;//journalTable.ledgerJournalTable().JournalNum;
    	updatePurchT.doupdate();
    	ttsCommit;
    }
    
    Args args = new Args();
    args.record(LedgerJournalTable::find(journalTable.JournalNum));
    new MenuFunction(MenuItemDisplayStr(LedgerJournalTable3),MenuItemType::Display).run(Args);

    It seems to me that the relevant code is just this:

    PurchTable updatePurchT;
    select forUpdate updatePurchT
    	where updatePurchT.PurchId == _localDialogPurchId;
    	
    if (updatePurchT)
    {
    	ttsBegin;
    	updatePurchT.JournalNum = "" ; //journalTable.ledgerJournalTable().JournalNum;
    	updatePurchT.doupdate();
    	ttsCommit;
    }

    Why are you doing that?

    By the way, I see a plenty of other problems in your code. For example, you don't initialize LedgerJournalTable from journal name, don't initialize LedgerJournalTrans from LedgerJournalTable, forget to call validateWrite(), calling doinsert() instead of instert() on journal tables isn't correct (you'll be skipping businss logic by that), you don't insert records in a transaction and so on. This may lead to to many bugs and even data corruption in your production system.

  • Riyas ahamed F Profile Picture
    1,215 on at

    Thanks for your time sir, and you're code interesting for me and again thanks sir. :)  

  • Riyas ahamed F Profile Picture
    1,215 on at

    Sir, its working fine header and line created but I'm facing this error after creating the record in general journal.

    pastedImagebase640-_2800_1_2900_.png

    Also I did debug I didn't find any issue and what can be issue ?

  • Martin Dráb Profile Picture
    237,967 Most Valuable Professional on at

    It seems that you miss a currency somewhere.

    It surely can be debugged. For example, put a breakpoint to Info.add() to stop execution when the error is thrown. Then you can check where it's thrown from and why.

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
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 429 Most Valuable Professional

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 239 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans