User Defined Script - Problems Getting Child Handle

SOLVED

I'm trying to accumulate the quantities of all the detail lines in an AR invoice, but only if the item is of a certain type.  I defined a UDF for that checkbox, and put it in the Item Maintenance screen.

I would like the total to update whenever a user commits a detail line.  So if they change a quantity, as soon as they leave the line I would like for the total to update.

I attached a User Defined Script to AR Invoice Detail's PostWrite event (also tried on PreWrite).

The script first gets the header, then the header gets all the lines. 

We loop through the lines, and for each one we get the corresponding Item for it, so that we can check the item for the presence of the UDF.

This is where it breaks down.  It's not displaying any of the properties of the item.  

The script is below.  Note that there are a lot of MsgBox statements in there, which we would never do in real life.  But for testing purposes it helps.

' Fires on AR Invoice Detail - Post Write

' First we get all of the lines
Set oLines = oSession.AsObject(oHeaderObj.Lines)

totalQuantity = 0
footage = ""
itemCode = ""
lineItemCode = ""

result = oLines.MoveFirst()

Do Until oLines.EOF
    ' This works.  
	result = oLines.GetValue("ItemCode$", lineItemCode)
	MsgBox "Item Code from detail line is " & lineItemCode

    ' Get the Item record for this detail line
	Set oItem = oSession.AsObject(oLines.GetChildHandle("ItemCode"))

    ' This doesn't work.  In fact, none of the GetItems from the oItem work.  
    ' Every property is blank.  Note that result winds up being 1, which is no error
	result = oItem.GetValue("ItemCode$", itemCode)
	MsgBox "Item Code from item is " & itemCode
		    
	result = oItem.GetValue("UDF_FOOTAGE$", footage)
	MsgBox "footage is " & footage

    ' This never gets hit, because the footage variable is never set
	if footage = "Y" Then
		MsgBox "Footage Item"
		qty = 0
		result = oLines.GetValue("Quantity", qty)
		MsgBox "quantity is " & qty
		totalQuantity = totalQuantity + qty
	End If

	result = oLines.MoveNext()
Loop

MsgBox totalQuantity

result = oHeaderObj.SetValue("UDF_TOTAL_FOOTAGE", totalQuantity)

Parents Reply Children
  • 0 in reply to Aaron Edwards

    Aaron,

    If you are updating a total, I think this may not be the most efficient way to do that.  You still need some of the logic, but going through all the lines after the write of each line, is probably overkill.  You need several steps to ensure this is accurate.

    Post-Read AR Invoice Detail

    * save off previous value of item, and qty into storage variables

    Post-Write AR Invoice Detail

    * retrieve item and udf_footage (from item service), and qty

    * retrieve storage variables for original line values of item and qty

    * if old item is different than the new item, lookup udf_footage for old.item 

    * retrieve total quantity from header using oHeaderObj.GetValue()

    * then do your calculation hdrQty = hdrQty + (new Qty - orig Qty) - do a oHeaderObj.SetValue() to update that value.

    I think reading through all the lines in the middle of writing a line is probably messing things up.

  • 0 in reply to jepritch

    You're probably right, that reading through all of the lines while writing a line is the culprit.  We wanted to show a current total in the header that updates whenever you change a line; that's why I'm trying it this way.