Updating record when opening a Form

RodericksRodericks Member Posts: 6
Hi all!!

I need to update some records in a table when I open a form, I put some code on the OnAfterGetCurrRecord() trigger:


BankAccLedger.SETCURRENTKEY("Bank Account No.");
BankAccLedger.SETRANGE("Bank Account No.","Bank Account No.");
BankAccLedger.SETRANGE("Posting Date",01011900D,"Statement Date");
BankAccLedger.CALCSUMS(Amount);
"Bank Ending Balance" := BankAccLedger.Amount;

Difference := "Statement Ending Balance" - "Bank Ending Balance";
BankAccLedger.MODIFY;


but we I run it, I get this error:

"You cannot make any changes in the database until a transaction has been started"

It seems that the MODIFY instruction is not allowed here.

I have unsuccessfully tried this in every trigger form the form :(


How can I update the record when opening the form?


Thanks-
Rod.

Comments

  • Marije_BrummelMarije_Brummel Member, Moderators Design Patterns Posts: 4,262
    Why would you want to write code like this?

    a. Your bank account balance should Always match your ledger
    b. If you want to do some calculation, why do you want to save them in the database like this?

    In Dynamics NAV we have transactions in validation triggers and codeunit processes.

    My suggestion would be to investigate best practices in how to develop in Microsoft Dynamics NAV

    I'm doing a virtual introduction to NAV in two weeks.

    http://decisions.msdynamicsworld.com/se ... ion-design
  • kapamaroukapamarou Member Posts: 1,152
    Rodericks wrote:
    BankAccLedger.SETCURRENTKEY("Bank Account No.");
    BankAccLedger.SETRANGE("Bank Account No.","Bank Account No.");
    BankAccLedger.SETRANGE("Posting Date",01011900D,"Statement Date");
    BankAccLedger.CALCSUMS(Amount);
    "Bank Ending Balance" := BankAccLedger.Amount;

    Difference := "Statement Ending Balance" - "Bank Ending Balance";
    BankAccLedger.MODIFY;

    To which table does "Bank Ending Balance" belong?
    You are modifying BankAccLedger without a FIND command?

    I agree with Mark. You should reconsider where your code should be located.
Sign In or Register to comment.