Integrating with financial management

Although it is not likely to make big changes in financial management, it might be necessary to create G/L Entries in a new posting routine.

In the previous chapter, we already pointed out briefly that during posting transactions in Microsoft Dynamics NAV, the actual journal line records are never really inserted in the database. They are used as temporary containers to hold the data during posting. Doing an actual INSERT would require defining a journal template name, journal batch name, and line no. and could cause locking in the database.

Let's create a new codeunit that will create a G/L transaction.

Creating a G/L transaction

After creating the codeunit, we need to set up the two variables that are the minimum requirement to post something to the general ledger.

Creating a G/L transaction

The preceding screenshot shows two variables:

  • GenJnlLine: This is a reference to the General Journal Line table (81).
  • GenJnlPostLine: The Gen. Jnl.-Post Line Codeunit (12) creates the G/L Entries, the register, and the other financial entries.

The C/AL code

Creating a new G/L Entry requires some of mandatory fields. All the other fields in the general journal line are either optional for basic entries or mandatory in combination with more advanced postings, as we will find out later.

We will start by writing this code to the OnRun trigger, as follows:

OnRun() 
GenJnlLine.INIT;
GenJnlLine."Posting Date" := WORKDATE;
GenJnlLine.Description := 'Test Entry';
GenJnlLine."Document No." := 'PACKT';
GenJnlLine."Account No." := '6120';
GenJnlLine.Amount := 100;
GenJnlPostLine.RunWithCheck(GenJnlLine);

If we execute this C/AL code, we will receive the following error message, which indicates that our transaction will result in an unbalanced chart of accounts:

The C/AL code

We can fix this by creating a balance transaction for -100 in the same OnRun trigger, as follows:

GenJnlLine.INIT;
GenJnlLine."Posting Date" := WORKDATE;
GenJnlLine.Description := 'Test Entry';
GenJnlLine."Document No." := 'PACKT';
GenJnlLine."Account No." := '6120';
GenJnlLine.Amount := -100;
GenJnlPostLine.RunWithCheck(GenJnlLine, TempJnlLineDim);

After executing the codeunit, we can navigate on our document no. to see the G/L Entries we created:

The C/AL code

This was a very simple example of how to integrate with financial management; let's create a more advanced example.

Advanced entries

We will create a new customer ledger entry with dimensions. To do this, we should change one of the C/AL parts we created to the following code:

GenJnlLine.INIT;
GenJnlLine."Posting Date" := WORKDATE;
GenJnlLine.Description := 'Test Entry';
GenJnlLine."Document No." := 'PACKT2';
GenJnlLine."Account Type" := GenJnlLine."Account Type"::Customer;
GenJnlLine."Account No." := '10000';
GenJnlLine.Amount := 100;
GenJnlPostLine.RunWithCheck(GenJnlLine);

But when we execute this C/AL code, we receive the following error message:

Advanced entries

This means we need to implement dimensions. Let's add the following C/AL code to the routine:

...
GenJnlLine.Amount := 100;
GenJnlLine."Dimension Set ID" := 3;
GenJnlPostLine.RunWithCheck(GenJnlLine, TempJnlLineDim);

This will use dimension set entry 3, which contains the dimensions that are required for this transaction.

Tip

This article on MSDN at http://msdn.microsoft.com/en-us/library/jj552498(v=nav.71).aspx explains how dimension sets are used in the Microsoft Dynamics NAV 2013 architecture.

Now, when we navigate on PACKT, we see that the system has created a Customer Ledger entry and a Detailed Cust. Ledg. Entry.

Advanced entries

Look, learn, and love

In Microsoft Dynamics NAV, there are many examples of how to integrate with financial management. The following is a list of interesting codeunits that create general journal lines:

  • Sales-Post (80)
  • Purch.-Post (90)
  • Job Calculate WIP (1000)
  • CheckManagement (367)
  • Sales-Post Prepayments (442)
  • Inventory Posting To G/L (5802)
  • Serv-Posting Journals Mgt. (5987)

Go ahead and have a look inside these codeunits to learn how Microsoft does the integration.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset