Adding Line to SO Entry with qty ordered being the total of extension amount of other items

Hello,

I am fairly new to scripting and have only written some simple ones before this. I am trying to add one line to the SO Detail with the Qty Ordered = the total Extension Amount of Inventory type items. I am running this as a Table - Pre Totals on the SO Sales Order Header Table. Can anyone help me with this one? I've tried a few different things but have not been able to get it to add a line when I do the script from the header like this. Thanks!

ExtAmt = 0
ItemType = ""
ItemCode = ""
QtyOrd = 0
TotExt = 0

Set oLines = oBusObj.AsObject(oBusObj.Lines)

retVal = oLines.MoveFirst()
Do Until cBool(oLines.EOF) = True

retVal = oLines.GetValue("ItemType$", ItemType)

if ItemType = "1" then
retVal = oLines.GetValue("ExtensionAmt", ExtAmt)
TotExt = TotExt + ExtAmt
End if

Loop

retVal = oLines.MoveLast()

retVal = oLines.AddLine()
retVal = oLines.SetValue("ItemCode$","/SUPPLYCHG")
retVal = oLines.SetValue("QuantityOrdered", TotExt)
retVal = oLines.Write()