Wednesday 22 November 2017

Dynamics NAV Document Posting in CSIDE/CAL

Hello there, as you work around in Dynamics NAV, there might be a time where you need to create and post for example a sales order in code. If you are new to Dynamics NAV, this might appear a daunting task. In the next few minutes, i will take you through the process. First a briefing.
Posting is the process where the system captures the information in the document and uses it to update the Sub Ledgers and General Ledger.
For example, if the sales order contains an item on the line. The system will update the item ledger entries, Vendor ledger entries, VAT account and Receivables Account. You can see the details when you navigate a customer ledger entry after posting.

All the posting in the system is achieved from Journals and that's the work of a document posting routine, to create journal entries which are posted to the various ledgers
So to post your sales order, simply create the document and call the posting routine. Posting routine reside in code units. Here is how it's done.

Note
I will assume you have already created a table to handle document data which you will then process into invoices using the posting routine.
I will also assume you have a page linked to the table above where users enter data and run the posting routine.

Your document posting routine should look like this

// SalesHeader is a record variable of table 36

//SalesLine is a record variable of table 37
//SalesPost is a codeunit variable of codeunit 80
//Invoices is a table where the document data is stored

SalesHeader.INIT; // Initializes the document & a document No. is given
        CLEAR(SalesHeader);

//Creating the invoice Document
        SalesHeader."Document Type" := SalesHeader."Document Type"::Invoice;
        SalesHeader.VALIDATE("Document Date",WORKDATE);
        SalesHeader.VALIDATE(SalesHeader."Posting Date",WORKDATE);
        SalesHeader.VALIDATE(SalesHeader."Sell-to Customer No.",Invoices."Student ID");

// This hides the dialog that prompts users to post
        SalesHeader.SetHideValidationDialog(TRUE);
        SalesHeader.VALIDATE(SalesHeader."Bill-to Customer No.",Invoices."Bill to No.");
        SalesHeader.VALIDATE(SalesHeader."VAT Bus. Posting Group",'DEFAULT');
        SalesHeader."Posting No. Series" := '';
        SalesHeader."Posting No." := 'TEST0001';// No for Sales header
             
        SalesHeader.INSERT(TRUE);// Inserts the record into Table 36
        // Creating the Sales Lines
        LineNo += 100000;
        SalesLine.INIT;
        CLEAR(SalesLine);
        SalesLine."Document Type"         := SalesLine."Document Type"::Invoice;
        SalesLine."Document No."          := SalesHeader."No.";
        PatientInvoiceNo                  := SalesHeader."No.";
        SalesLine."Line No."              := LineNo;
        SalesLine.VALIDATE("Sell-to Customer No.",SalesHeader."Sell-to Customer No.");
        SalesLine.VALIDATE(Type,Invoice.Type::"G/L Account");
        
        SalesLine.VALIDATE(Quantity,1);
        SalesLine.VALIDATE("Unit Price",Invoices.Price);// 
        SalesLine.VALIDATE(SalesLine."VAT Bus. Posting Group",'DEFAULT');
        SalesLine.VALIDATE(SalesLine."VAT Prod. Posting Group",'DEFAULT');
        SalesLine.INSERT(TRUE);

//Clears the posting codeUnit (80) of any data
        CLEAR(SalesPost);

// Runs the Document posting codeUnit 80 which posts the invoice document
        SalesPost.RUN(SalesHeader);// This call to the posting routine actually executes the posting


Good Luck

No comments:

Post a Comment