Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ' Replicating a problem with Sage 100 / Distribution table access.
- '
- ' Format: VBScript. To run: Copy to a local PC folder as a 'test.vbs' file;
- ' double click using Windows File Explorer. Sage 100 must be installed.
- '
- ' Input: The script will prompt for:
- ' User Name, Password, Company, Sales Order number, Shipped ID, Lot/Serial per each SO item.
- '
- ' A Sales Order Entry must be created before the test, containing at least two line items with a product with LOT valuation.
- ' For example, for ABC sample company: two line items containing items 2480-8-50 and 2481-5-50. Items should NOT be
- ' allocated/distributed in the sales order entry.
- ' The script will distribute the items. JAN10 may be entered as a lot number for both items, with script prompts.
- ' The issue is replicated when the following shows up with script messages:
- ' - 'WHY AM I SHOWING UNRELATED DATA?', and
- ' - 'WHY DID NOT I SHOW DISTRIBUTION ON LINE ITEM 000004 ?'.
- '
- ' Issue: oLines.oDistribution does not give Read access to a proper distribution record when navigating through line items
- ' and the current line item is second or next.
- ' In other words: Navigating to the second line item does not give access to that line item's distributions. It allows
- ' saving changes, but those changes again are not reflected with a subsequent Read operation.
- '
- Sub errchk(r, desc, o)
- if (r = 0) then
- MsgBox("Error " & desc & ": " & o.sLastErrorMsg)
- oss.nCleanup()
- oss.DropObject()
- Set oss = Nothing
- WScript.Quit
- end if
- end sub
- Sub warnchk(r, desc, o)
- if (r = 0) then
- MsgBox("Warning " & desc & ": " & o.sLastErrorMsg)
- end if
- end sub
- function wSplittedString(data)
- r = ""
- i_ = 1
- if (InStr(data, Chr(138)) > 0) then
- while i_ <= Len(data)
- char=Mid(data, i_, 1)
- if (char = Chr(138)) then
- r = r & Chr(255)
- else
- r = r & char
- end if
- i_ = i_ + 1
- wend
- else
- while i_ <= Len(data)
- char=Mid(data, i_, 1)
- if (Asc(char) > 128) then
- r = r & Chr(255)
- else
- r = r & char
- end if
- i_ = i_ + 1
- wend
- end if
- r = Replace(r, Chr(13), " ")
- r = Replace(r, Chr(10), " ")
- r = Replace(r, Chr(255), vbCrLf)
- wSplittedString = r
- end function
- function PreparedString(data, removeEmptyStrings)
- data = wSplittedString(data)
- if (removeEmptyStrings) then
- while InStr(data, vbCrLf & vbCrLf) > 0
- data = Replace(data, vbCrLf & vbCrLf, vbCrLf)
- if (InStr(data, vbCrLf) = 1) then
- data = Mid(data, Len(vbCrLf) + 1)
- end if
- wend
- if (Mid(data, Len(data) - Len(vbCrLf) + 1, Len(vbCrLf)) = vbCrLf) then
- data = Mid(data, 1, Len(data) - Len(vbCrLf))
- end if
- end if
- PreparedString = data
- end function
- function iif(what, iftrue, iffalse)
- if (what) then
- iif = iftrue
- else
- iif = iffalse
- end if
- end function
- 'Create ProvideX COM Object
- Set oScript = CreateObject ("ProvideX.Script")
- 'oScript.TraceOn=true
- '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
- 'The Init method must be the first method called
- oScript.Init(PathHome)
- 'The Session object must be the first MAS 90 object created
- Set oSS = oScript.NewObject("SY_Session")
- ' Set the user for the Session
- r = oSS.nLogon()
- If r=0 Then
- '''''''''''''''''''''''
- ' Enter: username, password
- user = Trim(InputBox("Enter User Name", "", "alex"))
- password = Trim(InputBox("Enter Password", "", ""))
- retVAL = oSS.nSetUser(User,Password)
- r = oss.nSetUser(user, password)
- errchk r, "logging in", oss
- End If
- ' Set the company, module date and module for the Session
- company = Trim(InputBox("Enter Company", "", "AAA"))
- r = oss.nsetcompany(company)
- errchk r, "setting company", oss
- orderNumber = trim(InputBox("Enter Sales Order Number", "", "0000182"))
- ' Prompt for a batch ID
- batchOptions = "N"
- Set oBatchOptions = oScript.NewObject("SY_BATCHOPTIONS_SVC", oSS)
- r = oBatchOptions.nFind("S/O01")
- errchk r, "Finding batch options", oBatchOptions
- r = oBatchOptions.nGetValue("ENABLEBATCHENTRY$", batchOptions)
- errchk r, "Getting batch options", oBatchOptions
- batchId = ""
- nextBatchNumber = ""
- if batchOptions = "Y" then
- batchId = trim(InputBox("Enter Batch ID", "", "00001"))
- ' nextBatchNumber = oBatchOptions.nGetValue("NEXTBATCHNO$")
- end if
- shipperId = ""
- shipperId = trim(InputBox("Enter Shipper ID", "", "1"))
- sDate = oSS.sModuleDate
- if sDate = "" then
- sDate = Year(Date) & Right("0" & Month(Date), 2) & Right("0" & Day(Date), 2)
- end if
- retVAL = oSS.nSetDate("S/O",sDate)
- r = oSS.nSetModule("S/O")
- errchk r, "Setting module", oSS
- qtyOrdered = 0
- qtyBackordered = 0
- Dim lines()
- Dim qtiesToShip()
- ''''''''''''''''''''''''''''''''''
- ' Filling in the outstanding quantity to ship
- oSEC_order = oSS.nSetProgram(oSS.nLookupTask("SO_SalesOrder_ui"))
- Set o_order = oScript.NewObject("SO_SalesOrder_bus", oSS)
- retVal = o_order.nSetKey( orderNumber )
- errchk retVal, "Set Order Key", o_order
- i = 0
- r = o_order.oLines.nMoveFirst
- 'MsgBox "Before the first line items loop"
- do while o_order.oLines.nEOF = 0
- 'MsgBox "First line items loop - iteration " & i
- r = o_order.oLines.nGetValue("QUANTITYORDERED", qtyOrdered)
- r = o_order.oLines.nGetValue("QUANTITYSHIPPED", qtyToBackordered)
- Redim preserve qtiesToShip(i+1)
- qtiesToShip(i) = qtyOrdered - qtyBackordered
- ' MsgBox "Qty to ship: " + CStr(qtiesToShip(i))
- i = i + 1
- r = o_order.oLines.nMoveNext
- loop
- r = o_order.DropObject()
- o_order = 0
- ''
- ''''''''''''''''''''''''''''''''''
- ' Creating Shipment business object
- oSEC = oSS.nSetProgram(oSS.nLookupTask("SO_Shipping_UI"))
- Set oHeader = oScript.NewObject("SO_Shipping_bus", oSS)
- fields = oHeader.sGetDataSources
- 'MsgBox(fields)
- nextInvoiceNo = ""
- r = oHeader.nGetNextInvoiceNo(nextInvoiceNo)
- errchk r, "Getting next invoice number", oHeader
- if batchOptions = "Y" then
- r = oHeader.nSelectBatch(batchId)
- errchk r, "Selecting the batch", oHeader
- 'MsgBox("Selected")
- end if
- r = oHeader.nSetKeyValue("InvoiceNo$", nextInvoiceNo)
- errchk r, "Setting invoice number", oHeader
- r = oHeader.nSetKey()
- errchk r, "Selecting the invoice", oHeader
- r = oHeader.nSetValue("ShipperID$", shipperId)
- errchk r, "setting shipper ID", oHeader
- r = oHeader.nSetValue("SalesOrderNo$", orderNumber)
- 'errchk r, "setting order number", oHeader
- MsgBox "Document created: " & nextInvoiceNo
- invoiceNumber=""
- lineSequenceNumber=""
- lineKey=""
- ' Re-populating shipment line items
- r = oHeader.oLines.nClear
- errchk r, "clear lines", oHeader.oLines
- r = oHeader.oLines.nCopyLinesFromSalesOrder(orderNumber, "Y")
- errchk r, "copy lines", oHeader.oLines
- ' Fillng in an in-memory list of line items to further navigate
- i = 0
- r = oHeader.oLines.nMoveFirst
- do while oHeader.oLines.nEOF = 0
- r = oHeader.oLines.nGetValue("INVOICENO$", invoiceNumber)
- r = oHeader.oLines.nGetValue("LineSeqNo$", lineSequenceNumber)
- r = oHeader.oLines.nGetValue("LINEKEY$", lineKey)
- r = oHeader.oLines.nGetValue("QUANTITYORDERED", qtyOrdered)
- r = oHeader.oLines.nGetValue("QUANTITYSHIPPED", qtyToBackordered)
- key = invoiceNumber+lineSequenceNumber+lineKey
- Redim preserve lines(i+1)
- lines(i) = key
- i = i + 1
- r = oHeader.oLines.nMoveNext
- loop
- currentIndex=""
- index = oHeader.oLines.sCurrentIndex()
- 'MsgBox "Current index: " + index
- 'r = oHeader.oLines.nSetIndex("kDISPLAY")
- 'errchk r, "error setting index", oHeader.oLines
- ' Preparing distributions object to get available distributions
- tableName = "IM_ItemCost_SVC"
- bObjectExists = oScript.Evaluate("o_" & tableName) <> 0
- if not bObjectExists then
- sExpr = "o_" + tableName + "=NEW( """ + tableName + """, %SYS_SS )"
- oScript.Execute(sExpr)
- end if
- ' Getting ItemCode Length
- Set fo = oSS.oFileObj
- itemCodeLength = fo.nGetColumnLength("IM_ItemCost", "ItemCode$")
- ' Iterating in-memory line items
- for i=0 to UBound(lines)-1
- 'MsgBox "Iteration # " & i+1 & " Searching for key: '" + lines(i)
- r = oHeader.oLines.nMoveFirst()
- errchk r, "Move First", oHeader.oLines
- r = oHeader.oLines.nFind(lines(i))
- errchk r, "document not found", oHeader.oLines
- item = ""
- r = oHeader.oLines.nGetValue("ItemCode$", item)
- errchk r, "getting item code", oHeader.oLines
- sValuation = ""
- sValuation = oHeader.oLines.sItemValuation
- ' Current position diagnostics
- r = oHeader.oLines.nGetValue("INVOICENO$", invoiceNumber)
- r = oHeader.oLines.nGetValue("LineSeqNo$", lineSequenceNumber)
- r = oHeader.oLines.nGetValue("LINEKEY$", lineKey)
- MsgBox "Processing line item: Invoice '" & invoiceNumber & "' Line sequence # '" & lineSequenceNumber & "' Line Key '" & lineKey & "' Item '" & item & "' valuation " & sValuation & "; Shipping qty: " & qtiesToShip(i)
- if sValuation = 5 or sValuation = 6 then
- ' Getting available lot/serial
- itemLow = "PAD(""" & item & """, " & itemCodeLength & ", $00$)"
- itemHigh = itemLow + " + $FE$"
- condition = ""
- expr = "iRet = o_" & tableName & "'GetResultSets(""ItemCode$+""""`!`""""+TierType$+""""`!`""""+LotSerialNo$+""""`!`""""+STR(QuantityOnHand-QuantityCommitted)"", ""ItemCode$"", s1$, s2$, """ & condition & """," & itemLow & "," & itemHigh & ")"
- r = oScript.Execute(expr)
- iRes = oScript.Evaluate("iRet")
- wResultFirst = oScript.Evaluate("S1$")
- wResult = oScript.Evaluate("S2$")
- list = PreparedString(wResultFirst, true)
- listArr = Split(list, vbCrlf)
- itemList = ""
- for j=0 to UBound(listArr)
- itemList = itemList & vbCrlf & listArr(j)
- next
- qty = qtiesToShip(i)
- if sValuation = 6 then
- qty = 1
- end if
- ' Lot/Serial prompt
- lotSerial = trim(InputBox("Enter a lot/serial. List of available: " & itemList, "", ""))
- currentIndex=""
- index = oHeader.oLines.oDistribution.sCurrentIndex()
- 'MsgBox "Current distribution index: " + index
- 'r = oHeader.oLines.nSetIndex("kDISPLAY")
- 'errchk r, "error setting index", oHeader.oLines
- ' Reading existing distributed quantity
- distributionKey = invoiceNumber + lineKey + lotSerial
- r = oHeader.oLines.oDistribution.nFind(distributionKey)
- ' warnchk r, "Setting Distribution position", oHeader.oLines.oDistribution
- r = oHeader.oLines.oDistribution.nMoveFirst
- 'MsgBox "Iterating distributions: MoveFirst = " & r
- eof = oHeader.oLines.oDistribution.nEOF
- MsgBox "EXISTING DISTRIBUTIONS: Iterating; EOF = " & eof & " meaning " & iif(eof=0, "RECORD EXISTS", "NO MORE RECORDS")
- do while eof = 0
- itemCode = ""
- r = oHeader.oLines.oDistribution.nGetValue("ITEMCODE$", itemCode)
- lotSerial = ""
- r = oHeader.oLines.oDistribution.nGetValue("INVOICENO$", lotSerial)
- qty = 0
- r = oHeader.oLines.oDistribution.nGetValue("QuantityShipped", qty)
- lk = ""
- r = oHeader.oLines.oDistribution.nGetValue("LineKey$", lk)
- MsgBox "EXISTING DISTRIBUTIONS: Read item '" & itemCode & "' Line Key '" & lk & "' Lot/Serial '" & lotSerial & "' Qty " & qty & Iif(lk <> lineKey, " WHY AM I SHOWING UNRELATED DATA? ", "")
- r = oHeader.oLines.oDistribution.nMoveNext
- 'MsgBox "Iterating distributions: MoveNext = " & r
- eof = oHeader.oLines.oDistribution.nEOF
- MsgBox "EXISTING DISTRIBUTIONS: Iterating; MoveNext EOF = " & eof & " meaning " & iif(eof=0, "RECORD EXISTS", "NO MORE RECORDS")
- loop
- ' Distributing
- MsgBox "DISTRIBUTING: Invoice '" & invoiceNumber & "' Line Key '" & lineKey & "' Lot/serial '" & lotSerial & "'; qty " & qty
- r = oHeader.oLines.oDistribution.nSetKeyValue("INVOICENO$", invoiceNumber)
- r = oHeader.oLines.oDistribution.nSetKeyValue("LINEKEY$", lineKey)
- r = oHeader.oLines.oDistribution.nSetKeyValue("LOTSERIALNO$", lotSerial)
- r = oHeader.oLines.oDistribution.nSetKey
- errchk r, "navigating to distribution item", oHeader.oLines.oDistribution
- r = oHeader.oLines.oDistribution.nSetValue("QUANTITYSHIPPED", qty)
- r = oHeader.oLines.oDistribution.nWrite
- errchk r, "distribution line write", oHeader.oLines.oDistribution
- r = oHeader.oLines.oDistribution.nUpdateIMItemCost
- ' warnchk r, "IM_ItemCost update", oHeader.oLines.oDistribution
- 'MsgBox "Iterating distributions"
- r = oHeader.oLines.oDistribution.nMoveFirst
- 'MsgBox "Iterating distributions: MoveFirst = " & r
- t = ""
- r = oHeader.oLines.oDistribution.nGetValue("LINEKEY$", t)
- 'MsgBox "LineKey for check: " & t
- conditionSatisfied = 0
- eof = oHeader.oLines.oDistribution.nEOF
- MsgBox "DISTRIBUTIONS AFTER UPDATE: Iterating; EOF = " & eof & " meaning " & iif(eof=0, "RECORD EXISTS", "NO MORE RECORDS")
- do while eof = 0
- itemCode = ""
- r = oHeader.oLines.oDistribution.nGetValue("ITEMCODE$", itemCode)
- lotSerial = ""
- r = oHeader.oLines.oDistribution.nGetValue("INVOICENO$", lotSerial)
- qty = 0
- r = oHeader.oLines.oDistribution.nGetValue("QuantityShipped", qty)
- lk = ""
- r = oHeader.oLines.oDistribution.nGetValue("LineKey$", lk)
- MsgBox "DISTRIBUTIONS AFTER UPDATE: Read item '" & itemCode & "' Line Key '" & lk & "' Lot/Serial '" & lotSerial & "' Qty " & qty
- if lk = lineKey then
- conditionSatisfied = conditionSatisfied + 1
- end if
- r = oHeader.oLines.oDistribution.nMoveNext
- 'MsgBox "Iterating distributions: MoveNext = " & r
- eof = oHeader.oLines.oDistribution.nEOF
- MsgBox "DISTRIBUTIONS AFTER UPDATE: Iterating; MoveNext EOF = " & eof & " meaning " & iif(eof=0, "RECORD EXISTS", "NO MORE RECORDS")
- loop
- end if
- if conditionSatisfied = 0 then
- MsgBox "WHY DID NOT I SHOW DISTRIBUTION ON LINE ITEM " & lineKey & " ?"
- end if
- r = oHeader.oLines.nSetValue("QUANTITYSHIPPED", qtiesToShip(i))
- r = oHeader.oLines.nSetValue("QUANTITYBACKORDERED", 0)
- r = oHeader.oLines.nSetQuantityBackordered(0)
- r = oHeader.oLines.nSetValue("LotSerialFullyDistributed$", "Y")
- r = oHeader.oLines.nWrite
- errchk r, "line write", oHeader.oLines
- MsgBox "Item processing finalized"
- next
- r = oHeader.nWrite
- errchk r, "saving the shipment - " & op, oHeader
- Wscript.Echo "DONE"
- ' Done with the Invoice object
- r = oHeader.DropObject()
- oHeader = 0
- oss.nCleanup() ' Call Cleanup() before dropping the Session Object
- oss.DropObject()
- Set oss = Nothing
- WScript.Quit
Add Comment
Please, Sign In to add comment