Code Review -Please Help

Hi All,

Anyone able to review my code? I’m getting a few errors but ultimately don’t make sense.

foreach(var r in ttResults.Where(r => r.Updated()))
  {
        
      try
      {
       
        bool brequiresUserInput = false;
        string spcMsg = "";
        int piMsgType = 0;
        string pcPCBinAction = "";
        string pcPCBinMessage = "";
        decimal pdSerialNoQty = 0;
        bool bplNegQtyAction = true;
        string pcMessage = "";
        string pcPartTranPKs = "";
        string pcProcessID = "RcptToInvEntry";
        decimal dLaborQty = 0;
        string errorMsg = "";
        string strWarehouseCode = (r.RcvDtl_WareHouseCode);
        string strBinNum = (r.RcvDtl_BinNum);
        string strJobNum = (r.JobOper_JobNum);
        int iAsmbSeq = 0;
        string tranReference = "UBAQ Auto Receipt for PUR-SUB";
        int lotNum = 0;
        string strLaborQty = "0";
        int poNum = (r.RcvHead_PONum);
        
        
        using(var jobRcpttoInv = Ice.Assemblies.ServiceRenderer.GetService<ReceiptsFromMfgSvcContract>(Db))
        {
          if (!Decimal.TryParse(strLaborQty, out dLaborQty));
          
          ReceiptsFromMfgTableset RcptfrMfgDS = new ReceiptsFromMfgTableset();
          jobRcpttoInv.GetNewReceiptsFromMfgJobAsm(strJobNum,iAsmbSeq,"MFG-STK","RcptToInvEntry",ref RcptfrMfgDS);
          RcptfrMfgDS.Tables["PartTran"].Rows[0]["WareHouseCode"] = strWarehouseCode;
          jobRcpttoInv.OnChangeWareHouseCode(pcProcessID, ref RcptfrMfgDS);
          RcptfrMfgDS.Tables["PartTran"].Rows[0]["ActTranQty"] = dLaborQty;
          jobRcpttoInv.OnChangeActTranQty(ref RcptfrMfgDS,out spcMsg); 
          RcptfrMfgDS.Tables["PartTran"].Rows[0]["BinNum"] = strBinNum;
          RcptfrMfgDS.Tables["PartTran"].Rows[0]["RowMod"] = "U";
          RcptfrMfgDS.Tables["PartTran"].Rows[0]["ActTranQty"] = dLaborQty;
          jobRcpttoInv.PreUpdate(ref RcptfrMfgDS, out brequiresUserInput);
          RcptfrMfgDS.Tables["PartTran"].Rows[0]["TranQty"] = dLaborQty;
          RcptfrMfgDS.Tables["PartTran"].Rows[0]["ThisTranQty"] = dLaborQty;

          if (!String.IsNullOrEmpty(tranReference)) {
            RcptfrMfgDS.Tables["PartTran"].Rows[0]["TranReference"] = tranReference;
          }
          /*
          if (!String.IsNullOrEmpty(lotNum)) {
            //RcptfrMfgDS.Tables["PartTran"].Rows[0]["LotNum"] = lotNum;
            jobRcpttoInv.OnChangeLotNum(ref RcptfrMfgDS, true, lotNum, out spcMsg, out errorMsg); 
          }*/

          jobRcpttoInv.VerifySerialMatchAndPlanContract(ref RcptfrMfgDS,out spcMsg,out piMsgType,out pcPCBinAction,out pcPCBinMessage);
          jobRcpttoInv.ReceiveMfgPartToInventory(ref RcptfrMfgDS,pdSerialNoQty,bplNegQtyAction,out pcMessage,out pcPartTranPKs,pcProcessID);
          Db.Validate();
          }
 

        using (var txScope = IceContext.CreateDefaultTransactionScope())
        {
          UD01 newRow = new UD01();
          Db.UD01.Insert(newRow);
          newRow.Company = Session.CompanyID;
          newRow.Key1 = strJobNum;
          newRow.Key2 = "";
          newRow.Key3 = "";
          newRow.Key4 = "";
          newRow.Character01 = "AutoReceipt of PO";
          newRow.CheckBox01 = true; // or false
          Db.Validate();
          txScope.Complete();
        }
            
     }
      catch (Exception ex)
      {
          Ice.Diagnostics.Log.WriteEntry($"", "Auto RcptToInv"); //Write to server log file!
          this.PublishInfoMessage(ex.Message, Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual, "TEAGLE/TEAGLE_JobReceiptToInvFromPO", "Update");
      }
     
 }

