Applying vendor ledger entries through code

MACL2MACL2 Member Posts: 10
Hi,

I need to write a batch job in NAV 2015 that creates vendor payment entries in the G/L Journal and applies them to the relevant vendor invoices. I must use ONE payment entry per vendor. This payment has to be applied to several vendor invoices (always more than 1 invoice per vendor). Normally the user would open page 233 (Apply Vendor Entries), mark all the invoices, Close the page with OK and post the Journal. Since I cannot use a page that opens in my batch job I have filled out the field "Applies-to ID" on the General Journal (vendor payment) as well as on all the vendor ledger entries (invoices) against which the payment shall be applied. I also made sure the field "Amount to Apply" on the vendor ledger entries (invoices) is filled out with the remaining amount of the relevant ledger entry. When I post the journal the payment is registered, the balance of the vendor is zero. So far so good. But - the payment and the invoices are all still open. The entries were not applied.

I have been searching/testing for hours and I can't find what I am still missing. When I mark the entries through page 233 (Apply Vendor Entries) and post, then everything is fine.

Here is the code that I used for testing:


GLJournal.RESET;
GLJournal.SETRANGE(GLJournal."Journal Batch Name",'STANDARDA');
IF GLJournal.FINDFIRST THEN BEGIN
CLEAR(VendEntry);
VendEntry.SETCURRENTKEY("Vendor No.",Open);
VendEntry.SETRANGE("Vendor No.",'K8118');
VendEntry.SETRANGE(Open,TRUE);
IF VendEntry.FINDSET THEN REPEAT
VendEntry.VALIDATE("Applies-to ID",GLJournal."Document No.");
VendEntry.CALCFIELDS("Remaining Amount");
VendEntry.VALIDATE("Amount to Apply",VendEntry."Remaining Amount");
VendEntry.MODIFY;
UNTIL VendEntry.NEXT = 0;

GLJournal.VALIDATE("Applies-to ID",GLJournal."Document No.");

CODEUNIT.RUN(CODEUNIT::"Gen. Jnl.-Post",GLJournal);
END;

I would really appreciate if somebody could help me to solve this problem.

Many thanks in advance.
MACL2

Best Answer

Answers

  • KishormKishorm Member Posts: 921
    A few things I've noticed...

    1) you are only doing this for the 1st general journal line
    2) you are filtering the vendor ledger entry on a hard coded vendor - if the vendor on the first line is different then you won't get any applications posted
    3) you are using applies-to ID so you need to set this on the general journal line(s) - same field name

    Regards
  • MACL2MACL2 Member Posts: 10
    Hi Kishorm

    Thank's for your reply. As I mentioned I used this code for testing. That's why I go only over the first G/L line and the vendor no. is hard coded. If you look at my code then you see that I am using "Applies-to ID" for the G/L line as well as for the vendor ledger entries I want to close.

    Regards
  • krikikriki Member, Moderator Posts: 9,112
    [Topic moved from 'General Chat' forum to 'NAV Three Tier' forum]

    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • MACL2MACL2 Member Posts: 10
    Hi Kishorm,

    I tried your suggestion and added "GLJournal.MODIFY" - it helped!

    Thanks a lot.

    Best regards,
    MACL2
  • raja123raja123 Member Posts: 50
    MACL2 wrote: »
    Hi,

    I need to write a batch job in NAV 2015 that creates vendor payment entries in the G/L Journal and applies them to the relevant vendor invoices. I must use ONE payment entry per vendor. This payment has to be applied to several vendor invoices (always more than 1 invoice per vendor). Normally the user would open page 233 (Apply Vendor Entries), mark all the invoices, Close the page with OK and post the Journal. Since I cannot use a page that opens in my batch job I have filled out the field "Applies-to ID" on the General Journal (vendor payment) as well as on all the vendor ledger entries (invoices) against which the payment shall be applied. I also made sure the field "Amount to Apply" on the vendor ledger entries (invoices) is filled out with the remaining amount of the relevant ledger entry. When I post the journal the payment is registered, the balance of the vendor is zero. So far so good. But - the payment and the invoices are all still open. The entries were not applied.

    I have been searching/testing for hours and I can't find what I am still missing. When I mark the entries through page 233 (Apply Vendor Entries) and post, then everything is fine.

    Here is the code that I used for testing:


    GLJournal.RESET;
    GLJournal.SETRANGE(GLJournal."Journal Batch Name",'STANDARDA');
    IF GLJournal.FINDFIRST THEN BEGIN
    CLEAR(VendEntry);
    VendEntry.SETCURRENTKEY("Vendor No.",Open);
    VendEntry.SETRANGE("Vendor No.",'K8118');
    VendEntry.SETRANGE(Open,TRUE);
    IF VendEntry.FINDSET THEN REPEAT
    VendEntry.VALIDATE("Applies-to ID",GLJournal."Document No.");
    VendEntry.CALCFIELDS("Remaining Amount");
    VendEntry.VALIDATE("Amount to Apply",VendEntry."Remaining Amount");
    VendEntry.MODIFY;
    UNTIL VendEntry.NEXT = 0;

    GLJournal.VALIDATE("Applies-to ID",GLJournal."Document No.");

    CODEUNIT.RUN(CODEUNIT::"Gen. Jnl.-Post",GLJournal);
    END;

    I would really appreciate if somebody could help me to solve this problem.

    Many thanks in advance.
    MACL2


    Same way I'm also doing , see my code
    Documentation()

    OnInitReport()
    GenJournalLine.SETRANGE("Journal Batch Name",'BANK');
    GenJournalLine.SETRANGE("Journal Template Name",'PAYMENT');

    OnPreReport()
    GenJournalLine.INIT;
    GenJournalLine."Posting Date":=020119D;
    GenJournalLine."Journal Batch Name":='BANK';
    GenJournalLine."Document Type":=GenJournalLine."Document Type"::Payment;
    GenJournalLine."Document No.":='ADI9';
    GenJournalLine."Journal Template Name":='PAYMENT';
    GenJournalLine."Account Type":=GenJournalLine."Account Type"::Vendor;
    GenJournalLine."Account No.":="Vendor no";
    GenJournalLine."Recipient Bank Account":='ECA';
    GenJournalLine."Payment Method Code":='CASH';
    GenJournalLine."Bal. Account Type":=GenJournalLine."Bal. Account Type"::"Bank Account";
    GenJournalLine."Bal. Account No.":='GIRO';
    GenJournalLine."Applies-to Doc. Type":=GenJournalLine."Applies-to Doc. Type"::Invoice;
    GenJournalLine.VALIDATE("Applies-to Doc. Type",GenJournalLine."Applies-to Doc. Type"::Invoice);


    VendorLedgerEntry.SETRANGE("Vendor No.","Vendor no");
    VendorLedgerEntry.SETRANGE("Document Type",VendorLedgerEntry."Document Type"::Invoice);
    IF VendorLedgerEntry.FINDSET THEN
    REPEAT
    VendorLedgerEntry.VALIDATE("Applies-to ID",GenJournalLine."Document No.");
    VendorLedgerEntry.CALCFIELDS("Remaining Amount");
    VendorLedgerEntry.VALIDATE("Amount to Apply",VendorLedgerEntry."Remaining Amount");
    //VendorLedgerEntry.MODIFY;
    UNTIL VendorLedgerEntry.NEXT=0;
    GenJournalLine.Amount:=-amt;


    GenJournalLine.INSERT;


    OnPostReport()
    //CODEUNIT.RUN(CODEUNIT::"Gen. Jnl.-Post",GenJournalLine);



    But I'm getting this error
    54p8m83lfa17.png
Sign In or Register to comment.