Customized reversed code in Dynamics NAV 2018

Hi Team,

I wrote the below code for reversal which is working fine but after the reverse action it display this “The journal lines were successfully posted. You are now in the TB-0000029 journal.” I want it to display “The journal lines were successfully reversed.” I also wanted to check the Reversed in the G/L entry.

WITH TBill DO BEGIN
IF CONFIRM(Text001+' '+"No."+' ?') = TRUE THEN BEGIN  
  IF TBill.Reversed THEN
    ERROR(Text002,"PFA Name");
    
  CMSetup.GET();
  //Delete Lines Present on the General Journal Line
  GenJnLine.RESET;
  GenJnLine.SETRANGE(GenJnLine."Journal Template Name",CMSetup."Payment Voucher Template");
  GenJnLine.SETRANGE(GenJnLine."Journal Batch Name","No.");
  GenJnLine.DELETEALL;

  Batch.INIT;
  IF CMSetup.GET() THEN
    Batch."Journal Template Name" := CMSetup."Payment Voucher Template";
  Batch.Name := "No.";
  IF NOT Batch.GET(Batch."Journal Template Name",Batch.Name) THEN
    Batch.INSERT;

  Lines.RESET;
  Lines.SETRANGE("No.","No.");
  IF Lines.FINDSET THEN BEGIN REPEAT //Name: IB, Date:04/09/2019
  instrumentNo := COPYSTR(Lines."Instrument No.",1,20);
   
    LineNo := LineNo + 1000;
    //Post bank Entries
    GenJnLine.INIT;
    GenJnLine."Journal Template Name" := CMSetup."Payment Voucher Template";
    GenJnLine."Journal Batch Name" := "No.";
    GenJnLine."Line No." := LineNo;
    GenJnLine."Account Type" := GenJnLine."Account Type"::"G/L Account";
    IF InvestmentAccountSetup.GET(PFA,Fund,"Type of Investment") THEN
      GenJnLine."Account No." := InvestmentAccountSetup."Asset Account";
    GenJnLine."Posting Date" := Lines."Settlement Date";
    GenJnLine."Document No." := instrumentNo;
    GenJnLine.Description := Lines."Bill Series Code";
    GenJnLine.Amount := -Lines.Consideration;
    GenJnLine.VALIDATE(GenJnLine.Amount);
    GenJnLine."Bal. Account Type" := GenJnLine."Bal. Account Type"::"Bank Account";
    GenJnLine.VALIDATE("Bal. Account No.","Bank Account");
    GenJnLine."External Document No." := "No.";
    GenJnLine.VALIDATE(GenJnLine."Shortcut Dimension 1 Code",PFA);
    GenJnLine.VALIDATE(GenJnLine."Shortcut Dimension 2 Code",Fund);
    IF GenJnLine.Amount <> 0 THEN
       GenJnLine.INSERT;

    UNTIL Lines.NEXT = 0;
    END;

    CODEUNIT.RUN(CODEUNIT::"Gen. Jnl.-Post",GenJnLine); // This is what is doing it.

    GLEntry.RESET;
    GLEntry.SETRANGE(GLEntry."Journal Batch Name","No.");
    GLEntry.SETRANGE(GLEntry.Reversed,FALSE); 
    IF GLEntry.FINDFIRST THEN BEGIN

      Lines.RESET;
      Lines.SETRANGE("No.","No.");
      IF Lines.FINDSET THEN BEGIN REPEAT
        IF Lines."Transaction Type" = Lines."Transaction Type"::Buy THEN BEGIN
        
        //Ledger
        //Reverse Previous Values
        TBillLedger.RESET;
        TBillLedger.SETRANGE("Bill Series Code",Lines."Bill Series Code");
        TBillLedger.SETRANGE(PFA,PFA);
        TBillLedger.SETRANGE(Fund,Fund);
        TBillLedger.SETRANGE("Instrument No.",Lines."Instrument No.");
        IF TBillLedger.FINDSET THEN
          TBillLedger.MODIFYALL(Reversed,TRUE,TRUE);

        TBillLedgerOld.RESET;
        IF TBillLedgerOld.FINDLAST THEN
          EntryNo := TBillLedgerOld."Entry No" +1
        ELSE
          EntryNo := 1;

        TBillLedger.INIT;
        TBillLedger."Entry No" := EntryNo;
        TBillLedger.VALIDATE("Bill Series Code",Lines."Bill Series Code");
        TBillLedger.PFA := Lines.PFA;
        TBillLedger.Fund := Lines.Fund;
        TBillLedger.VALIDATE("Instrument No.",Lines."Instrument No.");
        TBillLedger."Transaction Type" := TBillLedger."Transaction Type"::Acquisition;
        TBillLedger.Consideration := -Lines.Consideration;
        TBillLedger."Face Value" := -Lines.Principal;
        TBillLedger."Discounted Value" := -Lines."Discounted Amount";
        TBillLedger."Trade Date" := Lines."Trade Date";
        TBillLedger.VALIDATE("Value Date",Lines."Trade Date");
        TBillLedger.Reversed := TRUE;
        TBillLedger."Reversed By" := USERID;
        TBillLedger."Reversed Date" := TODAY;
        TBillLedger.INSERT(TRUE);

        //TBill Investment
        //Delete Previous Values
        TBills.RESET;
        TBills.SETRANGE("Bill Series Code",Lines."Bill Series Code");
        TBills.SETRANGE(PFA,PFA);
        TBills.SETRANGE(Fund,Fund);
        TBills.SETRANGE("Transaction No.",Lines."Instrument No.");
        IF TBills.FINDSET THEN 
          TBills.DELETEALL;
        END;
        UNTIL Lines.NEXT = 0;
        COMMIT;
        END;
        Reversed := TRUE;
        "Reversed By" := USERID;
        "Reversed Date" := TODAY;
        MODIFY;
