Create 100's Sales Invoice Documents Programmatically.

Hi All,
I would like to create a hundreds of Sales Invoices Documents Programmatically. I do have Customer and Item information( stored on my tables) to used to create a new Sales Invoices Document. Is there a easy way to call something to create the Sales Invoices and it will calculate taxes, Invoice Details, Shipping and Billing, etc? to have the Sale Invoice Document ready to be posted?
Thank you.

Simply create a DO WHILE loop around the action on the page.

Thank you. May be I did not explain myself good. Every Sale Invoice has different Customer and Items. I have to read from my tables the Customer and Items information. I am looking for something like Import Sales Invoice Documents.


do you have the tables in Excel files? And what version of NAV are you using?

Hi Thomas,
No I do not have tables in Excel and I am using NAV 2017. It is similar to the NAV Service Contract where you have to create Invoices based on the Contract. I am looking for a “easy way” to create Sales Invoice Documents.

If you’re new to NAV development, then creating invoices is not the most easy part. But do as most developers does, use copy-paste. Find similar “patterns” with-in NAV, and then more or less copy what they do. We have always done it, but today they refer to it as “design patterns”. So really what I’m saying is start study the code in NAV with “re-use” in mind. Study the design-patterns on the Design Patterns website.

In your situation I would suggest, either the standard service contract invoicing, or maybe job invoicing. You first need to create the unposted sales invoice and lines (table 36 and 37), before you can use the posting codeunits to post the invoices (creating g/l entries and posted sales invoices etc.). If you have never tried with sales headers and lines, then study the code closely.

Ask again if you have specific issues. [:)]

Thank you Erik,
Yes, I am new to NAV. I will study the standard service contract invoicing and the job invoicing unit code. Thank you.


i don’t think there is any standard way of doing this. You have to develop your own code to go thru records and write them into Sales Header and Sales Line. When you write the record into the table you have to use the VALIDATE() function so bussines logic is applied. After inserting the record you can post the Invoices with the Post Batch action.


This would be in 2 steps.

You also can automate the posting but I would not recommend it because if you have some errors then whole transaction will be rolled back and it will also block the related tables.

Better have 2 buttons than one.

best regards,

Correct Thomas. Two buttons are better than one. But three are even better. [;)]

I would have a “Test-button”, a “Create-button” and eventually a “Post-button”. Most often if I create invoices from a batch, then I don’t use a special “Post-Batch” job, as the invoices are posted using the standard invoicing functionality.

Most users love that they are able to do a pre-invoicing test before they create the actual invoices. Design it so that it can also be run when actually creating the invoices. Just like the standard posting design patterns. There is almost always a pattern that can be reused, even if it has to be build for different existing.

Thank you Thomas. Yes the idea is just to create Sales Invoices and then NAV Post Batch will take care of the posting. I do not know about the VALIDATE() function but I will use it to validate Invoice HDR and Lines.

Thank you Erik. I like the idea of pre-invoicing test before they create the actual invoices. After they create the invoices NAV will take care of the rest. I do have Invoice HDR and Line Information ( Customer and default information from the customer and for the line I do have Item No., UofM, Price, Amount, and Qty) but I want NAV to calculate Taxes, Price discount, distribution, Dimension, etc.etc

Do you have this header and line information in NAV already, or creating it on the spot? It really depend on what context your invoice job is. Typically one kind of line items, combined with the use of item no.s and a customer no. “Plot” that into NAV sales headers and lines, and it will use this data and calculate taxes, discounts etc., like with normal invoices.

Yes, I do have my own NAV tables (HDR and Line Tables) where I have Customers (HDR information just the required fields ) and Line( Item information required fields). Now I would like to read my own HDR and Line tables and create many Sales Invoices Document.

Well, then your job is pretty much like the create job invoices function. Here you have job planning lines and the job, and creates invoices or credit memos.

Thank you Erik. I will look into job invoices function.
Thank you

Hi Erik,
Basically I have to understand CodeUnit 1002 and “re-use” it. Do you know the Codeunit for the standard service contract invoicing?

Basically yes. I would use the “patterns” in this codeunit, not the service contracts. Think the job postings are a little more “standard NAV” than the service module. Easier to turn around to something else.