Which refers to

          ReceiptsFromMfgTableset RcptfrMfgDS = new ReceiptsFromMfgTableset();
          jobRcpttoInv.GetNewReceiptsFromMfgJobAsm(strJobNum,iAsmbSeq,"MFG-STK","RcptToInvEntry",ref RcptfrMfgDS);
          RcptfrMfgDS.Tables["PartTran"].Rows[0]["WareHouseCode"] = strWarehouseCode;
          jobRcpttoInv.OnChangeWareHouseCode(pcProcessID, ref RcptfrMfgDS);
          RcptfrMfgDS.Tables["PartTran"].Rows[0]["ActTranQty"] = dLaborQty;
          jobRcpttoInv.OnChangeActTranQty(ref RcptfrMfgDS,out spcMsg); 
          RcptfrMfgDS.Tables["PartTran"].Rows[0]["BinNum"] = strBinNum;
          RcptfrMfgDS.Tables["PartTran"].Rows[0]["RowMod"] = "U";
          RcptfrMfgDS.Tables["PartTran"].Rows[0]["ActTranQty"] = dLaborQty;
          jobRcpttoInv.PreUpdate(ref RcptfrMfgDS, out brequiresUserInput);
          RcptfrMfgDS.Tables["PartTran"].Rows[0]["TranQty"] = dLaborQty;
          RcptfrMfgDS.Tables["PartTran"].Rows[0]["ThisTranQty"] = dLaborQty;

section… I’m trying to create a ReceiptFromMfg from a BAQ data and update a UD Table with the data processed so I can use GetList to get the latest receipts and not reprocess the same records by using Checkbox01 as true and hide those rows.

Many thanks
Aaron.

When you double click on the error, where specifically in the code does it take you to? It should take you right to the problem line

Could be this line:

int poNum = (r.RcvHead_PONum);

I think PO is a nvarchar, not an int. You will need to convert it, or use a string.
Try something like this:

int poNum = Convert.ToInt32(r.RcvHead_PONum);

Nate,
RcvHead.PONum is of type int.

Gah! You’re right! I was looking at the OrderHead.PONum!

I got it all compling okay with following code

