Delete Sales Order detail

SOLVED

Hi,

I got this "The Records is not in editable state" Error while delete details from SO_SalesOrderDetail_bus object. 

Please anyone give hint to solve this issue.

Given below is my code

int TaskID = (int)oSS.GetType().InvokeMember("nLookupTask", BindingFlags.InvokeMethod, null, oSS, new object[] { "SO_SalesOrder_ui" });
oSS.GetType().InvokeMember("nSetProgram", BindingFlags.InvokeMethod, null, oSS, new object[] { TaskID });
so_sales_svc = pvx.GetType().InvokeMember("NewObject", BindingFlags.InvokeMethod, null, pvx, new object[] { "SO_SalesOrderDetail_bus", oSS });
try
{
object[] s_no = new object[] { "SalesOrderNo$", "0000103" };

ParameterModifier pmods1 = new ParameterModifier(s_no.Length);
for (int x = 0; x < s_no.Length; x++) pmods1[x] = true;
ParameterModifier[] pMods1 = new ParameterModifier[] { pmods1 };
retVal = so_sales_svc.GetType().InvokeMember("nSetKeyValue", BindingFlags.InvokeMethod, null, so_sales_svc, s_no, pMods1, null, null);
Console.WriteLine(retVal.ToString());
retVal = so_sales_svc.GetType().InvokeMember("nSetKey", BindingFlags.InvokeMethod, null, so_sales_svc, null);
Console.WriteLine(retVal.ToString());
so_sales_svc.GetType().InvokeMember("nDelete", BindingFlags.InvokeMethod, null, so_sales_svc, null);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}

Thanks.

  • 0

    Greetings,

    Service objects are read only.  If you want to delete a record you need to use the business object.  Start by changing the reference to use SO_SalesOrder_bus.

    Thanks,
    Kent

  • 0 in reply to Kent Mackall

    My first thought too,  but it appears he is already using the BUS object.

  • 0 in reply to TomTarget

    Tom,

    I missed the assignment of SO_SalesOrderDetail_bus.  However, if deleting the entire order it should still use SO_SalesOrder_bus not SO_SalesOrderDetail_bus.  If the intent is to get a Sales Order and then delete all the lines on the order, then that would be the same as deleting the entire order.  If the intent is to delete a specific line or set of lines on the order then using SO_SalesOrder_Detail_bus would be correct and there should be a loop to go through all the lines and selecting/deleting them. There should also be a reference to SO_SalesOrder_bus (header object) to set the key (put order into edit state) and to write the sales order to commit the updated order with the lines removed.

    I know there are plenty of examples of looping through the line detail on the forum

    Thanks,

    Kent

  • 0 in reply to Kent Mackall

    Hi Kent,

    Thanks for the reply. I want to delete all line items based on given sales order. So what I understood is, I have to use loop to delete line items.

  • 0 in reply to Pinkesh

    If you are deleting ALL the lines then why not just delete the entire order?

  • 0 in reply to Kent Mackall

    I want to delete line items only.

    In my code nSetKeyValue and nSetKey returns 0. What can be the reason?

  • +1 in reply to Pinkesh
    verified answer

    Here is some sample code that shows looping through lines and deleting all lines.  Remember if you delete all lines from an order and the order has a zero dollar amount you will not be able to save the order.

    'Assumes that script object has been instantiated and there is a session object (oSS) and a user has been logged on
    ' Setup the Session object for ABC company and S/O module

    r = oss.nsetcompany("ABC")
    r = oSS.nSetDate("S/O","20180511")
    r = oSS.nSetModule("S/O")

    ' Instantiate a Sales Order business object

    s = oSS.nSetProgram(oSS.nLookupTask("SO_SalesOrder_ui"))
    Set o = oScript.NewObject("SO_SalesOrder_bus", oSS)

    ' Create a new header record using the next available order number

    OrderNo = "12345" ' Set to appropriate Order number that already exists
    r = o.nSetKey(OrderNo)


    '*********************************************************
    ' If we did not know the LineKey$ we would use the MoveFirst()
    ' and MoveNext() methods to move through the lines until the
    ' ItemCode was found. Because the "move" methods also set the
    ' edit state, the EditKey$ and the EditLine method would not be
    ' needed. An example of this logic is shown below
    '
    ' ItemCode = ""
    ' r = o.oLines.nMoveFirst()
    ' Do until o.oLines.nEOF=1
    ' r = o.oLines.nGetValue("ItemCode$", ItemCode)
    ' IF ItemCode="GB-MD750" Then Exit Do
    ' r = o.oLines.nMoveNext()
    ' Loop
    ' r = o.oLines.nSetValue("QuantityOrdered", 2)
    ' r = o.oLines.nWrite()
    '**********************************************************

    ' Delete the 6655 item line, again we knew the LineKey$ is
    ' "000002" since this was the second line created. The EditKey$
    ' is required to delete a line. DeleteLine() with no key deletes
    ' all lines, and if inventory is integrated, commits deleted lines
    ' to disk immediately.

    'EditKey = o.oLines.sGetEditKey("000002")
    'r = o.oLines.nDeleteLine(EditKey)

    'Deleate all lines
    r = o.oLines.nDeleteLine()

    ' Write the line change to memory
    ' if all lines deleted this will return 0 and an error message
    r = o.oLines.nWrite()

    ' Write the order header and lines to disk
    ' this will fail if all lines are deleted and the order has a zero dollar amount.
    r = o.nWrite()

    ' Done with the order object
    r = o.DropObject()
    o = 0

    ' End
    Wscript.Echo "DONE"
    Wscript.Quit

  • 0 in reply to Kent Mackall

    What should I do if I want to delete all line items.

    Line items are deleted but order total amount remains same I mean it should be zero.

  • 0 in reply to Pinkesh

    ,  the business rules will not allow the saving of an order that has zero line items and zero for a total.  What would be your purpose to retaining an order that has none of the line detail and a zero total in the active sales order table?  

  • 0 in reply to jepritch

    yes, But i have checked even after deleting single item total is not updated automatically.  Actually after deleting all line items I want to insert new updated line items. I am able to delete all items but total remains same it should become zero before inserting new record.