END;
END;
END;
//End

Thanks.

do you need to post the bank entries and reverse the custom entries in one go?

No at all. When its posting code, the amount is positive while reverse code, the amount is negative. Thanks

If you want to handle user dialogs in your own code, you should call codeunit “Gen. Jnl.-Post Batch” instead of “Gen. Jnl.-Post”. The way it is done in your code, confirmation will be requested twice - first time your custom CONFIRM, then the one in the Gen. Jnl.-Post codeunit. Must be a bit irritating for the user. If you replace the call, you can show any dialogs you want.

The message you want to get rid of, is shown after posting a journal batch which has a number in its code. If you post a journal batch “TB-0000028”, it is replaced with a new one “TB-0000029” after posting. So keep this in mind when you post journal lines.

Not sure how you want to check the Reversed in G/L Entry and why you can’t do this now. But there is a question to your code in its current state - why are you committing the transaction before modifying the “Reversed” field in the TBill record? This can lead to data inconsistency if the last MODIFY fails. Besides, codeunit “Gen. Jnl.-Post Batch” (which is called from “Gen. Jnl.-Post”) has a COMMIT itself. It is possible that you post reversal entries, while the respective TBill record will not be marked as Reversed.

I appreciated your response. I got all your points. TBill record is marked as Reversed.

In the G/L Entry, there is a field called Reversed, I want to check its TRUE when I post a reversal entries. But I wrote something like this below with error message while on super permission set " You don’t have the following permission on the TABLEDATA: G/L Entry.

GLEntry.RESET;
    GLEntry.SETRANGE(GLEntry."Journal Batch Name","No.");
    GLEntry.SETRANGE(GLEntry.Reversed,FALSE); 
     IF GLEntry.FINDSET THEN BEGIN
      GLEntry.MODIFYALL(Reversed,TRUE,TRUE);
      or 
      GLEntry.Reversed := TRUE;

What I wanted to achieve is that, when I click Reverse button on the chart of account, it reversed with posting negative value and also check the reversed field to TRUE.

Thanks

Ok, I understand you want to change the value of the field in the G/L Entry. This code fails because direct modifications of the G/L Entry table are not allowed by the license. You could modify your object and add indirect permissions. You need to change the property Permissions to the following value.

TableData G/L Entry=rm

But… I strongly recommend NOT doing this. Tweaking with G/L entries is always a huge headache in the long run. It would be much better to create a custom table with 1-1 relationship to G/L Entry and keep your own “Reversed” checkmark separated from the base application table.

I appreciated I have a table in relationship with G/L Entry where are keep my Reversed.