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.

Parents Reply Children
  • +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.

  • 0 in reply to Pinkesh

    You could try forcing the PreTotals event.

  • +1 in reply to Kevin M
    verified answer

    Thank kevin, 

    I have used nPriceRecalculation and nSalesTaxRecalculation methods and it works for me.