I managed to reproduce the issue also with this simplified code:
int docEntry = 1800;
IDocuments ord = (IDocuments)oCompany.GetBusinessObject(BoObjectTypes.oOrders);
try
{ //locate order ord.GetByKey(docEntry); //locate the row ord.Lines.SetCurrentLine(0); Debug("Before populate:"); Debug("ord.Lines.UnitPrice = " + ord.Lines.UnitPrice); Debug("ord.Lines.DiscountPercent = " + ord.Lines.DiscountPercent); Debug("ord.Lines.LineTotal = " + ord.Lines.LineTotal); //populate fields! ord.Lines.ItemCode = "OTHER_ITEM"; ord.Lines.UnitPrice = 800; ord.Lines.LineTotal = 308; Callback("After populate:"); Callback("ord.Lines.UnitPrice = " + ord.Lines.UnitPrice); Callback("ord.Lines.DiscountPercent = " + ord.Lines.DiscountPercent); Callback("ord.Lines.LineTotal = " + ord.Lines.LineTotal); //commit data to B1 int result = ord.Update(); if (result < 0) Callback(CommonsB1.oCompany.GetLastErrorDescription()); Debug("After Update:"); Debug("ord.Lines.UnitPrice = " + ord.Lines.UnitPrice); Debug("ord.Lines.DiscountPercent = " + ord.Lines.DiscountPercent); Debug("ord.Lines.LineTotal = " + ord.Lines.LineTotal); ord.GetByKey(docEntry); Debug("After Update and GetByKey:"); Debug("ord.Lines.UnitPrice = " + ord.Lines.UnitPrice); Debug("ord.Lines.DiscountPercent = " + ord.Lines.DiscountPercent); Debug("ord.Lines.LineTotal = " + ord.Lines.LineTotal);
}
catch (Exception e)
{ Debug(e.Message);
}
finally
{ if (ord != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(ord); GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); }
}The starting situation is a sales order with just one line in it, with an item with code "AN_ITEM" and dafault values for the other line fields.
Normally this code should change the item in the order line, loading its defaults, but also setting unit price and line total and calculate the correct discount percent.
The output produced by Debug method is:
Before populate: ord.Lines.UnitPrice = 0 ord.Lines.DiscountPercent = 0 ord.Lines.LineTotal = 0 After populate: ord.Lines.UnitPrice = 800 ord.Lines.DiscountPercent = 0 ord.Lines.LineTotal = 308 After Update: ord.Lines.UnitPrice = 800 ord.Lines.DiscountPercent = 0 ord.Lines.LineTotal = 308 After Update and GetByKey: ord.Lines.UnitPrice = 800 ord.Lines.DiscountPercent = 0 ord.Lines.LineTotal = 800
while the final part should be:
ord.Lines.UnitPrice = 800 ord.Lines.DiscountPercent = 61.50 ord.Lines.LineTotal = 308
as it happens when Adding an order instead of Updating it.