Post Purchase Invoice

Hi All,

When i Post Purchase order Invoice through code It through an error : Can not Insert Multiple records in VendInvoiceInfoTable

The Record Already Exist.

The code is given below:

NumberSeq numberSeq;

Purchtable Purchtable;

PurchLine PurchLine;

PurchFormLetter purchFormLetter;

;

ttsbegin;

numberSeq =NumberSeq::newGetNum(purchparameters::numRefPurchId(),true);

// Initialize Purchase order values

Purchtable.initValue();

Purchtable.PurchId = numberSeq.num();

Purchtable.OrderAccount =

‘OXI-016872’;

Purchtable.CurrencyCode =

‘INR’;

Purchtable.initFromVendTable();

if (!Purchtable.validateWrite())

{

throw Exception::Error;

}

Purchtable.insert();

// Initialize Purchase Line items

PurchLine.PurchId = Purchtable.PurchId;

PurchLine.ItemId =

‘285’;

PurchLine.PurchQty =

2;

PurchLine.PurchPrice =

50;

PurchLine.createLine(

true, true, true, true, true, false);

PurchLine.clear();

PurchLine.PurchId = Purchtable.PurchId;

PurchLine.ItemId =

‘1162’;

PurchLine.PurchQty =

1;

PurchLine.PurchPrice =

200;

PurchLine.createLine(

true, true, true, true, true, false);

ttscommit;

//PO confirmation

ttsBegin;

purchTable = PurchTable::find(purchTable.PurchId);

purchFormLetter = PurchFormLetter::construct(DocumentStatus::PurchaseOrder);

purchFormLetter.update(purchTable,

strFmt(“Inv_%1”, purchTable.PurchId));

ttsCommit;

//PO Product Receipt

ttsBegin;

purchFormLetter = purchFormLetter::construct(DocumentStatus::PackingSlip);

purchFormLetter.update(purchtable,

// Purchase record Buffer

“PO_”+purchTable.PurchId, // Invoice Number

systemdateget()); // Transaction date

ttsCommit;

//PO Invoice

ttsBegin;

purchFormLetter = purchFormLetter::construct(DocumentStatus::Invoice);

//purchFormLetter.update(purchtable,“Inv_123”,systemdateget()); // Transaction date

//purchFormLetter.update(purchTable, strFmt(“PO_%1”, purchTable.PurchId));

purchFormLetter.update(purchtable,

// Purchase record Buffer

“INV-”+purchTable.PurchId, // Invoice Number

systemdateget()); // Transaction date

ttscommit;

if (PurchTable::find(purchTable.PurchId).DocumentStatus == DocumentStatus::Invoice)

{

info(strfmt(“Posted invoiced journal for purchase order %1”,purchTable.PurchId));

}

AX Version 2012 R3

Kindly Help Me

Thanks in Advance.

Is that the total error?

Error is : cannot insert multiple records in vendor invoice (vendivoiceinfotable) purchase order XXX the record already exists
Update has been cancelled

BTW when i create po through code and post invoice through front end means through form is working fine but from code through error.

Have you tried by creating the PO through code and posting it from front end? What unique indexes do you have on VendInvoiceInfoTable? BTW, I tried your posting code and it works for me.

Hi kranthi
Thanks for your suggestion
Actually there were some problem in purchformletter class/ run I just call rearrange method like
Purchformletter.reArrange(false)
And issue is resolved