Add Order Misc Charge BPM

I am getting stuck. GetNewOHOrderMsc does not seem to be populating a new row in the OrderMsc table for me to use. That NewMiscCharge variable comes back null every time. Is there something I am missing here?

var OrderInfo = (from o in ttOrderHed
                 where o.RowMod == "U" && o.Company == callContextClient.CurrentCompany
                 select o).FirstOrDefault();
if (OrderInfo != null)
{    
  var OrderNum = OrderInfo.OrderNum;
  var CustNum = OrderInfo.CustNum;
  decimal OrderAmt = OrderInfo.OrderAmt;
  decimal Pct = .03M;

  info += "OrderNum: " + OrderNum + "\nCustNum: " + CustNum + "\nOrderAmt: " + OrderAmt + "\nPct: " + Pct + "\n";
  
  Erp.Contracts.SalesOrderSvcContract boSalesOrder = Ice.Assemblies.ServiceRenderer.GetService<Erp.Contracts.SalesOrderSvcContract>(Db);
  Erp.Tablesets.SalesOrderTableset dsSalesOrder = new Erp.Tablesets.SalesOrderTableset(); 
  
  dsSalesOrder = boSalesOrder.GetByID(OrderNum);

  boSalesOrder.GetNewOHOrderMsc(ref dsSalesOrder, OrderNum, 0);
 
  var NewMiscCharge = (from m in dsSalesOrder.OrderMsc
                       where m.RowMod == "A" && m.Company == callContextClient.CurrentCompany
                       select m).FirstOrDefault();

//  if (NewMiscCharge != null)
//  {    
//    bool lCheckForOrderChangedMsg = true;
//    bool lcheckForResponse = true;
//    string cTableName = "OHOrderMsc";
//    int iCustNum = CustNum;
//    int iOrderNum = OrderNum;
//    bool lweLicensed = false;
//    bool lContinue = false;
//    string cResponseMsg = "";
//    string cCreditShipAction = "";
//    string cDisplayMsg = "";
//    string cCompliantMsg = "";
//    string cResponseMsgOrdRel = "";
//    string cAgingMessage = "";

    NewMiscCharge.MiscCode = "CCFE";
    boSalesOrder.ChangeMiscCode(ref dsSalesOrder, "OHOrderMsc");    

    NewMiscCharge.MiscAmt = OrderAmt * Pct;
    boSalesOrder.ChangeMiscAmount(ref dsSalesOrder, "OHOrderMsc");
    
    NewMiscCharge.FreqCode = "F";  
    NewMiscCharge.SeqNum = 1;    

    boSalesOrder.Update(ref dsSalesOrder);
//  }

}

I know that I am getting my OrderInfo to populate as well as the dsSalesOrder. I can write a different query to the OrderDtl table within the dataset and see the information from my order. But it doesn’t seem to be setting up the new row in OrderMsc.

Only thing I can think of is that you also need to set a few more variables, before Update.

 NewMiscCharge.CurrencySwitch = false;
 NewMiscCharge.DocDspMiscAmt = miscAmount;
 NewMiscCharge.DocMiscAmt = miscAmount;
 NewMiscCharge.Type = "A";

I added those… but my problem seems to be here:

  boSalesOrder.GetNewOHOrderMsc(ref dsSalesOrder, OrderNum, 0);
 
  var NewMiscCharge = (from m in dsSalesOrder.OrderMsc
                       where m.RowMod == "A" && m.Company == callContextClient.CurrentCompany
                       select m).FirstOrDefault();

I am not getting this row to be retrieved from the query. NewMiscCharge comes back null. Is there a better way to “get a new misc charge”?

Bah. I think I found it. I needed to be using OHOrderMsc in the from statement. Should’ve been:

  var NewMiscCharge = (from m in dsSalesOrder.OHOrderMsc
                       where m.RowMod == "A" && m.Company == callContextClient.CurrentCompany
                       select m).FirstOrDefault();

Any idea why I would keep getting the “Row has been modified by another user and couldn’t be updated” BLE? It seems to happen anytime I set the DocDspMiscAmount field. If I don’t set that field, the amount does not show up on the misc charge. It just shows 0. Even after I get the BLE, it still adds the charge but it doesn’t save the change I made to the header to initiate the BPM to execute.

I believe it’s related to the fact that when I add the misc charge successfully, it forces an update to the OrderHed table as it changes the OrderAmt. So the OrderHed row gets locked at that time.

Ok. So then my question is: is there a better way to do what I am trying to do other than a pre-processing on the MasterUpdate?