Script on SO Lines, Visual Bug

SUGGESTED

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()

  • 0

    refreshing the lines gets rather complicated.  however, i noticed you are using LinesAdded = 1, have you tried LinesAdded = -1?

  • 0 in reply to David Speck

    Hi David, yes, unfortunately the kit component lines still duplicate in the UI with LinesAdded = -1.

  • 0 in reply to JasonKonopack
    SUGGESTED

    I'm thinking it is because you are moving the Lines object off of the record it was on in the middle of a kit's components.  Funky things happen when you change the current key during an event script on the detail object.  Try saving the key into a variable at the top of your script, should be able to use something like sCurrentKey = oBusObj.GetKeyPadded().  Then at the end of your script, use oBusObj.SetKey sCurrentKey. 

    Also, since this is triggered on the post write, the script will be triggered when you write the changes to the parent kit item so you need to account for that and make sure you don't recursively loop too much.  There are a couple ways around this.

    1. Make sure your If Then Else conditions will filter for only the actual times it should proceed with your logic.
    2. Use oScript.DeactivateProcedure and oScript ActivateProcedure to surround your lines of code that would trigger a recursive loop.
    3. Check the oScript.RefCount property at the beginning of your script, if 1, it is the first occurrence of the script triggering.  If anything greater than 1, then it is triggering recursively.