Trying to update Sales Order Header UDF from Invoice Entry getting error

SOLVED

I'm trying to update a user defined field in the Sales Order Header table using a script from Sales Order Invoice entry from the Table - PreWrite event. The script saves the USERCODE from the session object into a UDF in the Invoice and then tries writing the USERCODE back into a duplicate UDF in the Sales Header. When the oSalesSvc.WRITE() is called I get an error "*memory* does not have correct permissions". I've tried everything I can think of. Anybody see what i could do differently?

Here is the script:

tmpValue = ""

tmpSalesOrderNo = ""

' only update UDF's if the UDF is empty

retVal = oBusObj.GetValue("UDF_USERCODE$",tmpValue)
retVal = oBusobj.GetValue("SalesOrderNo$",tmpSalesOrderNo)

IF tmpValue = "" THEN

retVal = oBusObj.SetValue("UDF_USERCODE$",oSession.UserCode)

Set oSalesSvc = oSession.AsObject(oSession.GetObject("SO_SalesOrder_bus"))

retVal = oSalesSvc.ReadRec(tmpSalesOrderNo)

retVal = oSalesSvc.SetValue("UDF_USERCODE$",oSession.UserCode)

retVal = oSalesSvc.Write()

End If