Search code examples
dynamics-ax-2009axapta

how to create a new record in form datasource from x++


In form Journal Voucher (AR>Journal> PaymentJournal> clicking buttonLines). I want to create a new record from x++ code.

I have seen few methods in the form viz create(), initvalue(), ledgerJournalEngine_custPayment... etc which are called when we press ctrl+n . How we could use these methods through x++ code to create a record using standard functionality.

plz help.


Solution

  • Before you elaborated, I thought you were trying to create your own custom form extending the journal functionality. If you're just trying to create a tool, you can just create a new Settlement Using Cust Group button. In the clicked event, call your transaction marking form or whatever you do to get the transactions you want to use. Then put something like this in it:

    void clicked()
    {
        ;
    
        element.lock();
    
        super();
    
        // Put your code here to call the open transaction editing code
    
        // CREATE THIS CUSTOM METHOD on C\LedgerJournalEngine_CustPayment\settleTransCustGroup
        ledgerJournalEngine.settleTransCustGroup(ledgerJournalTable);
    
        ledgerJournalTrans_ds.active();
        ledgerJournalTrans_ds.reread();
        ledgerJournalTrans_ds.executeQuery();
    
        //recalculate balances
        ledgerJournalEngine.newJournalActive(ledgerJournalTable, true);
        element.firstField();
        element.unLock();
    }
    

    Then in the new method you created, which I named settleTransCustGroup, you can loop over your records in the testLedgerJournalSpecTrans modeling off of something similar to this (custom method created on the engine class):

    void settleTransCustGroup(LedgerJournalTable    _ledgerJournalTable)
    {
        LedgerJournalTrans      ledgerJournalTrans;
        ;
        // Turn this stuff into a loop and default whatever else you need
        ledgerJournalTrans.clear();
        ledgerJournalTrans.initValue();
        ledgerJournalTrans.AccountNum = '100003';
        ledgerJournalTrans.AmountCurCredit = 10;
        this.initValue(ledgerJournalTrans);
        ledgerJournalTrans.insert();
        this.write(ledgerJournalTrans);
    
        ledgerJournalTrans.clear();
        ledgerJournalTrans.initValue();
        ledgerJournalTrans.AccountNum = '100005';
        ledgerJournalTrans.AmountCurCredit = 15;
        this.initValue(ledgerJournalTrans);
        ledgerJournalTrans.insert();
        this.write(ledgerJournalTrans);
    }