foreach(var r in ttResults.Where(r => r.Updated()))
  {
       bool brequiresUserInput = false;
        string spcMsg = "";
        int piMsgType = 0;
        string pcPCBinAction = "";
        string pcPCBinMessage = "";
        decimal pdSerialNoQty = 0;
        bool bplNegQtyAction = true;
        string pcMessage = "";
        string pcPartTranPKs = "";
        string pcProcessID = "RcptToInvEntry";
        decimal dLaborQty = (r.RcvDtl_OurQty);
        string errorMsg = "";
        string strWarehouseCode = (r.RcvDtl_WareHouseCode);
        string strBinNum = (r.RcvDtl_BinNum);
        string strJobNum = (r.JobOper_JobNum);
        int iAsmbSeq = 0;
        string tranReference = "UBAQ Auto Receipt for PUR-SUB";
        int lotNum = 0;
        string strLaborQty = "0";
        int poNum = (r.RcvHead_PONum); 
      try
      {
        using(var jobRcpttoInv = Ice.Assemblies.ServiceRenderer.GetService<ReceiptsFromMfgSvcContract>(Db))
        {
          if (!Decimal.TryParse(strLaborQty, out dLaborQty));
          ReceiptsFromMfgTableset RcptfrMfgDS = new ReceiptsFromMfgTableset();
          jobRcpttoInv.GetNewReceiptsFromMfgJobAsm(strJobNum,iAsmbSeq,"MFG-STK","RcptToInvEntry",ref RcptfrMfgDS);
          //RcptfrMfgDS.Tables["PartTran"].Rows[0]["Company"] = Session.CompanyID;
          RcptfrMfgDS.PartTran[0].Company = Session.CompanyID;
          RcptfrMfgDS.PartTran[0].WareHouseCode = strWarehouseCode;
          jobRcpttoInv.OnChangeWareHouseCode(pcProcessID, ref RcptfrMfgDS);
          RcptfrMfgDS.PartTran[0].ActTranQty = dLaborQty;
          jobRcpttoInv.OnChangeActTranQty(ref RcptfrMfgDS,out spcMsg); 
          RcptfrMfgDS.PartTran[0].BinNum = strBinNum;
          RcptfrMfgDS.PartTran[0].RowMod = "U";
          RcptfrMfgDS.PartTran[0].ActTranQty = dLaborQty;
          jobRcpttoInv.PreUpdate(ref RcptfrMfgDS, out brequiresUserInput);
          RcptfrMfgDS.PartTran[0].TranQty = dLaborQty;
          RcptfrMfgDS.PartTran[0].ThisTranQty = dLaborQty;
          if (!String.IsNullOrEmpty(tranReference)) {
            RcptfrMfgDS.PartTran[0].TranReference = tranReference;
          }
          /*
          if (!String.IsNullOrEmpty(lotNum)) {
            RcptfrMfgDS.PartTran[0].LotNum = lotNum;
            jobRcpttoInv.OnChangeLotNum(ref RcptfrMfgDS, true, lotNum, out spcMsg, out errorMsg); 
          }
*/
          jobRcpttoInv.VerifySerialMatchAndPlanContract(ref RcptfrMfgDS,out spcMsg,out piMsgType,out pcPCBinAction,out pcPCBinMessage);
          jobRcpttoInv.ReceiveMfgPartToInventory(ref RcptfrMfgDS,pdSerialNoQty,bplNegQtyAction,out pcMessage,out pcPartTranPKs,pcProcessID);
          Db.Validate();
          }
        using (var txScope = IceContext.CreateDefaultTransactionScope())
        {
          UD14 newRow = new UD14();
          Db.UD14.Insert(newRow);
          newRow.Company = Session.CompanyID;
          newRow.Key1 = strJobNum;
          newRow.Key2 = Convert.ToString(poNum);
          newRow.Key3 = "";
          newRow.Key4 = "";
          newRow.Character01 = "AutoReceipt of PO: {poNum}";
          newRow.CheckBox01 = true; // or false
          Db.Validate();
          txScope.Complete();
        }
     }
      catch (Exception ex)
      {
         this.PublishInfoMessage(ex.Message, Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual, "TEAGLE/TEAGLE_JobReceiptToInvFromPO", "Update");
      }
 }

But now I’m getting error

