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

  • +1
    verified answer

    It looks like this script is attached to the Post-Write of the SO_SalesOrderDetail business object.   It's important to know that the data entry model, writes the line detail to an internal memory file and is committed to the physical table when the entire sales order is written.  The script on the post-write of the detail object allows us to act on each line as it's written to the temporary table.

    You should also have a Post-Delete script that mirrors the Post-Delete in that it backs out the quantities, this way if a line is deleted it reduces the salespersons totals as well.  If you have both scripts, I believe you will have the desired effect.

    If the order is cancelled it should run the Post-Delete script of each line to reverse your changes.

    Hope this helps, Elliott

  • +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 jepritch

    Thank you very much! I was able to get some better results, but now that I understand how this works I have realized that having this on the header will be a better solution. Thanks again, very helpful. 

  • 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!