Script to total a values from Detail Lines into Sales Order Header

I know I can create a field in the SO detail and total it in the header, but they have hundreds of existing orders and we are changing the calculation which would then go to zero.  I have it on the SalesOrderHeader Table Pre-totals.

So I want to read all the detail lines and put the total in the Header.

I get an error 88 on oHeaderObj  SO_SaleasOrderHistory.pvc

itemtype= ""
TotUnits =0
Set oLines = oBusObj.AsObject(oBusObj.Lines)
retVal = oLines.MoveFirst()
Do until oLines.EOF
retval = oLines.GetValue("ItemType$", itemtype)
if itemtype = "1" then
retval = oLines.GetValue("UDF_PALLET_COUNT_UNITS",punits)
retval = oLines.GetValue("UDF_PALLET_WEIGHT",pwgt)
if pwgt = 0 then
Totwgt = Totwgt + (punits * 75)
else
Totwgt = Totwgt + (punits * pwgt)
End if
TotUnits = TotUnits + punits
End if
retval = oLines.MoveNEXT()
Loop

retval=oHeaderObj.SetValue("UDF_PALLET_WEIGHT LBS",Totwgt)
retval=oHeaderObj.SetValue("UDF_TOTAL_PALLETS_UNITS",TotUnits)

Thank you in advance.

  • 0
    Since you're starting by creating oLines, your script trigger must be in the header already, which doesn't have oHeaderObj... use oBusObj for your SetValue lines.
  • 0 in reply to Kevin M
    Thank you and that worked really well for the Sales Order Header, but I tried the same logic for the Invoice Header and the Weight calculates properly, but does not update in the Invoice Header. I have updated the script again to check that the field name is correct and I removed the default from the Sales Order Header, but I cannot get it to work.

    TotUnits=0
    Totwgt=0
    itemtype= ""
    Set oLines = oBusObj.AsObject(oBusObj.Lines)
    retVal = oLines.MoveFirst()
    Do until oLines.EOF
    retval = oLines.GetValue("ItemType$", itemtype)
    if itemtype = "1" then
    retval = oLines.GetValue("UDF_PALLET_COUNT_UNITS",punits)
    retval = oLines.GetValue("UDF_PALLET_WEIGHT",pwgt)
    TotUnits = TotUnits + punits
    if pwgt = 0 then

    Totwgt = Totwgt + (punits * 75)
    else

    Totwgt = Totwgt + (punits * pwgt)
    End if
    End if
    retval = oLines.MoveNEXT()
    Loop
    msgbox(Totwgt)
    retval=oBusObj.SetValue("UDF_PALLET_WEIGHT LBS",Totwgt)
    retval=oBusObj.SetValue("UDF_TOTAL_PALLETS_UNITS",TotUnits)
  • 0 in reply to MJZ
    I think I am just looking at too many numbers. I think it is working. Thank you.