Wednesday 19 July 2017

Dynamics NAV 2016 Applying and posting entries in code



Hello, i was doing some customization then i realized i had to post an application of a payment against an invoice in code. I felt the urge to share with someone there that may be facing a hurdle doing so. Just follow these easy steps.

  • Search and found the invoice record in the customer Ledger Entry table
  • Once you have find the invoice entry from the customer ledger entry table, Search for the payment entry as well from the customer ledger entry table
  • Edit and update the Invoice ledger Entry you found above
  • Set Applies to ID
  • Post Application
Here is a  sample code to achieve that;


  CustomerLdgEntryInv.RESET;//CustomerLdgEntryInv is a record variable of table 21
  CustomerLdgEntryInv.SETRANGE(CustomerLdgEntryInv."Document Type",      CustomerLdgEntryInv."Document Type"::Invoice);//Find only Invoices
 CustomerLdgEntryInv.SETRANGE(CustomerLdgEntryInv.Open, TRUE);// Find only Open entries    to apply
  IF CustomerLdgEntryInv.FINDFIRST THEN BEGIN
    // Search for Payment entries in customer ledger entries
      CustomerLdgEntry.RESET;//CustomerLdgEntryInv is a record variable of table 21
      CustomerLdgEntry.SETRANGE(CustomerLdgEntry."Document Type",    CustomerLdgEntry."Document Type"::Payment);// Find the payment
      CustomerLdgEntry.SETRANGE(CustomerLdgEntry.Open, TRUE);// Find the payment Ledger Entry
      IF CustomerLdgEntry.FINDFIRST THEN REPEAT//Repeat for all the payment entries
          //set applies to ID
          CustomerLdgEntry.CALCFIELDS(Amount);
      //Update all the invoice entries
          CustomerLdgEntryInv.CALCFIELDS(Amount);
          CustomerLdgEntryInv."Applying Entry" := TRUE;
          CustomerLdgEntryInv."Applies-to ID" := USERID;
          CustomerLdgEntryInv.CALCFIELDS("Remaining Amount");
          CustomerLdgEntryInv.VALIDATE("Amount to Apply", CustomerLdgEntryInv."Remaining Amount");
          CODEUNIT.RUN(CODEUNIT::"Cust. Entry-Edit", CustomerLdgEntryInv);
          COMMIT;// Commit the change
          SetAppliesToID.SetApplId(CustomerLdgEntry,CustomerLdgEntryInv,USERID);// SatAppliesToID is a codeUnit variable of Code Unit 101
          // Post the application
          PostAppn.Apply(CustomerLdgEntryInv,SalesInvoiceHeader."No.",SalesInvoiceHeader."Posting Date"); // PostAppn is a codeUnit variable of CodeUnit 226
      UNTIL CustomerLdgEntry.NEXT = 0;
  END;

I would like to hear your opinion of this blog and article. Please leave a comment
Best regards
      Author

Thursday 13 July 2017

Dynamics NAV Copy Document using Code




Hello, I was working on a project and i needed to copy a posted sales Invoice into a credit memo and post.
You may be trying to do the same. Let me save you the hurstle. Here's what you need to do.
You need to create a sales credit memo using the INIT function.Remember the INIT function ignores
the primary key. So VALIDATE the primary key for your credit memo
VALIDATE Sell-to Customer No. for your Credit memo
Here is a sample
SalesCreditMemo.INIT;// Initialises the Credit memo(SalesCreditMemo is a Record of table 36)
SalesCreditMemo.VALIDATE(SalesCreditMemo."No.",CrdMemoNo);//CrdMemoNo is a variable holding the Document No for the Sales Credit Memo
SalesCreditMemo.VALIDATE(SalesCreditMemo."Document Type",SalesCreditMemo."Document Type"::"Credit Memo");
SalesCreditMemo.VALIDATE(SalesCreditMemo."Sell-to Customer No.",[Put here your Customer No]);
SalesCreditMemo.INSERT;
Copy the lines from the posted sales Invoice
Here is a sample
//copy the Sales Invoice to the credit memo you have just made.
CopySalesDoc.SetSalesHeader(SalesCreditMemo);//CopySalesDoc is a Variable of Report 292
//CopyDocMgt is a variable of CodeUnite 6620
CopyDocMgt.SetProperties(TRUE,FALSE,FALSE,FALSE,FALSE,SalesSetup."Exact Cost Reversing Mandatory",FALSE);// SalesSetup is a Variable of Record 311
CopyDocMgt.CopySalesDoc(DocumentType::"Posted Invoice", [Put here Posted Sales Invoice Document No.],SalesCreditMemo);
//Post the credit memo
CLEAR(SalesPost);// SalesPost is a variable of CodeUnit 80
SalesPost.RUN(SalesCreditMemo);// This will post your Sales credit memo


************************************************************************************************
I would like to hear your feedback about this article. Please leave a comment.
Thank you
          Author

Monday 3 July 2017

Dynamics NAV Account Schedules


Hello, you may need a special report from your chart of accounts data but can't get one from the generic reports provided along with Dynamics NAV. The best alternative is not coding your own report but instead creating an account schedule. Account schedules in Dynamics NAV give you the power to define your own reports from Chart of Accounts data without writing any code. In fact you will not need technical knowledge to create one.
Click this for a step by step procedure . Click here for a you tube video about the same