Cust. Ledger Entry get record problem in code Unit/ Close all open payments on button click

MubasherMubasher Member Posts: 25
Hello Everyone,
I have close all opened payments on one button click. My custom codeunit will check all opened payment records and deduct the payment amount from the sale invoices against the same customer. If sales invoice remaining amount become zero than that sale invoice should close.
I write my custom codeunit to solve this problem but issue is that when i get the record from cust. Ledger Entry table the amount against the customer appears zero.

Name DataType Subtype Length
Cust_led Record Cust. Ledger Entry
CustomerNo Code 20
Amount Decimal

Cust_led.FINDSET;
IF (Cust_led.FIND('-')) THEN REPEAT

IF (Cust_led.Open = TRUE) THEN BEGIN

CASE Cust_led."Document Type" OF
Cust_led."Document Type"::Payment:
BEGIN

CustomerNo := Cust_led."Customer No.";
Amount := Cust_led.Amount;
MESSAGE(FORMAT(Amount));
Cust_led.Open := FALSE;
IF (Cust_led.FIND('-')) THEN REPEAT
IF(Cust_led.Open = TRUE) THEN
IF(Cust_led."Customer No." = CustomerNo) AND (Amount < 0) THEN BEGIN
CASE Cust_led."Document Type" OF
Cust_led."Document Type"::Invoice:
BEGIN
Cust_led."Remaining Amount" := Cust_led."Remaining Amount" + Amount;
IF(Cust_led."Remaining Amount" <= 0) THEN BEGIN
Cust_led.Open := FALSE;
Amount := Cust_led."Remaining Amount";
MESSAGE('Payment apply');
END
END
END
END
UNTIL Cust_led.NEXT = 0;
MESSAGE('Payment Close');
END
END
END
UNTIL Cust_led.NEXT = 0;

I write this custom codeunit. Anyone can guide me where am i doing wrong? or any better approach to solve this task?

Answers

  • SanderDkSanderDk Member Posts: 497
    You need to add a calcfields :smile:
    Cust_led.calcfields(Amount);
    For help, do not use PM, use forum instead, perhaps other people have the same question, or better answers.
  • JuhlJuhl Member Posts: 724
    Sorry for this statement.
    But if you don’t know calcfields, then don’t write to ledgers.
    Fill journals and post them. Don’t mess with ledger tables!!! Unless you really know what you are doing.
    Follow me on my blog juhl.blog
  • MubasherMubasher Member Posts: 25
    Thanks Juhl for comment.
    Can you guide me how can i fill the journals and post them using codeunit by one button click. If you know any web link for help please mention in comment.
    Thanks
  • JuhlJuhl Member Posts: 724
    Just document how you would do it manually. Which fields, and in what order.
    Then do the same on the journal using Insert/Modify.
    Follow me on my blog juhl.blog
  • MubasherMubasher Member Posts: 25
    Can i modify flowfields data in codeunit?

    Cust_led."Remaining Amount" := Amount;
    Cust_led."Remaining Amt. (LCY)" := Amount;
    Cust_led.Open := FALSE;
    Cust_led."Closed by Entry No." := EntryNo;
    Cust_led."Closed by Amount" := Cust_led.Amount;
    Cust_led."Closed by Amount (LCY)" := Cust_led.Amount;
    Cust_led."Applying Entry" := TRUE;
    Cust_led."Closed at Date" := TODAY;
    MESSAGE('Payment Close');
    Cust_led.MODIFY;

    I am using this code but the flow fields are not going to change
  • JuhlJuhl Member Posts: 724
    You cant change flowfields. They are based on calculations.
    Follow me on my blog juhl.blog
  • MubasherMubasher Member Posts: 25
    edited 2018-12-20
    Ok
    Can you share any document to post the journals manually?
    or steps i should follow?
  • JuhlJuhl Member Posts: 724
    Just google.

    But it is important to learn NAV before you can code NAV !
    Follow me on my blog juhl.blog
  • MubasherMubasher Member Posts: 25
    edited 2018-12-20
    I know how to code in Nav and i have 1 year experience in Nav development
Sign In or Register to comment.