Scripting Dilemma (Pre vs Post Table write)

Hello, 

I am working on a scripting solution that sets a hard cap on the number of free sample items a salesperson can receive. I currently have a UDT set up that tracks how many of each product line a sales person has, and am able to write to that table fine. However, I want to be able to set the price to 0 if they have not reached the limit, but keep the price at the normal price if they have reached the limit. My two current problems are the following: 

       1) It appears that to change the price, I need to have the price change to be on a script that is not of the Post-Table-Write type. However, doing so causes the item quantities to be written to my UDT even when the user cancels the order. If anyone knows a simple solution to this problem I would be very grateful.

       2) If the cap for the product line "Rugs" is 10, and the salespersons current quantity is at 5, they are allowed to purchase as many different rugs as they want as long as each specific rug has quantity less than 5. I would like to be able to update the current quantity on the UDT as the order is placed, but for changes to be disregarded if the order is cancelled. 

The below script currently works great for recording the number of free samples that have been used, but is incomplete as I'm not sure where to go from here (One script, multiple scripts, etc), and I have included it to give you all an idea of where I'm at:

This is currently set up as Post Table Write:

'Sales Person Free Sample Cap
'Initialize Variables


SP = ""
ItemC = ""
pLine = ""
Qty = 0
QtyOld = 0
price = 0
cap = 0
BSWA = 0
CORN = 75
PILL = 35
THRO = 5
VAS = 3
LIG = 3
WALL = 1


'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", "SO_UDT_Cap"))
set oItem = oSession.AsObject(oSession.GetObject("CI_ItemCode_bus"))
retVal = oItem.GetValue("ProductLine$", pLine)


'Write to Table

resul = oCap.SetKey(SP + pLine)

If resul = 2 then
result = oCap.SetValue("UDF_Current", Qty)
result = oCap.SetValue("UDF_SALESPERSON$", SP)
result = oCap.Write()
End If

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

Any guidance in this will be greatly appreciated!

  • 0

    If you must write to the current line in a Post-Write event, you need to get the current key and use that to put the line back in an editable state, make your changes, deactivate the pre and/or post write event, and then "write" the line.

    in a post-write on the detail, you can use either of these two approaches to edit the line.


    sCurrentDetailLineKey = "" : oBusObj.GetValue "LineKey$", sCurrentDetailLineKey

    sCurrentDetailKey = "" : sCurrentDetailKey = oBusObj.GetEditKey(sCurrentDetailLineKey)

    oBusObj.EditLine sCurrentDetailKey


    Or


    sCurrentDetailKey = "" : sCurrentDetailKey = oBusObj.GetKey()

    oBusObj.EditLine sCurrentDetailKey


    Once you make your changes, before you call oBusObj.Write, you need to deactivate the events so you don't end up in a recursive loop.

    Here is more info from the Scripting.docx file.


    retVal = oScript.ActivateProcedure
    (proc_name as String)
     

    and


    retVal = oScript.DeactivateProcedure
    (proc_name as String)

    These are used to give the script author the ability to avoid recursive calls. For example, if the PostValidateQuantityShipped(col, val) procedure script was to invoke the oBusObj.SetValue(“QuantityShipped”, val), this would in turn cause the PostValidateQuantityShipped(col, val) procedure to be called again. To avoid this, call retVal = oScript.DeactivateProcedure(“PostValidateQuantityShipped”) prior to the SetValue() call, then use the retVal=oScript.ActivateProcedure(“PostValidateQuantityShipped”) to reactivate this procedure.

    Note: Use retVal = oScript.DeactivateProcedure(“*ALL*”) to deactivate/activate all procedures for the current business object.

    Note: Use retVal = oScript.Deactivate(“*”) to deactivate/activate the current procedure.


    So you are going to want to sandwich your oBusObj.Write after the oScript.DeactivateProcedure and before the oScript.ActivateProcedure. It is up to you to determine which argument you will pass to those methods because you may want to deactivate all or maybe just the post write if you have other scripts on the pre write that you still want to execute.

  • 0 in reply to David Speck

    Hi 

    I have a similar requirement, but the UDF is on the Header Table where as the data is in a different table, Any similar solution approach would be very helpful. 

    Thanks in advance.

    AJ

  • 0 in reply to AJ@Marki

    If you have more details, that would help but you may want to start your own thread. Generally, if you just need info from another table who's handle already exists in memory because it is a part of the child collection then you can use the GetChildHandle method. The child handle is, as far as i know, always going to be for the _Svc version of the class so you can't use it to modify or create records but you can use it for read only purposes. If you have to modify or create records, you will need to use the GetObject method, depending on the task your script is running under, you may also need to incorporate the SetProgram and LookupTask methods to make sure the user the script is running under has the appropriate role settings.