Script creates duplicated Invoice Lines

SOLVED

Hello,

Could you please advise - what is wrong with the script below.
It creates duplicated invoice lines, seemingly due to the CopyLinesFromSalesOrder call.

Maybe this function is outdated - looks like line items are created anyway?

Thanks,

Alex

'Create ProvideX COM Object
Set oScript = CreateObject ("ProvideX.Script")

'Get the ODBC path for the last accessed installation of MAS 90/200
Const HKEY_CURRENT_USER = &H80000001
Set oReg = GetObject("winmgmts:\\.\root\default:StdRegProv")
oReg.GetExpandedStringValue HKEY_CURRENT_USER,"Software\ODBC\ODBC.INI\SOTAMAS90","Directory",PathRoot
PathHome = PathRoot & "\Home"
MsgBox(PathHome)
Set oReg = Nothing

oScript.Init(PathHome)

Set oSS = oScript.NewObject("SY_Session")

' Set the user for the Session
r = oSS.nLogon()
If r=0 Then



    '''''''''''''''''''''''
    ' Enter: username, password
    r = oss.nSetUser("user","password")


End If


' Set the company, module date and module for the Session
r = oss.nsetcompany("ABC")



'''''''''''''''''''''''
' Enter: date
r = oSS.nSetDate("S/O","20140406")
r = oSS.nSetModule("S/O")




' Instantiate a Sales Order business object
oSEC = oSS.nSetProgram(oSS.nLookupTask("SO_SalesOrder_ui"))
Set o = oScript.NewObject("SO_SalesOrder_bus", oSS)





'''''''''''''''''''''''
' Enter: OrderNumber
OrderNo = "0000408"




MsgBox(OrderNo)

' Create a new S/O Invoice for the Sales Order created above

oSEC = oSS.nSetProgram(oSS.nLookupTask("SO_Shipping_ui"))
Set o = oScript.NewObject("SO_Shipping_bus", oSS)

' Check the BatchEnabled property to determine if batches are enabled
IF o.nBatchEnabled = 1 Then
    BatchNo = ""
    PrivateFlag = "Y"
    CommentText = "This is a comment"
    r = o.nSelectNewBatch(BatchNo, PrivateFlag, CommentText)
End If

' Create a new header record using the next available invoice number
InvoiceNo = ""
r = o.nGetNextInvoiceNo(InvoiceNo)
r = o.nSetKey(InvoiceNo)
r = o.nSetValue("ARDivisionNo$", "02")
r = o.nSetValue("SalesOrderNo$", OrderNo)
MsgBox(InvoiceNo)

' Copy the lines from a Sales Order by calling the CopyLinesFromSlesOrder method.
shipCompleteOrder = "Y"
r = o.oLines.nCopyLinesFromSalesOrder(OrderNo, shipCompleteOrder)
If r = 0 Then
    MsgBox("Error copying SO lines to INV: " & o.sLastErrorMsg & vbCRLF & "Quiting")
    oss.nCleanup()        ' Call Cleanup() before dropping the Session Object
    oss.DropObject()
    Set oss = Nothing
    WScript.Quit
End If

' Process Lines Loop - set shipped quantity
r = o.oLines.nMoveFirst()
Do Until o.oLines.nEOF=1
    r = o.oLines.nGetValue("ItemType$",ItemType)

    tmpQuantity = 0
    r = o.olines.nGetValue("QuantityOrdered",tmpQuantity)
    If r = 0 Then
        MsgBox("Error getting QuantityOrdered: " & o.sLastErrorMsg & vbCRLF & "Quitting")
        oss.nCleanup()        ' Call Cleanup() before dropping the Session Object
        oss.DropObject()
        Set oss = Nothing
        WScript.Quit
    End If

    r = o.olines.nGetValue("QuantityShipped",quantityShipped)
    If r = 0 Then
        MsgBox("Error getting QuantityShipped: " & o.sLastErrorMsg & vbCRLF & "Quitting")
        oss.nCleanup()        ' Call Cleanup() before dropping the Session Object
        oss.DropObject()
        Set oss = Nothing
        WScript.Quit
    End If

    'r = o.oLines.nGetValue("Valuation$",Valuation)
        
    MsgBox("ItemType: " & ItemType & "; Quantity Ordered: " & tmpQuantity)
        
    ' Set QuantityShipped
    r = o.oLines.nSetValue("QuantityShipped",tmpQuantity)
    If r = 0 Then
        MsgBox("Error setting line quantity: " & o.sLastErrorMsg & vbCRLF & "Quitting")
        oss.nCleanup()        ' Call Cleanup() before dropping the Session Object
        oss.DropObject()
        Set oss = Nothing
        WScript.Quit
    End If

    r = o.oLines.nMoveNext()
Loop ' End Invoice Lines loop

' Write the line change to memory
r = o.oLines.nWrite()
If r = 0 Then
    MsgBox("Error writing lines: " & o.sLastErrorMsg & vbCRLF & "Quitting")
    oss.nCleanup()        ' Call Cleanup() before dropping the Session Object
    oss.DropObject()
    Set oss = Nothing
    WScript.Quit
End If

' Write the invoice header and lines to disk
r = o.nWrite()
If r = 0 Then
    MsgBox("Error writing INV: " & o.sLastErrorMsg & vbCRLF & "Quitting")
    oss.nCleanup()        ' Call Cleanup() before dropping the Session Object
    oss.DropObject()
    Set oss = Nothing
    WScript.Quit
End If

' Done with the Invoice object
r = o.DropObject()
o = 0

Wscript.Echo "DONE"
Wscript.Quit