Packslip Creation with multiple line - BPM

Hi,
I am creating a packslip with multiple lines using BPM. with this following code I am able to create first line. at the time of 2nd iteration foreach I am getting ‘Object reference error- in
GetNewOrdrShipDtl method.’. I really appreciate your help with this.

//This BPM is used to create the Packslip with the UD101 data.
var ttrow = (from row in ttUD101 where row.Company == Session.CompanyID && (row.RowMod == "A" || row.RowMod == "U")&& row.Character04 =="True" /* "Create PackSlip"*/ select row).FirstOrDefault();
if(ttrow != null)
{
     try
     {
          int orderNum = Convert.ToInt32(ttrow.Key2);//5631;
          int custNum = 0;
          var dbOrderHed = (from row in Db.OrderHed where row.Company == Session.CompanyID && row.OrderNum == orderNum select row).FirstOrDefault();
          if(dbOrderHed != null)
          {
            custNum= Convert.ToInt32(dbOrderHed.CustNum);
          }
          string creditMessage = "";
          string shipToCustomerList = "";
          int iShipToCustNum = custNum;//13331;
          string shipToList = "";
          int ipPackNum = 0;
          bool opPromptForNum = false;
          int lineNum = 0;
          bool belongToAnotherPC = false;
          string pcOutsideMessage= "";
          bool cError,compError,updateComplete,checkComplianceError,changeStatusError,checkShipDtlAgain = false;
          string opReleaseMessage,opCompleteMessage,opShippingMessage,opLotMessage,opInventoryMessage,opLockQtyMessage,opAllocationMessage,opPartListNeedsAttr,opLotListNeedsAttr,shipCreditMsg,msg,opPostUpdMessage = "";
          string partNum = "";
          string vMsgText,vMsgType,origPartNum = "";
          bool vSubAvail = false;
          int ipBTCustNum = custNum;//13331;
          
         // using (var Txscope = IceContext.CreateDefaultTransactionScope())
          {
              var svc = Ice.Assemblies.ServiceRenderer.GetService<Erp.Contracts.CustShipSvcContract>(Db);
              Erp.Tablesets.CustShipTableset CT;
              CT = new Erp.Tablesets.CustShipTableset();
              svc.GetNewShipHead(ref CT);
              svc.GetHeadOrderInfo(orderNum,out creditMessage,ref CT);
              svc.BuildShipToCustomerList(orderNum,out shipToCustomerList);
              svc.BuildShipToList(orderNum,iShipToCustNum,out shipToList);
              svc.GetLegalNumGenOpts(ipPackNum,ref CT, out opPromptForNum);
              svc.CheckPCBinOutLocation(ref CT,out lineNum,out belongToAnotherPC, out pcOutsideMessage);
              
              CT.ShipHead[0].ShipDate = Convert.ToDateTime(DateTime.Now);
              //CT.ShipHead[0].ShipViaCode = "UPS";
              CT.ShipHead[0].CustNum = custNum;//13331;
              CT.ShipHead[0].Plant = Session.PlantID.ToString();
              CT.ShipHead[0].RowMod = "A";
              
              svc.UpdateMaster(ref CT,false,false,false,false,false,false,false,ipPackNum,iShipToCustNum,out opReleaseMessage,out opCompleteMessage,out opShippingMessage,out opLotMessage,out opInventoryMessage,out opLockQtyMessage,out opAllocationMessage,out opPartListNeedsAttr,out opLotListNeedsAttr,out shipCreditMsg,out cError,out compError,out msg,out opPostUpdMessage,out updateComplete,out checkComplianceError,out changeStatusError,out checkShipDtlAgain);
             
              int Packnum = CT.ShipHead[0].PackNum;
               InfoMessage.Publish("Header Created"+Packnum.ToString());
              
              string company = CT.ShipHead[0].Company; 
              int rowIndex = 0;
              
              foreach(var DbPackslipRow in(from row in Db.UD101 where row.Company == Session.CompanyID && row.Key2 == ttrow.Key2 select row))
              {
                  
                  int ipOrderNum = 0;
                  string pcWarningMsg,cWarning,ipPCID= "";
                  // string ipPCID = "";
                  svc.POGetNew(ipOrderNum,Packnum,out pcWarningMsg);
                  string ipPackMode = "PACK";
                  svc.POGetDtlList(Packnum,ipPCID,orderNum,ipPackMode,out cWarning);
                  InfoMessage.Publish(orderNum.ToString()+"   order num");
                  svc.GetNewOrdrShipDtl(ref CT,Packnum,orderNum);
                  InfoMessage.Publish("shipdtl --1");
                  svc.GetOrderInfo(orderNum,out creditMessage,ref CT);
                  //svc.GetManifestInfo(orderNum,Packnum,ref CT);
                  svc.BuildShipToList(orderNum,iShipToCustNum,out shipToList);
                  //svc.GetListFilterShipped();
                  int orderLine = Convert.ToInt32(DbPackslipRow.Key3);
                  
                  svc.CheckPrePartInfo(ref partNum,orderNum,orderLine,out vMsgText,out vSubAvail,out vMsgType,out origPartNum);
                  int packLine = 0;
                  int orderRelNum = Convert.ToInt32(DbPackslipRow.Key4);
                  string subsPart = "";//"8700-1251-900";
                  var dbOrderRel = (from row in Db.OrderRel where row.Company == Session.CompanyID && row.OrderNum == orderNum && row.OrderLine == orderLine && row.OrderRelNum == orderRelNum select row).FirstOrDefault();
                  if(dbOrderRel != null)
                  {
                    subsPart = Convert.ToString(dbOrderRel.PartNum);
                  }
                  
                  svc.GetOrderLineInfo(ref CT,packLine,orderLine,subsPart);
                  bool allowNewShipTo = true;
                  svc.GetOrderRelInfo(ref CT,packLine,orderRelNum,allowNewShipTo);
                  InfoMessage.Publish("Display Inv QTY--1");
                  CT.ShipDtl[rowIndex].DisplayInvQty = Convert.ToInt32(DbPackslipRow.Number01);
                   InfoMessage.Publish("Disply --2");
                 //SERIAL TRACKED PART CODE START
                  var dbPart = (from row in Db.Part where row.Company == Session.CompanyID && row.PartNum == subsPart select row).FirstOrDefault();
                  if(dbPart != null)
                  {
                    if(dbPart.TrackSerialNum == true)
                    {   
                      string ipPartNum = dbPart.PartNum.ToString();
                      decimal ipQuantity = Convert.ToInt32(DbPackslipRow.Number01);//CT.ShipDtl[0].OurInventoryShipQty;
                      string ipUOM = CT.ShipDtl[rowIndex].InventoryShipUOM.ToString();
                      //int ipPackNum = CT.ShipDtl[0].PackNum;
                      int ipPackLine = CT.ShipDtl[rowIndex].PackLine;
                      string ipTranType = "STK-PCK";
                      string ipJobNum = CT.ShipDtl[rowIndex].JobNum.ToString();//"";
                      string ipWhseCode = CT.ShipDtl[rowIndex].WarehouseCode.ToString();
                      string ipBinNum = CT.ShipDtl[rowIndex].BinNum.ToString();
                      string ipLotNum = CT.ShipDtl[rowIndex].LotNum.ToString();
                      InfoMessage.Publish(ipWhseCode.ToString()+"wh "+ipBinNum.ToString()+"Bin");
                      bool ipFromPO = false;
                      int ipOrderLine = CT.ShipDtl[rowIndex].OrderLine;
                      int ipOrderRelNum = CT.ShipDtl[rowIndex].OrderRelNum;
                      Guid ipSysRowID = new Guid("00000000-0000-0000-0000-000000000000");
                      
                      svc.GetSelectSerialNumbersParams(ipPartNum,ipQuantity,ipUOM,ipPackNum,ipPackLine,ipTranType,ipJobNum,ipWhseCode,ipBinNum,ipLotNum,ipFromPO,ipOrderNum,ipOrderLine,ipOrderRelNum,ipSysRowID);
                      
                      var SSNBO = Ice.Assemblies.ServiceRenderer.GetService<Erp.Contracts.SelectedSerialNumbersSvcContract>(Db);
                      Erp.Tablesets.SelectedSerialNumbersTableset ssnTs = new Erp.Tablesets.SelectedSerialNumbersTableset();
                      
                      string xrefPartNum = string.Empty;
                      string xrefPartType = string.Empty;
                      int xrefCustNum = 0;
                      SSNBO.GetSerialNumFormat(ref ssnTs, ipPartNum, xrefPartNum, xrefPartType, xrefCustNum, Session.PlantID.ToString());
                      
                      // SELECTING SERIAL NUMBERS START
                       var dbSerialNums = (from row in Db.UD101A where row.Company == Session.CompanyID && row.Key1 == DbPackslipRow.Key1 && row.Key2 == DbPackslipRow.Key2 && row.Key3 == DbPackslipRow.Key3 && row.Key4 == DbPackslipRow.Key4 && row.Key5 == DbPackslipRow.Key5 select row).ToList();
                      if(dbSerialNums.Count > 0)
                      {
                        foreach(var SerialNum in dbSerialNums)
                        {
                           var SerialNoBO = Ice.Assemblies.ServiceRenderer.GetService<Erp.Contracts.SerialNoSvcContract>(Db);
                            Erp.Tablesets.SerialNoTableset snTs = new Erp.Tablesets.SerialNoTableset();
                            string partNum1 = "";
                            SerialNoBO.GetNewSerialNo(ref snTs, partNum1); // Create new serial number
                            string SerialNumber = SerialNum.ShortChar01.ToString();
                            snTs.SerialNo[0].SerialNumber = SerialNum.ShortChar01.ToString(); // Inserting serial number
                            snTs.SerialNo[0].SNReference = "Test Ref"; // Reference
                            
                            string pcProposedPartNum = ipPartNum;//dbPart.PartNum.ToString();
                            string custID = "";
                            SerialNoBO.ChangePartNum(pcProposedPartNum, ref snTs); // Assign part to the serial number
                            var dbcustrow = (from row in Db.Customer where row.Company == Session.CompanyID && row.CustNum == custNum select row).FirstOrDefault();
                            if(dbcustrow != null)
                            {
                              custID = Convert.ToString(dbcustrow.CustID);
                            }
                            string proposedCustID = custID;//dbCustomer.CustID.ToString();
                            SerialNoBO.ChangeCustomerID(proposedCustID, ref snTs); // Assign customer to serial number
                            SerialNoBO.Update(ref snTs);
                            bool snExists = false;
                            SerialNoBO.SerialNumberExists(SerialNumber, out snExists); // Check if serial number exists
                            SerialNoBO.Update(ref snTs); // Save the serial number
                          //  SerialNoBO.Dispose();
                            InfoMessage.Publish("Serial Number saved");
                            
                            // Changing values after update----
                            var dbSerialNoAfter = (from row in Db.SerialNo where row.Company == Session.CompanyID && row.SerialNumber == SerialNumber select row).FirstOrDefault();
                            if(dbSerialNoAfter != null)
                            {
                              dbSerialNoAfter.SNStatus = "INVENTORY"; // Serial Number Status
                              dbSerialNoAfter.WareHouseCode = ipWhseCode;// "MAIN"; // Warehouse Code
                              dbSerialNoAfter.BinNum = ipBinNum;//"10A1"; // BinNum
                              Db.Validate();
                              InfoMessage.Publish("Serial Number status changed");
                            }
                           
                          SSNBO.AddSerialNum(ref ssnTs, ipPartNum, xrefPartNum, xrefPartType, xrefCustNum, SerialNum.ShortChar01.ToString(), Guid.Empty.ToString(), "STK-PCK", Session.PlantID.ToString());
                        }
                      }
                      // SELECTING SERIAL NUMBERS END
                      
                      // Adding the selected serials to custship tableset START
                      
                      for(int i = 0; i < ssnTs.SelectedSerialNumbers.Count; i++)
                      {
                        var ssnrow = CT.SelectedSerialNumbers.NewRow();
                        ssnrow["Company"] = ssnTs.SelectedSerialNumbers[i].Company;
                        ssnrow["SerialNumber"] = ssnTs.SelectedSerialNumbers[i].SerialNumber;
                        ssnrow["Scrapped"] = ssnTs.SelectedSerialNumbers[i].Scrapped;
                        ssnrow["ScrappedReasonCode"] = ssnTs.SelectedSerialNumbers[i].ScrappedReasonCode;
                        ssnrow["Voided"] = ssnTs.SelectedSerialNumbers[i].Voided;
                        ssnrow["Reference"] = ssnTs.SelectedSerialNumbers[i].Reference;
                        ssnrow["ReasonCodeType"] = ssnTs.SelectedSerialNumbers[i].ReasonCodeType;
                        ssnrow["ReasonCodeDesc"] = ssnTs.SelectedSerialNumbers[i].ReasonCodeDesc;
                        ssnrow["PartNum"] = ssnTs.SelectedSerialNumbers[i].PartNum;
                        ssnrow["SNPrefix"] = ssnTs.SelectedSerialNumbers[i].SNPrefix;
                        ssnrow["SNBaseNumber"] = ssnTs.SelectedSerialNumbers[i].SNBaseNumber;
                        ssnrow["SourceRowID"] = Guid.Empty; //tsSelSerials.SelectedSerialNumbers[i].SourceRowID;
                        ssnrow["TransType"] = ssnTs.SelectedSerialNumbers[i].TransType;
                        ssnrow["PassedInspection"] = ssnTs.SelectedSerialNumbers[i].PassedInspection;
                        ssnrow["Deselected"] = ssnTs.SelectedSerialNumbers[i].Deselected;
                        ssnrow["KitWhseList"] = ssnTs.SelectedSerialNumbers[i].KitWhseList;
                        ssnrow["RawSerialNum"] = ssnTs.SelectedSerialNumbers[i].RawSerialNum;
                        ssnrow["KBLbrAction"] = ssnTs.SelectedSerialNumbers[i].KBLbrAction;
                        ssnrow["KBLbrActionDesc"] = ssnTs.SelectedSerialNumbers[i].KBLbrActionDesc;
                        ssnrow["PreventDeselect"] = ssnTs.SelectedSerialNumbers[i].PreventDeselect;
                        ssnrow["XRefPartNum"] = ssnTs.SelectedSerialNumbers[i].XRefPartNum;
                        ssnrow["XRefPartType"] = ssnTs.SelectedSerialNumbers[i].XRefPartType;
                        ssnrow["PreDeselected"] = ssnTs.SelectedSerialNumbers[i].PreDeselected;
                        ssnrow["poLinkValues"] = ssnTs.SelectedSerialNumbers[i].poLinkValues;
                        ssnrow["SNMask"] = ssnTs.SelectedSerialNumbers[i].SNMask;
                        ssnrow["NotSavedToDB"] = ssnTs.SelectedSerialNumbers[i].NotSavedToDB;
                        //ssnrow["RowSelected"] = ssnTs.SelectedSerialNumbers[i].RowSelected;
                        ssnrow["SysRowID"] = Guid.Empty; //tsSelSerials.SelectedSerialNumbers[i].SysRowID;
                        ssnrow["RowMod"] = "A";
                        CT.SelectedSerialNumbers.Add(ssnrow);
                      }
                      InfoMessage.Publish("sn created");
                      // Adding selected serials to custship tableset END
                     
                    //  SSNBO.Dispose();
                     // Txscope.Complete();
                     // }
                    }
                  }
                  //------ SERIAL TRACKED PART CODE END
                  var dbPart_Lot = (from row in Db.Part where row.Company == Session.CompanyID && row.PartNum == subsPart select row).FirstOrDefault();
                  if(dbPart_Lot != null)
                  {
                    // TRACKLOT PART CODE START
                    if(dbPart_Lot.TrackLots == true)
                    {
                     var LSUBO = Ice.Assemblies.ServiceRenderer.GetService<Erp.Contracts.LotSelectUpdateSvcContract>(Db);
                      Erp.Tablesets.PartLotListTableset partlotlistTs = new Erp.Tablesets.PartLotListTableset();
                     string ipWhseCode = CT.ShipDtl[rowIndex].WarehouseCode.ToString();
                      string ipBinNum = CT.ShipDtl[rowIndex].BinNum.ToString();
                     string whereClause = "(PartNum = '"+subsPart+"') AND Onhand = true";//(PartNum = '00M3') AND OnHand = true
                     int pageSize = 100;
                      int absolutePage = 0;
                      bool morePages = false;
                      partlotlistTs = LSUBO.GetListByBinNum(ipWhseCode,ipBinNum,whereClause,pageSize,absolutePage, out morePages);
                      CT.ShipDtl[rowIndex].LotNum = partlotlistTs.PartLotList[0].LotNum.ToString();
                    //  LSUBO.Dispose();
                    }
                     //--------- TRACKLOT PART CODE END
                  }
                  
                  svc.CheckPCBinOutLocation(ref CT,out lineNum,out belongToAnotherPC,out pcOutsideMessage);
                  bool doValidateCreditHold,doCheckShipDtl = false;
                  CT.ShipHead[0].RowMod = "U";
                  CT.ShipDtl[rowIndex].PackNum = Packnum;
                  CT.ShipDtl[rowIndex].PackLine = packLine;
                  CT.ShipDtl[rowIndex].OrderLine = orderLine;
                  CT.ShipDtl[rowIndex].OrderRelNum = orderRelNum;
                  CT.ShipDtl[rowIndex].LineType = "PART";
                  CT.ShipDtl[rowIndex].OurInventoryShipQty = Convert.ToDecimal(DbPackslipRow.Number01);
                  CT.ShipDtl[rowIndex].Packages = 1;
                  CT.ShipDtl[rowIndex].PartNum = subsPart;
                 // CT.ShipDtl[0].WarehouseCode = "CHI";
                 // CT.ShipDtl[0].BinNum = "01-01-02";
                  CT.ShipDtl[rowIndex].RowMod = "A";
                  InfoMessage.Publish("before update");
                  svc.UpdateMaster(ref CT,false,false,false,false,false,false,false,Packnum,ipBTCustNum,out opReleaseMessage,out opCompleteMessage,out opShippingMessage,out opLotMessage,out opInventoryMessage,out opLockQtyMessage,out opAllocationMessage,out opPartListNeedsAttr,out opLotListNeedsAttr,out shipCreditMsg,out cError,out compError,out msg,out opPostUpdMessage,out updateComplete,out checkComplianceError,out changeStatusError,out checkShipDtlAgain);
                  InfoMessage.Publish("Packslip Created. Packslip No:"+Packnum.ToString());
                  rowIndex = rowIndex++;
                  }
                  ttrow.ShortChar01 = "PackSlip Created";
                  ttrow.Character01 = Packnum.ToString(); // storing in UD101 PackNum
                 
              InfoMessage.Publish("end");
             // Db.Validate();
             // svc.Dispose();
            //  Txscope.Complete();
          }
    }
    catch(Exception e)
    {
      InfoMessage.Publish(e.Message.ToString());
      ttrow.Character02 = e.Message.ToString();
    }
}   

Hi @Nika , did you get this resolved ?