I am new to scripting and have created a script to add up the total cost of the lines on a Sales Order and then calculate the profit margin % and populate 2 UDFs on the totals tab. If I run the script as a table pre-write from the SO Header, it will display the Total Cost and Profit Margin % after I accept the order and go back into it. I have found 2 ways to get the script to write when lines are being entered and I select the totals tab (PTotals Panel PostLoad and SO Header Pre-Total), but when I click accept, I'm getting popup boxes one after the other saying ""TotalSales"" (one popup for each line entered, i.e. 5 lines nets 5 popups of ""TotalSales"") and ""CalculatedMargin"". The script is working correctly and is calculating the Total Cost and Gross Margin % correctly, it's just giving me those popups. What do I need to change to prevent those popup boxes? Or what steps should I do different to achieve this?
TotalSales = 0
TotalCost = 0
CalculatedMargin = 0
'
Set oLines = oBusObj.AsObject(oBusObj.Lines)
oLines.MoveFirst()
'
WHILE oLines.Eof<1
tmp=oLines.GetValue(""ItemType$"",lineItemType)
IF lineItemType=""1"" then
tmp=oLines.GetValue(""QuantityOrdered"",QuantityOrdered)
tmp=oLines.GetValue(""ExtensionAmt"",lineExtensionAmt)
tmp=oLines.GetValue(""UnitCost"",lineUnitCost)
TotalSales = TotalSales + lineExtensionAmt
TotalCost = TotalCost + (lineUnitCost*quantityOrdered)
end if
oLines.MoveNext()
WEND
'
if TotalSales<>0 and TotalCost <>0 then
CalculatedMargin = ((TotalSales-TotalCost)/TotalSales)*100
else
CalculatedMargin = 0
end if
'
'Set Total Cost and Margin
retVal = oBusObj.SetValue(""UDF_TOTAL_COST"", TotalCost)
retVal = oBusObj.SetValue(""UDF_MARGIN"", CalculatedMargin)