Apologies for the large block of text but this is a strange one.
I have a script that recalculates the Unit Price of a parent Kit item based on the component items' standard prices, and recalculates whenever a Quantity Ordered is changed for any component item. It runs on Post-Write of the lines.
When the Kit is first entered and Sage auto-populates the lines with the components, the script properly recalculates the Unit Price for the parent item by running after each line is inserted and updating the parent price. However, when we modify a quantity, the Unit Price does not change unless we click away to the Totals tab (or other tab) and come back.
To counteract this, we run a button script at the end of the main script to trigger a grid refresh. But with the grid refresh enabled, Sage is adding phantom lines below the component items. For example, in demo data ABC, item D1400 has 8 components. With the grid refresh button script active, Sage adds an additional 8 lines to the UI, duplicating the component lines, but they are not saved in the data table and will vanish by either changing panels or triggering the button script manually with a button press (adding multiple executions of the button script does not help).
Have tried using oUIObj.InvokeChange and many different timings of when to trigger the button script. Anything we try either results in the visual bug of duplicated lines, or a Unit Price of the parent item that doesn't recalculate when a component qty is modified.
Any ideas would be welcome. Script below.
Thanks!
Main Script:
Dim sIsKitParent, sKitLineKeyTrigger, sKitLineKey, sKey sIsKitParent = "" : sKitLineKey = "" : sKey = "" retVal = oBusObj.GetValue("SalesKitLineKey$",sKitLineKeyTrigger) retVal = oBusObj.GetValue("ExplodedKitItem$",sIsKitParent) retVal = oBusObj.GetValue("LineKey$",sKey) If Len(sKitLineKeyTrigger) > 0 And sIsKitParent = "N" Then Dim nNewPrice, nQtyLast, nQtyOrdered, nNewAmt nNewPrice = 0 : nQtyLast = 0 : nQtyOrdered = 0 : nNewAmt = 0.00 retVal = oBusObj.MoveFirst DO WHILE NOT CBool(oBusObj.EOF) retVal = oBusObj.GetValue("SalesKitLineKey$",sKitLineKey) retVal = oBusObj.GetValue("ExplodedKitItem$",sIsKitParent) If Len(sKitLineKey) > 0 Then If CInt(sKitLineKey) = CInt(sKitLineKeyTrigger) And sIsKitParent = "N" Then retVal = oBusObj.GetValue("UDF_STD_Unit_Price", nUnitPrice) retVal = oBusObj.GetValue("QuantityOrdered", nQtyOrdered) nNewAmt = nNewAmt + (nUnitPrice * nQtyOrdered) ElseIf CInt(sKitLineKey) = CInt(sKitLineKeyTrigger) Then sKey = oBusObj.GetKey() End If End If retVal = oBusObj.MoveNext Loop 'Go to Parent Line, calculate UnitPrice and set it retVal = oBusObj.EditLine(sKey) If retVal > 0 Then retVal = oBusObj.GetValue("QuantityOrdered", nQtyOrdered) If nNewAmt <> 0 And nQtyOrdered <> 0 Then nUnitPrice = Round(nNewAmt/nQtyOrdered,2) Else nUnitPrice = 0 End If retVal = oBusObj.SetValue("UnitPrice", nUnitPrice) retVal = oBusObj.Write() End If End If retVal = oBusObj.MoveLast
Button Script (on DMAIN in SO Entry):
oScript.LinesAdded = 1 myretVal = oScript.LoadGrid("GD_Lines") myretVal = oUIObj.HandleScriptUI()