Writes not working from a event script

SOLVED
My goal is on a new PO, update the SOs PO number and PO required Date values that are referenced in the PO.  I have been able to go through the PO lines and find the SOs, but I can't seem to update the SO lines.  I do not seem to be getting any failures, just no updates.  Anyone have any ideas?
rc = oscript.debugprint("Starting postOpenPurchaseOrder Processing")
'initialize variables'
rc       = 0
itemType = 0
sPurchaseOrderNo      = ""
sPurchaseOrderReqDate = ""
sSalesOrderNo         = ""
sItemCode             = ""
sSOItemCode           = ""
sSOSalesOrderNo       = ""
'Get Purchase Order Lines'
'oBusObj = PurchaseOrderHeader'
Set oPurchaseOrderDetail = oBusObj.AsObject(oBusObj.Lines)
rc = oBusObj.GetValue("PurchaseOrderNo$",sPurchaseOrderNo)
rc = oBusObj.GetValue("RequiredExpireDate$",sPurchaseOrderReqDate)
'Setup the OpenSaleOrderHeader Object'
Set oSalesOrderHeader = oSession.AsObject(oSession.GetObject("SO_SalesOrder_bus"))
'set oSalesOrderHeader = oBusObj.AsObject(oBusObj.GetChildHandle("SalesOrderNo"))
'Start going through all the lines in the PO to update SOs'
rc = oPurchaseOrderDetail.MoveFirst()
Do Until cBool(oPurchaseOrderDetail.EOF)
rc = oPurchaseOrderDetail.GetValue("SalesOrderNo$",sSalesOrderNo)
rc = oPurchaseOrderDetail.GetValue("ItemCode$",sItemCode)
rc = oscript.debugprint("PO No:       " + sPurchaseOrderNo)
rc = oscript.debugprint("PO Req Date: " + sPurchaseOrderReqDate)
rc = oscript.debugprint("Item Code    " + sItemCode)
rc = oscript.debugprint("SO No:       " + sSalesOrderNo)
' If we have a null SalesOrderNo then assume PO is for stock'
if (sSalesOrderNo <> "") then
 'now find the SO for the current PO Line item'
 rc = oSalesOrderHeader.setkey(sSalesOrderNo)
 set oSalesOrderDetail = oSalesOrderHeader.AsObject(oSalesOrderHeader.Lines)
 ' Check to make sure the SO exists'
 if ( rc <> 0 ) then
  rc = oscript.debugprint("Found SO for PO Item")
  rc = oSalesOrderDetail.MoveFirst()
  Do Until cBool(oSalesOrderDetail.EOF)
   'now get the SO lines and check for a item code match'
   rc = oSalesOrderDetail.GetValue("SalesOrderNo$",sSOSalesOrderNo)
   rc = oSalesOrderDetail.GetValue("ItemCode$",sSOItemCode)
   rc = oscript.debugprint(sSOSalesOrderNo + ":" + sSOItemCode)
   sLineKey = ""
   rc = oSalesOrderDetail.GetValue("LineKey$",sLineKey)
   if (rc = 0) then rc = oscript.debugprint(oSalesOrderDetail.GetLastError) end if
   sEditKey = oSalesOrderDetail.GetEditKey(sLineKey)
   rc = oSalesOrderDetail.EditLine(sEditKey)
   if (rc = 0) then rc = oscript.debugprint(oSalesOrderDetail.GetLastError) end if
   rc = oSalesOrderDetail.SetValue("PurchaseOrderNo$",sPurchaseOrderNo)
   if (rc = 0) then rc = oscript.debugprint(oSalesOrderDetail.GetLastError) end if
   rc = oSalesOrderDetail.Write()
   if (rc = 0) then rc = oscript.debugprint(oSalesOrderDetail.GetLastError) end if
   
   rc = oSalesOrderDetail.MoveNext()
  Loop
 end if
 else
 'log as warning that we did not have a SO number'
 'osession.WriteLog("M","POPO:NULL SO #")'
 end if
rc = oPurchaseOrderDetail.MoveNext()
Loop
rc = oscript.debugprint("Ending postOpenPurchaseOrder Processing")
  • 0

    Are you perhaps missing the oSalesOrderHeader.Write at the end of each SO?

  • +1 in reply to n0tgunshy2
    verified answer

    In addition to appearing to be missing the oSalesOrderHeader.Write after update the sales order's lines, your check to see if the sales order exists might be better off checking to see if rc = 1, if rc = 2, then your SetKey is starting a new sales order record, if rc =1, then you are selecting an existing sales order, if rc = -1, then a warning occurred and you should check oSalesOrderHeader.LastErrorMsg to see what it is, it could be then the sales order is opened by someone else or is being invoiced and your edits won't be saved anyway.