Question

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.

Was it helpful?

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);
}

OTHER TIPS

Generally, your X++ code would look something like this:

static void InsertRecord(Args _args)
{
    LedgerJournalTrans  ledgerJournalTrans;
    ;

    ledgerJournalTrans.AccountNum = "10000";
    ledgerJournalTrans.AmountCurCredit = 50.64;
    ledgerJournalTrans.AccountType = LedgerJournalACType::Ledger;
    ledgerJournalTrans.insert();
}

You can replace the fields and values as needed. If any fields are missing, the error will display in the infolog (for example, if you were to run the above, you will get a "Currency code must be specified" error), so be sure all required fields are addressed.

In most cases, you can also call ledgerJournalTrans.initValue(); before assigning your values to pre-populate the record with default AX values. I believe this will be the same as what you see when you use Ctrl + N on the form. In the above example, doing so will cause the Currency Code to be filled in, and the record to be saved correctly (at least on our system).

There is no magical way of calling standard funcionality out of the frameworks quoted here on other comments. For each Ledger Type (Accounting, Inventory, Orders, Payments, ...), the way of creating and initializing lines is different and you have to work on this specific way if you want the journal to post properly.

There are a lot of examples on google of X++ code that inserts journal transactions for almost every type of them. It's not easy, but at least it's always almost the same code and it can be easilly reused.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top