BPM Code to add Parent/Child UD1XX/UD1XXA data

,

As promised, here is the final code. As a sidenote, it is not working 100% (Required field exception), but I don’t believe it is due to the code structure, so I add it here for others to glean things I searched to find.

EDIT - below is the final code I am using and it all works. If you find the code useful in any way, hit Like at the bottom to encourage me and others to share more helpful source code here. :slight_smile:

int counter = 0;
int InspectionBatchNumber = 0;
string SerialLotTKLotNumber = string.Empty;
string StepName = string.Empty;
string UserID = string.Empty;
List<string> SerialNos = new List<string>() { };
try
{
  using (var txScope = IceContext.CreateDefaultTransactionScope())
  {
    StepName = "Update UD11";
    // Get next PutAway Batch Number
    foreach (var UD11 in (from UD11Row in Db.UD11.With(LockHint.UpdLock) where UD11Row.Key1 == "PutAway" select UD11Row))
    {
      InspectionBatchNumber = System.Convert.ToInt32(UD11.Number01);
      InspectionBatchNumber++;
      UD11.Number01 = System.Convert.ToDecimal(InspectionBatchNumber);
      Db.Validate();
    }
    StepName = "Get Serial Numbers";
    SerialNos.Clear();
    foreach (var ttSN in (from row in ttSelectedSerialNumbers select row))
    {
      if (!SerialNos.Contains(ttSN.SerialNumber))
        SerialNos.Add(ttSN.SerialNumber);
    }
    StepName = "Update UD105 Inspection";
    SerialLotTKLotNumber = string.Empty;
    counter= 0;
    using (var UD105 = Ice.Assemblies.ServiceRenderer.GetService<Ice.Contracts.UD105SvcContract>(Db))
    {
      foreach (var ttIR in (from row in ttInspRcpt where row.RowMod == "U" select row))
      {
        if (counter == 0)
        {
          if (SerialNos.Count > 0)
            SerialLotTKLotNumber = SerialNos[0];
          else if (ttIR.LotNum.ToString() != "")
            SerialLotTKLotNumber = ttIR.LotNum.ToString();
          else if (ttIR.UDField<string>("TK_LotNumber_c") != "")
            SerialLotTKLotNumber = ttIR.UDField<string>("TK_LotNumber_c");
          counter = 1;
        }
        var tsUD105 = new UD105Tableset();
        UD105.GetaNewUD105(ref tsUD105);
        tsUD105.UD105[0].Company = Session.CompanyID;
        tsUD105.UD105[0].Key1 = InspectionBatchNumber.ToString();
        tsUD105.UD105[0].Key2 = string.Empty;
        tsUD105.UD105[0].Key3 = string.Empty;
        tsUD105.UD105[0].Key4 = string.Empty;
        tsUD105.UD105[0].Key5 = string.Empty;
        tsUD105.UD105[0]["UserID_c"] = Session.UserID == null ? string.Empty : Session.UserID;
        tsUD105.UD105[0]["PartNumber_c"] = ttIR.PartNum.ToString();
        tsUD105.UD105[0]["PackSlip_c"] = ttIR.PackSlip.ToString();
        tsUD105.UD105[0]["PONum_c"] = ttIR.PONum;
        tsUD105.UD105[0]["POLine_c"] = ttIR.POLine;
        tsUD105.UD105[0]["InspectedBy_c"] = ttIR.InspectorIDName;
        tsUD105.UD105[0]["InspectionDate_c"] = ttIR.InspectedDate;
        tsUD105.UD105[0]["PORelNum_c"] = ttIR.PORelNum;
        tsUD105.UD105[0]["ReceiptDate_c"] = ttIR.ReceiptDate;
        tsUD105.UD105[0]["PackSlipLine_c"] = ttIR.PackLine;
        tsUD105.UD105[0]["Revision_c"] = ttIR.RevisionNum;
        tsUD105.UD105[0]["PartDescription_c"] = ttIR.PartDescription;
        tsUD105.UD105[0]["VendorName_c"] = ttIR.VendorNumName;
        tsUD105.UD105[0]["VendorNumber_c"] = ttIR.VendorNum;
        tsUD105.UD105[0]["ReceivedQty_c"] = System.Convert.ToInt32(ttIR.OurQty);
        tsUD105.UD105[0]["QtyInspected_c"] = ttIR.UDField<int>("QuantityInspected_c");
        tsUD105.UD105[0]["QtyPassed_c"] = System.Convert.ToInt32(ttIR.PassedQty);
        tsUD105.UD105[0]["QtyRejected_c"] = System.Convert.ToInt32(ttIR.FailedQty);
        tsUD105.UD105[0]["SerialLotNumber_c"] = SerialLotTKLotNumber;
        tsUD105.UD105[0]["FileName_c"] = string.Format("{0} SN{1} PL{2} {3}", ttIR.PartNum, SerialLotTKLotNumber, ttIR.PackSlip, ttIR.PONum.ToString());
        UD105.Update(ref tsUD105);
      }
      StepName = "Update UD105A Serial Numbers";
      foreach (string sn in SerialNos)
      {
        var tsUD105A = new UD105Tableset();
        UD105.GetaNewUD105A(ref tsUD105A, InspectionBatchNumber.ToString(), string.Empty, string.Empty, string.Empty, string.Empty);
        var UD105ANew = tsUD105A.UD105A.Where(r=>r.RowMod=="A").FirstOrDefault();
        UD105ANew.Company = Session.CompanyID;
        UD105ANew.ChildKey1 = sn;
        UD105ANew.ChildKey2 = string.Empty;
        UD105ANew.ChildKey3 = string.Empty;
        UD105ANew.ChildKey4 = string.Empty;
        UD105ANew.ChildKey5 = string.Empty;
        UD105.Update(ref tsUD105A);
      }
    }
    txScope.Complete();
  }
}
catch (Exception ex)
{
  string msg = string.Format("Error during {0} step of LoadPutAwayBatch Pre-Processing directive.\r\nError Message: {1}.", StepName, ex.Message);
  this.PublishInfoMessage(msg,Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual,"",""); 
}