Post-Write Script is firing before the record is written.

SOLVED

Hello, 

I am trying to write a simple tracking script that records the number of items within a specific product line a salesperson has purchased. I currently have this script as "Table: Post-Write", but even if I cancel my order in Sales Order Entry, the quantity ordered is added to my tracking UDT. 

What do I need to do to my script to ensure that it does not fire unless the sales order is placed? 


'-----------------------

'Initialize Variables
SP = ""
ItemC = ""
pLine = ""
Qty = 0
QtyOld = 0
cap = 0

price = 0

'--------------------------

'Grab Data
retVal = oHeaderObj.GetValue("SalesPersonNo$", SP)
retVal = oBusObj.GetValue("ItemCode$", ItemC)
retVal = oBusObj.GetValue("QuantityOrdered", Qty)
retVal = oBusObj.GetValue("UnitPrice", price)

set oCap = oSession.AsObject(oSession.GetObject("CM_UDTMaint_bus", "AR_UDT_SALESPERSON_CAP"))
set oItem = oSession.AsObject(oSession.GetObject("CI_ItemCode_bus"))
retVal = oItem.GetValue("ProductLine$", pLine)
'-----------------------------

'Set Values

resul = oCap.SetKey(SP + pLine)

If resul = 2 Then
result = oCap.SetValue("UDF_Current_Qty", Qty)
result = oCap.Write()
End If

If resul = 1 Then
result = oCap.GetValue("UDF_Current_qty", QtyOld)
result = oCap.SetValue("UDF_Current_qty", QtyOld + Qty)
result = oCap.Write()
End If

'--------------------------------

Top Replies

Parents
  • +1
    verified answer

    PostWrite on the Details table happens during line data entry.  If you want to have it only run when saving an order, switch it to a Header trigger, and loop through lines.

  • 0 in reply to Kevin M

    Thanks Kevin, it sounds like your solution is what I'm looking for! Will I still be able to use my

     set oItem = oSession.AsObject(oSession.GetObject("CI_ItemCode_bus")) line inside of oLines? 

  • 0 in reply to Surya IT

    You will have to retrieve the object handle via the oLines object, something like:

    Set oLines = oSession.AsObject(oBusObj.Lines)

    Set oItem = oSession.AsObject(oLines.GetChildHandle("ITEMCODE"))

    I'm not sure your logic above would have worked as it is in your example.

    But once you have the oItem above you should be able to use it for your GetValue() of the ProductLine.

    Thanks

    Elliott

  • 0 in reply to jepritch

    I feel I am very close, Thanks for all the help.  Right now, this writes to my table perfectly with the two below lines commented out. However, I get an error when uncommenting those two lines. Does this have to do with the fact that the one that works is changing the Key, and the other is changing a non-key value? The error is: "Cannot use parentheses when calling a Sub" 

    Here is the code in question: 


    Set oLines = oBusobj.AsObject(oBusObj.Lines)
    oLines.MoveFirst()
    do Until cBool(oLines.EOF)
    Set oItem = oSession.AsObject(oLines.GetChildHandle("ITEMCODE"))
    retVal = oItem.GetValue("ProductLine$", pLine)
    'retVal = oItem.GetValue("QuantityOrdered", Qty)
    resul = oCap.SetKey(SP + Pline)
    'oCap.SetValue("UDF_Current_qty", Qty)
    oCap.Write()
    retVal = oLines.MoveNext()
    Loop

  • 0 in reply to Surya IT

    QuantityOrdered needs to come from oLines, not oItem.

  • 0 in reply to Kevin M

    Thanks, now when I uncomment the first commented line there is no error, but I still receive the same error when I uncomment the second commented line. 

  • 0 in reply to Kevin M

    I have: 

    Set oLines = oBusobj.AsObject(oBusObj.Lines)
    oLines.MoveFirst()
    do Until cBool(oLines.EOF)
    Set oItem = oSession.AsObject(oLines.GetChildHandle("ITEMCODE"))
    retVal = oItem.GetValue("ProductLine$", pLine)
    retVals = oLines.GetValue("QuantityOrdered", Qty)
    resul = oCap.SetKey(SP + Pline)
    oCap.SetValue("UDF_Current_qty", Qty)
    oCap.Write()
    retVal = oLines.MoveNext()

    and get this error: 

  • 0 in reply to Surya IT

    I got it to work by removing the parentheses...duh. Thanks again!

Reply Children