System.Data.Entity.Core.OptimisticConcurrencyException: Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=472540 for information on understanding and handling optimistic concurrency exceptions.
   at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.ValidateRowsAffected(Int64 rowsAffected, UpdateCommand source)
   at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()
   at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
   at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions options, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction)
   at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions options, Boolean executeInExistingTransaction)
   at Ice.IceDataContext.SaveChanges(SaveOptions options) in C:\_Releases\ICE\ICE3.2.400.38\Source\Framework\Epicor.System\Data\IceDataContext.cs:line 364
   at Ice.IceDataContext.Validate() in C:\_Releases\ICE\ICE3.2.400.38\Source\Framework\Epicor.System\Data\IceDataContext.cs:line 292
   at Ice.ContextBoundBase`1.Complete(Boolean isRoot) in C:\_Releases\ICE\ICE3.2.400.38\Source\Framework\Epicor.Ice\Services\ContextBoundBase.cs:line 221
   at Epicor.Hosting.OperationBoundInvoker.InnerInvoke(Object instance, Func`2 func) in C:\_Releases\ICE\ICE3.2.400.38\Source\Framework\Epicor.System\Hosting\OperationBoundInvoker.cs:line 62
   at Epicor.Hosting.OperationBoundInvoker.Invoke(Object instance, Func`2 func

Never seen this error before

If I remove

RcptfrMfgDS.PartTran[0].RowMod = "U";

From the code it will run through fine but I don’t get a MFG-STK - PartTran record any ideas?

foreach(var r in ttResults.Where(r => r.Updated()))
  {
       bool brequiresUserInput = false;
        string spcMsg = "";
        int piMsgType = 0;
        string pcPCBinAction = "";
        string pcPCBinMessage = "";
        decimal pdSerialNoQty = 0;
        bool bplNegQtyAction = true;
        string pcMessage = "";
        string pcPartTranPKs = "";
        string pcProcessID = "RcptToInvEntry";
        decimal dLaborQty = (r.RcvDtl_OurQty);
        string errorMsg = "";
        string strWarehouseCode = (r.RcvDtl_WareHouseCode);
        string strBinNum = (r.RcvDtl_BinNum);
        string strJobNum = (r.JobOper_JobNum);
        int iAsmbSeq = 0;
        string tranReference = "UBAQ Auto Receipt for PUR-SUB";
        string lotNum = "";
        string strLaborQty = "0";
        int poNum = (r.RcvHead_PONum);
        int key1Num = new Random().Next(1000, 9999);
      
      

     try
      {
      
        using(var jobRcpttoInv = Ice.Assemblies.ServiceRenderer.GetService<ReceiptsFromMfgSvcContract>(Db))
        {
        
          if (!Decimal.TryParse(strLaborQty, out dLaborQty));
          
          ReceiptsFromMfgTableset RcptfrMfgDS = new ReceiptsFromMfgTableset();
          jobRcpttoInv.GetNewReceiptsFromMfgJobAsm(strJobNum,iAsmbSeq,"MFG-STK","RcptToInvEntry",ref RcptfrMfgDS);
          
          //RcptfrMfgDS.Tables["PartTran"].Rows[0]["Company"] = Session.CompanyID;
          RcptfrMfgDS.PartTran[0].Company = Session.CompanyID;
          RcptfrMfgDS.PartTran[0].WareHouseCode = strWarehouseCode;         
          jobRcpttoInv.OnChangeWareHouseCode(pcProcessID, ref RcptfrMfgDS);
          RcptfrMfgDS.PartTran[0].ActTranQty = dLaborQty;
          RcptfrMfgDS.PartTran[0].RowMod = "U";
          jobRcpttoInv.OnChangeActTranQty(ref RcptfrMfgDS,out spcMsg);           
          RcptfrMfgDS.PartTran[0].BinNum = strBinNum;
          RcptfrMfgDS.PartTran[0].ActTranQty = dLaborQty;          
          jobRcpttoInv.PreUpdate(ref RcptfrMfgDS, out brequiresUserInput);         
          RcptfrMfgDS.PartTran[0].TranQty = dLaborQty;
          RcptfrMfgDS.PartTran[0].ThisTranQty = dLaborQty;
          RcptfrMfgDS.PartTran[0].TranReference = tranReference;
          RcptfrMfgDS.PartTran[0].LotNum = lotNum;
          //RcptfrMfgDS.PartTran[0].RowMod = "U";
          jobRcpttoInv.VerifySerialMatchAndPlanContract(ref RcptfrMfgDS,out spcMsg,out piMsgType,out pcPCBinAction,out pcPCBinMessage);
          jobRcpttoInv.ReceiveMfgPartToInventory(ref RcptfrMfgDS,pdSerialNoQty,bplNegQtyAction,out pcMessage,out pcPartTranPKs,pcProcessID);
          } 
      }
      catch (Exception ex)
      {
         Ice.Diagnostics.Log.WriteEntry(ex.Message);
      }

        using (var txScope = IceContext.CreateDefaultTransactionScope())
        {
          UD14 newRow = new UD14();
          Db.UD14.Insert(newRow);
          newRow.Company = Session.CompanyID;
          newRow.Key1 = Convert.ToString(key1Num);
          newRow.Key2 = Convert.ToString(poNum);
          newRow.Key3 = strJobNum;
          newRow.Key4 = "";
          newRow.Character01 = "AutoReceipt of PO: {poNum}";
          newRow.CheckBox01 = true; // or false
          Db.Validate();
          txScope.Complete();
        }
        
 }

Resolve it, it didn’t like the ttResults

1 Like

Hi @aarong may I know what is the working code from your final solutions? mind to share it?

Hi Vincent,

I no longer for this business. What are you trying to achieve? Maybe I can help you.