Hi,
I am creating a production order from sales order (I have shared my code below) Which works fine, but when I attempt to retrieve the production order document number through GetByKey() method after using GetNewObjectCode(), production order line component used in all created production has component of first item (from sales order) instead of using its respective BOM componet. So only 1st created production order component is correct and rest of the production order component are incorrect (has component from 1st BOM, even thought on production order > product no is correct (from sales order) ). So definitely something is not getting refreshed when i am using GetByKey to retrieve the recent production order.
Would anyone be able to advise on if I am using GetByKey() method correctly, would appreciate it a lot? If i remove the GetByKey() method then all created production order gets created with correct component from the BOM.
//Initialize B1 Objects
SAPbobsCOM.Company di = company;
SAPbouiCOM.Application ui = application;
SAPbobsCOM.Recordset rs = null;
SAPbobsCOM.Recordset rs1 = null;
SAPbouiCOM.Form fi = form;
SAPbouiCOM.Form fi_udf = ui.Forms.GetForm("-139", 1);
SAPbouiCOM.Item oItem;
SAPbouiCOM.Matrix oMatrix;
SAPbouiCOM.EditText oEdtTxt;
SAPbouiCOM.EditText oEdtTxtPO;
/* Get the setting value from BRV_KRF_SETTINGS table */
SAPbobsCOM.UserTable BRVsettings = company.UserTables.Item("BRV_KRF_SETTINGS");
int DaysBeforeDispatch =0;
if(BRVsettings.GetByKey("31"))
{
DaysBeforeDispatch = Convert.ToInt32(BRVsettings.UserFields.Fields.Item("U_SETTING_VALUE").Value);
DaysBeforeDispatch = DaysBeforeDispatch * -1;
//ui.MessageBox(Convert.ToString(x));
}
//Get Sales Order Matrix
oItem = fi.Items.Item("38"); //Matrix S0's Object
oMatrix = ((SAPbouiCOM.Matrix)(oItem.Specific));
rs = (SAPbobsCOM.Recordset)di.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);
rs1 = (SAPbobsCOM.Recordset)di.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);
string _xMesg = "";
string _xLog = "";
string xRemark = "Based on Sales Order " + "$[$8.0.0]" + ".";
oItem = fi_udf.Items.Item("U_Log"); //Sales Order Log
oEdtTxt = (SAPbouiCOM.EditText)(oItem.Specific);
_xLog = oEdtTxt.Value;
//Get Sales Order DocEntry
int _iDocEntry = Convert.ToInt32("$[ORDR.DocEntry.0]");
int _iLineNum = 0;
//Get Production Order Details to be added from sales order
//f0 - DocEntry, f1 - VisOrder, f2 - ItemCode, f3 - Qty, f4 - WhsCode, f5 - TreeType, f6 - ProdOrderNo, f7 - DocDucDate, f8 - PostingDate
object sSQL = "SELECT *, GetDate() FROM (SELECT t0.DocEntry, t0.VisOrder + (select COUNT(*) from RDR10 t10 where t10.DocEntry = t0.DocEntry and t10.AftLineNum < t0.VisOrder) As ActualOrder, t0.ItemCode, t0.Quantity, t0.WhsCode, t0.TreeType, isnull(t0.U_ProdOrd,0) As U_prodord, t3.DocDueDate FROM ORDR t3 inner join RDR1 t0 on t3.DocEntry = t0.DocEntry inner join OITT t1 on t0.itemcode = t1.code inner join OITM t2 on t0.itemcode = t2.itemcode where t2.prcrmntmtd = 'M' and t1.treetype = 'P' and isnull(t0.U_prodord,'') = '' and t0.docentry = " + "$[ORDR.DocEntry.0]" + ") As ProdOrdInfo";
rs = (SAPbobsCOM.Recordset)di.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);
rs.DoQuery(System.Convert.ToString(sSQL));
ProductionOrders prodord = (ProductionOrders)di.GetBusinessObject(BoObjectTypes.oProductionOrders);
ProductTrees BOM = (ProductTrees) di.GetBusinessObject(BoObjectTypes.oProductTrees);
while (! rs.EoF)
{
DateTime dueDate = DateTime.Parse(Convert.ToString(rs.Fields.Item(7).Value));
//dueDate = dueDate.AddDays(DaysBeforeDispatch); // production due date = sales order dispatch date - days before dsipatch(this is defined in KPA settings table)
prodord.CustomerCode = "$[$4.0.0]";
prodord.PostingDate = DateTime.Parse(Convert.ToString(rs.Fields.Item(8).Value));
prodord.DueDate = DateTime.Parse(Convert.ToString(rs.Fields.Item(7).Value));
prodord.ProductionOrderOriginEntry = _iDocEntry;
prodord.ItemNo = Convert.ToString(rs.Fields.Item(2).Value);
prodord.PlannedQuantity = Convert.ToDouble(rs.Fields.Item(3).Value);
prodord.Warehouse = Convert.ToString(rs.Fields.Item(4).Value);
_iLineNum = Convert.ToInt32(rs.Fields.Item(1).Value)+1;
prodord.Remarks = xRemark;
if(prodord.Add() != 0)
{ ui.MessageBox("Error adding Production Order: "+di.GetLastErrorDescription(),1,"OK","","");}
else
{
string DocEntry;
di.GetNewObjectCode(out DocEntry);
prodord.GetByKey(Convert.ToInt32(DocEntry)) // if i remove this its fine, but i need to get the recently added production order to do further validation
string PODocNum = Convert.ToString(prodord.DocumentNumber);
ui.MessageBox("Production order created: " +PODocNum);
}
}
rs.MoveNext();
}
Thanks,
Sujit