Script to update child line

SOLVED

I wrote a script sales order detail post write script that creates a child line that is a percentage of the parent line. If the updates the parent line quantity, unit price or extension the script updates the child line, BUT does not display the change in the grid until you go to another tab, then back to the lines. I have tried using the add line function, but still does not refresh the grid line.  

'Get Values from parent line

Set oUI = oSession.AsObject(oSession.UI)

retval = oHeaderObj.getvalue("ARDivisionNo$",ARDiv)
retval = oHeaderObj.getvalue("CustomerNo$",Customerno)
retval = oBusObj.getvalue("SalesOrderNo$",SalesOrderNo)
retval = obusObj.getvalue("LineKey$",ParentLineKey)
retval = obusObj.getvalue("QuantityOrdered",Quantity)
retval = oBusObj.getvalue("UDF_TARIFF_AMT",TariffAmt)
retval = obusObj.getvalue("UDF_TARIFFLINEKEY$",ChildLineKey)

if Quantity <> 0 then UnitPrice = TariffAmt / Quantity

'Locate child line

if ChildLineKey <> "" then


retVal = oBusObj.MoveFirst()

do until cBool(oBusObj.EOF)


retval = obusObj.getvalue("LineKey$",LineKey)


'Checks if the line is the child
if Linekey = ChildLineKey then


retVal = oBusObj.SetValue("QuantityOrdered",Quantity)
retVal = oBusObj.SetValue("UnitPrice",UnitPrice)
if retval <> 1 then oui.messagebox(oBusObj.LastErrorMsg)
retVal = oBusObj.Write()

'Refresh the grid
oScript.LinesAdded = -1
oScript.LoadGrid "GD_Lines"

End if

oBusObj.movenext
loop


End if

Any help would be greatly appreciated.

Justin 

Parents
  • 0

    I think i have a solution somewhere for this.

    Also, you should probably place you oBusObj.Write between a oScript.DeactivateProcedure and oScript.ActivateProcedure line so you don't end up recursively looping through the lines.

    Here are more details on the two methods from the scripting.docx.

    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.

    EDIT: Removed code that wasn't working and removed redundant info that can be obtained from my latest answer.

  • 0 in reply to David Speck

    Hi David,

    I am trying to use the oBusObj.EditLine . The message box returns the key I want to update, but the error message is referencing a linekey that does not exists on the order. Below is the code with screenshots inserted. Any advise would be greatly appreciated.

    Script is on post write on the Sales Order Detail business object. 

    'Other code above

    retval = oBusObj.getvalue("SalesOrderNo$",SalesOrderNo)
    retval = obusObj.getvalue("UDF_TARIFFLINEKEY$",TariffLineKey)

    slinkey = SalesOrderNo & TariffLineKey

    oui.messagebox(slinkey)

     

    retval = oBusObj.EditLine(slinekey)

    'Retval = 2 

    if retval <> 1 then oui.messagebox(oBusObj.LastErrorMsg)

     

     

     

    Here is the record in DFDM

     

  • 0 in reply to justinaroberts

    EditLine MUST be passed the full kDisplay key. In this case, that is SalesOrderNo+LineSeqNo+LineKey.

    GetEditKey only works for detail records that have been committed to the physical file,this occurs when you click Accept in Sales Order Entry. 

    After using EditLine, instead of checking the return value, trying using a message box to display the value returned from the GetKey method. Sometimes LastErrorMsg doesn't clear between methods and events and you could go down a rabbit hole trying to resolve a false positive. If you absolutely must trap an error, before you call the method that is prone to errors, set the LastErrorMsg property to a blank string. The GetKey method will tell you which key you are on, it should be a combination of the SalesOrderNo+LineSeqNo+LineKey.

    EDIT: Corrected details on the EditLine method.

  • +1 in reply to David Speck
    verified answer

    So a little testing revealed EditLine expects the key defined by the KDisplay index, which is SalesOrderNo+LineSeqNo+LineKey. GetEditKey will return this if you pass it a LineKey but it will not work on new lines added to an order that have not been committed to the physical file yet.

    So this means either loop though the lines or, assuming no one ever reorders the lines, store the full KDisplay key in the UDF instead of just the LineKey, this will be a problem if someone does resequence a line because the LineSeqNo will change but if you don't script a way to update the full key value stored in the UDF, it will no longer match.

    Hopefully that all makes sense.

    You might just be better off looping through the lines after all.

Reply
  • +1 in reply to David Speck
    verified answer

    So a little testing revealed EditLine expects the key defined by the KDisplay index, which is SalesOrderNo+LineSeqNo+LineKey. GetEditKey will return this if you pass it a LineKey but it will not work on new lines added to an order that have not been committed to the physical file yet.

    So this means either loop though the lines or, assuming no one ever reorders the lines, store the full KDisplay key in the UDF instead of just the LineKey, this will be a problem if someone does resequence a line because the LineSeqNo will change but if you don't script a way to update the full key value stored in the UDF, it will no longer match.

    Hopefully that all makes sense.

    You might just be better off looping through the lines after all.

Children
  • 0 in reply to David Speck

    For completeness, another way to do this would be to use the Browse Index and Browse Filter. You do want to be careful and check and save off any current values for the current key (use the GetKey method) CurrentBrowseIndex property and use GetValue on "cBrowseFilter". Save these into variables and then use the SetBrowseIndex method followed by the SetBrowseIndex method followed my the MoveFirst method, if the MoveFirst method returns 0, then no record was found matching the Browse Filter for the specified Browse Index. After locating the line, you can use GetKey to store its full key, then set the browse filter back to what it was previously using the variable you stored it into followed by setting the Browse Index to what it was previously followed by using EditLine on the correct key. Once done editing the line, use EditLine on the original key that you stored in a variable. Then refresh the grid.