Create a code unit for creating the Purchase Invoice in Navision

Hi I wanted to create a Purchase Invoice in Navision Through Code Unit.
Can someone please help me with that.

Thank you

Look at the code for the “Copy Document” functionality. That should provide a good guide.

Can you please explain where to check that I didn’t get you

Sure, here’s an example code unit that creates a purchase invoice in Navision:

CODEUNIT 50100 “Create Purchase Invoice”

VAR
PurchaseInvoiceHeaderRec: Record Purchase Invoice Header;
PurchaseInvoiceLineRec: Record Purchase Invoice Line;
BEGIN
// Initialize Purchase Invoice Header record
PurchaseInvoiceHeaderRec.INIT;

// Set header fields
PurchaseInvoiceHeaderRec.“Document Type” := PurchaseInvoiceHeaderRec.“Document Type”::Invoice;
PurchaseInvoiceHeaderRec.“Posting Date” := TODAY;
PurchaseInvoiceHeaderRec.“Vendor No.” := ‘V00001’;
PurchaseInvoiceHeaderRec.“No.” := ‘PI00001’;

// Insert Purchase Invoice Header
PurchaseInvoiceHeaderRec.INSERT;

// Initialize Purchase Invoice Line record
PurchaseInvoiceLineRec.INIT;

// Set line fields
PurchaseInvoiceLineRec.“Document Type” := PurchaseInvoiceLineRec.“Document Type”::Invoice;
PurchaseInvoiceLineRec.“Line No.” := 10000;
PurchaseInvoiceLineRec.“No.” := PurchaseInvoiceHeaderRec.“No.”;
PurchaseInvoiceLineRec.“Posting Date” := TODAY;
PurchaseInvoiceLineRec.“Vendor No.” := ‘V00001’;
PurchaseInvoiceLineRec.“Direct Unit Cost” := 10.00;
PurchaseInvoiceLineRec.“Direct Cost” := 100.00;
PurchaseInvoiceLineRec.“Line Amount” := 110.00;
PurchaseInvoiceLineRec.“Location Code” := ‘LOC01’;

// Insert Purchase Invoice Line
PurchaseInvoiceLineRec.INSERT;
END.

This code unit creates a purchase invoice with one line item for vendor ‘V00001’, with a direct unit cost of $10.00 and a total line amount of $110.00. You can modify the code to include multiple line items or different vendors as needed.

That code is not going to work. To start with it is using the wrong tables. It needs to use “Purchase Header” and “Purchase Line”. The tables it is using are used to store posted invoice documents.

Even if the right tables were used, it also has the problem of no validation being called where it is needed. This would result in required fields (i.e. posting groups etc) not being populated. Meaning the document could not be posted. There may be more, but that should get you started.

When designing processes like this also be aware of locking duration and the impact on other users or processes. If only creating invoices with 1 or a few lines, likely not an issue. But if you plan to create documents with many lines this may be a consideration. May also depend on what the others are doing. Just don’t wait till go-live to find you have a problem.

I’m not a big fan of just handing someone the answer to a complex problem. Rather I will help to guide them to finding the answer themselves. if just handed the answer, do they really learn anything? They may be able to replicate the answer, but can they explain why that is the right way? Or better yet, recognize that it is the wrong answer for their needs?

1 Like

Yes, Actually I wanted to create this code unit using the tables Purchase Header and Purchase Line

“Scarlett_Jonathon”: This is a terrible code, that not utilize Navision!
I think validation and INSERT with parameter TRUE.
And - creating a Posted Purchace Invoice (POSTED Invoice) - that’s a NO-GO!
Look at “bbrown5962” answer.