Button Script to delete Sales order with cancel code

Hello all,

I wrote a script with the goal to delete sales orders with a specific condition. So I can delete sales orders just fine its just that when the orders are deleted the cancel reason code is not flowing to history it is staying blank. I am even providing the value in the script. This is a button script that sits on Tab 1 of SO entry. I have tried a variation of code to force the UI (sCancelReasonCode = "" : oUIObj.InvokeLookup "SO_CancelReason", sCancelReasonCode) to bring up the cancel reason window and then it does go to history with a a code but the order status is left as "A". I would really like to just press the button it finds the orders I want to delete and it just goes to history with the cancel code I have chosen in the code. Code below. Any help would be appreciated.

  • sMsg = ""
    sItem = ""
    sDItem = ""
    CustDiv = ""
    CustNo = ""
    sSP = ""
    sSO=""
    sPO=""
    sOT=""
    sStatus=""
    sCode=""
    If UserCode = "VAB" or UserCode = "SDM" or UserCode = "KNT" or UserCode = "DSP" or UserCode = "JLN" or UserCode = "CLJ" or UserCode = "DAB" or UserCode = "REE" or UserCode = "SNN" or UserCode = "CMV" or UserCode = "NAT"or UserCode = "KNM" then
    Dim retVal
    retVal = oBusObj.GetValue("SalesOrderNo$", sSO)
    sMsg = "You are about to delete Sales Orders please make sure that the Div and Cust and Order Type are right in the Code." + Chr(13)+ Chr(10)+ Chr(13)+ Chr(10)+" Proceed?"
    retMsg = oSession.AsObject(oSession.UI).MessageBox(".Y2", sMsg)

    If retMsg = "NO" then
    sMsg = "Deletion has been canceled"
    retMsg = oSession.AsObject(oSession.UI).MessageBox("", sMsg)
    retVal = oScript.SetError ("Delete Canceled")
    ElseIf retMsg = "YES" then
    sMsg = "Last chance to cancel please make sure that the Div and Cust and Order Type are right in the Code.!" + Chr(13)+ Chr(10)+ Chr(13)+ Chr(10) & " Proceed?"
    retMsg = oSession.AsObject(oSession.UI).MessageBox(".Y2", sMsg,"icon=!")
    If retMsg = "NO" then
    sMsg = "Deletion has been canceled"
    retMsg = oSession.AsObject(oSession.UI).MessageBox("", sMsg)
    retVal = oScript.SetError ("Delete Canceled")
    ElseIf retMsg = "YES" then

    Dim oSales
    Set oSales = oSession.AsObject(oSession.GetObject("SO_SalesOrder_bus"))
    retVal = oSales.MoveFirst()
    do while oSales.EOF <> 1
    retVal = oSales.GetValue("ARDivisionNo$", CustDiv)
    retVal = oSales.GetValue("CustomerNo$", CustNo)
    retVal = oSales.GetValue("OrderType$", sOT)
    retVal = oSales.GetValue("CustomerPONo$", sPO)
    retVal = oSales.GetValue("SalesOrderNo$", sSalesOrderNo)
    IF sPO = "RSK" then
    retVal = oSales.SetValue("OrderStatus$", "H")
    'retVal = oSales.SetValue("CancelReasonCode$", "RSK")
    retVal = oSales.SetValue("CustomerPONo$", sPO & " Completed")
    retVal = oSales.Write()
    retVal = oSales.Delete()
    retVal = oSales.MoveNext()
    Else

    retVal = oSales.MoveNext()

    End If


    Loop
    sMsg = "All Sales order for your Criteria have been deleted."
    retMsg = oSession.AsObject(oSession.UI).MessageBox("", sMsg)
    End If
    End if
    End IF

  • in reply to David Speck

    Hello David, Thats where I got some of the code to start with to invoke the cancel reason panel to populate but, the thing is I want the code to go to history without involving that window. I am trying to pass the value in my code above.

  • in reply to Bareus16

    Did you see the property called SaveDeletedReason?  It was in that post I linked which should be among the last posts in that thread.  It is a property of the business object, not a table column so you don't use SetValue on it but instead set it like I did in my example.

    oBusObj.SaveDeletedReason = "YourReasonToCancelGoesHere"

  • in reply to David Speck

    I actually tried this but errors out from a button.

  • in reply to Bareus16

    Are you setting the property to a valid string and on file cancel/reason code?

  • in reply to David Speck

    Yes, I made a code RSK. Running it now to post the error.

  • in reply to Bareus16

    I got it to work with oSales.SaveDeletedReason = "RSK". 

  • Final Code

    'Deletes all CSPs
    UserCode = MAS_SCR_USR
    PriceCodeRec = ""
    PriceCode = ""
    sMsg = ""
    ExpireDate = ""
    sYDate = Year(date) & Right("0" & Month(date),2) & Right("0" & Day(date),2)
    sItem = ""
    sDItem = ""
    CustDiv = ""
    CustNo = ""
    sSP = ""
    sSO=""
    sPO=""
    sOT=""
    sStatus=""
    sCode=""
    If UserCode = "VAB" or UserCode = "SDM" or UserCode = "KNT" or UserCode = "DSP" or UserCode = "JLN" or UserCode = "CLJ" or UserCode = "DAB" or UserCode = "REE" or UserCode = "SNN" or UserCode = "CMV" or UserCode = "NAT"or UserCode = "KNM" then
    Dim retVal

    sMsg = "You are about to delete Sales Orders please make sure that the Div and Cust and Order Type are right in the Code." + Chr(13)+ Chr(10)+ Chr(13)+ Chr(10)+" Proceed?"
    retMsg = oSession.AsObject(oSession.UI).MessageBox(".Y2", sMsg)

    If retMsg = "NO" then
    sMsg = "Deletion has been canceled"
    retMsg = oSession.AsObject(oSession.UI).MessageBox("", sMsg)
    retVal = oScript.SetError ("Delete Canceled")
    ElseIf retMsg = "YES" then
    sMsg = "Last chance to cancel please make sure that the Div and Cust and Order Type are right in the Code.!" + Chr(13)+ Chr(10)+ Chr(13)+ Chr(10) & " Proceed?"
    retMsg = oSession.AsObject(oSession.UI).MessageBox(".Y2", sMsg,"icon=!")
    If retMsg = "NO" then
    sMsg = "Deletion has been canceled"
    retMsg = oSession.AsObject(oSession.UI).MessageBox("", sMsg)
    retVal = oScript.SetError ("Delete Canceled")
    ElseIf retMsg = "YES" then

    Dim oSales
    Set oSales = oSession.AsObject(oSession.GetObject("SO_SalesOrder_bus"))
    retVal = oSales.MoveFirst()
    do while oSales.EOF <> 1
    retVal = oSales.GetValue("ARDivisionNo$", CustDiv)
    retVal = oSales.GetValue("CustomerNo$", CustNo)
    retVal = oSales.GetValue("OrderType$", sOT)
    retVal = oSales.GetValue("CustomerPONo$", sPO)


    IF Left(sPO,13) = "RSK Completed" then
    retVal = oSales.SetValue("OrderStatus$", "H")
    retVal = oSales.SetValue("CancelReasonCode$", "RSK")
    'retVal = oSales.SetValue("CustomerPONo$", sPO & " Completed")
    retVal = oSales.Write()
    oSales.SaveDeletedReason = "RSK"
    'oBusObj.Delete sSalesOrderNo
    retVal = oSales.Delete()
    retVal = oSales.MoveNext()
    Else

    retVal = oSales.MoveNext()

    End If


    Loop
    sMsg = "All Sales order for your Criteria have been deleted."
    retMsg = oSession.AsObject(oSession.UI).MessageBox("", sMsg)
    End If
    End if
    End IF

  • in reply to Bareus16

    This is a button script in Sales Order Entry, correct?  

    If yes, there is no need to get a separate object handle to SO_SalesOrder_Bus when you can use oBusObj.  However, since it is a button script and buttons added via Customizer are normally only enabled when a record is loaded, you should use the methods in the other thread to make sure to save or clear the current record before looping through the records.