Create Purchase Order Invoice via BOI

SOLVED

Experts,


I am able to create Sales Order, Sales Order Invoices, and AP Invoices via BOI without any issues. I am missing something about the order of things for PO Receipt of Invoice. Here is an excerpt of my code (it's in Powershell, but the methods are the same as you would use in VBScript or C#): I took out some of the not relevant logic and error handling to make it more readable.

$retVal = $oPOInv.nSetValue("ReceiptType$", "I")
$sRecNo = ""
$retVal = $oPOInv.nGetNextNextInvoiceEntryNo([ref]$sRecNo)
$retVal = $oPOInv.nSetKeyValue("ReceiptType$", "I")
$retVal = $oPOInv.nSetKeyValue("ReceiptNo$",[ref]$sRecNo)
$retVal = $oPOInv.nSetKey()
...
            
$retVal = $oPOInv.nSetValue("PurchaseOrderNo$",[ref]$sPONo)
...
$retVal = $oPOInv.nSetValue("InvoiceNo$",[ref]$sInv)
...
$retVal = $oPOInv.nSetValue("InvoiceDate$",[ref]$sInvoiceDate)
...
            
# Loop through the Line Items
...
$bLinesOkay = $true
foreach ($item in $xDoc.Invoice.LineItems)
{ 
    # Get PO Item Data for the line
    $sItemCode = $item.item.ItemNumber
    $nQty = [float]$item.item.Quantity
    $nAmount = [float]$item.item.Amount
    if ([string]::IsNullOrEmpty($item.item.GLAccount))
    {
        $sGLAccount = ''
    }
    else
    {
        $sGLAccount = $item.item.GLAccount
        $retVal = $oAccount.nFind("$sGLAccount")
        ...
        $retVal = $oAccount.nGetValue("AccountKey$",[ref]$sGLAccount)
    }
    $sOrderLineKey = $item.item.OrderLineKey
                    
    # Create and Write the line
    $retVal =$oPOInvLines.nAddLine()
    ...
    $retVal = $oPOInvLines.nSetValue("ItemCode$",[ref]$sItemCode)
    ...
    $retVal = $oPOInvLines.nSetValue("OrderLineKey$",[ref]$sOrderLineKey)
    ... 
    $retVal = $oPOInvLines.nSetValue("QuantityInvoiced",[ref]$nQty)
    ...
    $retVal = $oPOInvLines.nSetValue("ExtensionAmt",[ref]$nAmount)
    ...
}

All $retVal are fine up to when I set the ItemCode (I get a field is not in the I/O list error). When I only try to use the OrderLineKey$ I get a Missing or Invalid Header at the setval for the OrderLineKey$. 

The Sage version is v2014 and PO Invoice Entry has Batch Entry turned on. I am not 100% on setting the ReceiptType. The examples I saw only set it once with a SetValue in the beginning (I assume that is what Sage does when you open the Invoice task vs. the ROG task). I do it a second time as part of the SetKeyValue for the Invoice. If I don't it will error out on the SetKey().

Even so the nNextNextInvoiceEntryNo looks like a typo, it comes back with the next invoice no. So that piece works.

Any ideas what I am doing wrong?

Thanks,

Bastian