we add a UDF UDF_STATUS_UPDATE in both SO_SalesOrderHeader and PO_PurchaseOrderHeader table to track order fulfill status. when change this order fulfill status in sales order, we want all PO generated from this sales order same UDF fulfill status get update, if sales order is brand new order, and use the generate PO button in sales order total panel to generate new PO, the value in PO get write, but if later we change UDF_STATUS_UPDATE value again in sales order and run this script in SO pre-write event, it success, but when we check the PO value, still show the value before this change
IF oSession.CompanyCode = "TEST" Then
IF oSession.StartProgram ="SO_SALESORDER_UI" THEN
IF CBool(oScript.UIObj) = "True" AND oSession.Updating = 0 Then
'set this list to hold all po numbers
Dim pos
Set pos = CreateObject("System.Collections.ArrayList")
'loop sales order detail to find all po numbers
Set oLines = oBusObj.AsObject(oBusObj.Lines)
retVal = oLines.MoveFirst()
Do until oLines.EOF
sPurchaseOrderNo=""
retval = oLines.GetValue("PurchaseOrderNo$",sPurchaseOrderNo )
IF CSTR(sPurchaseOrderNo) <>"" Then
If NOT pos.Contains(sPurchaseOrderNo) Then
pos.Add sPurchaseOrderNo
End If
End if
retval = oLines.MoveNEXT()
Loop
'read Sales order fulfill status
sUDF_STATUS_UPDATE=""
retVal =oBusObj.GetValue("UDF_STATUS_UPDATE$", sUDF_STATUS_UPDATE)
'Update each PO fulfill status
For Each poNo In pos
'move to PO_PurchaseOrder_Bus
SET oMyObj = oSession.AsObject(oSession.GetObject("PO_PurchaseOrder_Bus"))
retVal =oMyObj.SetKeyValue("PurchaseOrderNo$", poNo)
retVal =oMyObj.SetKey
retVal =oMyObj.SetValue("UDF_STATUS_UPDATE$", sUDF_STATUS_UPDATE)
Next
END IF
END IF
END IF