X++ Code to Create Purchase Order and Post the Invoice.

Following Job creates the Purchase order from code and post the invoice by making use of PurchFormLetter class.
If you don't have demo data , please test with your input values.


static void Dev_CreatePO_and_Invoice(Args _args)
{

NumberSeq numberSeq;
Purchtable Purchtable;
PurchLine PurchLine;
PurchFormLetter purchFormLetter;

;

ttsbegin;
numberSeq = NumberSeq::newGetNumFromCode(purchParameters::numRefPurchaseOrderId().NumberSequence,true);

// Initialize Purchase order values
Purchtable.initValue();
Purchtable.PurchId = numberSeq.num();
Purchtable.OrderAccount = '3000';
Purchtable.initFromVendTable();

if (!Purchtable.validateWrite())
{
throw Exception::Error;
}
Purchtable.insert();

// Initialize Purchase Line items
PurchLine.PurchId = Purchtable.PurchId;
PurchLine.ItemId = 'B-R14';
PurchLine.createLine(true, true, true, true, true, false);
ttscommit;

purchFormLetter = purchFormLetter::construct(DocumentStatus::Invoice);
purchFormLetter.update(purchtable, // Purchase record Buffer
"Inv_"+purchTable.PurchId, // Invoice Number
systemdateget()); // Transaction date


if (PurchTable::find(purchTable.PurchId).DocumentStatus == DocumentStatus::Invoice)
{
info(strfmt("Posted invoiced journal for purchase order %1",purchTable.PurchId));
}
}

Change the documentstatus to packingSlip , if you want to post packing slip.
Enjoy ....Invoicing through Code.

3 comments:

  1. If you just want to post the packing slip for a specific line in the purchTable, do you pass the purchLine as the common record in the update method?

    ReplyDelete
  2. As such we don't have provision to pass purchLine as parameter to Update Method.
    To update packing slip for One item
    1) In purchLine , specify the Receive now qty for that item (Field , PurchReceiveNow).
    2) Pass PurchUpdate::ReceiveNow parameter to update method.

    PurchUpdate will decide whether to update for all items,for single items, for packing slips. So while creating Purchase order , just specify receive now qty in purchline for those items which you want to update packingslip.
    I hope above explanation will clarify your doubt, if not plz comment.

    ReplyDelete
  3. How Can I post an invoice from a Packing Slip and it from many Purchase orders?

    ReplyDelete

Note: Only a member of this blog may post a comment.