BPM problem when trying to create a new UD101A record

I am trying to add a new UD101A record using a BPM, when executing the update statement I receive an error message in the server event viewer saying:

Ice.Common.EpicorServerException: BPM runtime caught an unexpected exception of ‘EntityException’ type.
See more info in the Inner Exception section of Exception Details. —> System.Data.Entity.Core.EntityException: The underlying provider failed on EnlistTransaction. —> System.Data.SqlClient.SqlException: The operation failed because the session is not single threaded.

Here is the code:

var svcUD101 = Ice.Assemblies.ServiceRenderer.GetService<Ice.Contracts.UD101SvcContract>(Db,true);
bool v_Bool = false;

var v_UD101 = (from ttUD101 in ttUD101
select ttUD101).FirstOrDefault();
if (v_UD101 != null)
{
Int32 v_FromPallet = Convert.ToInt32 (v_UD101.ShortChar04);
Int32 v_ToPallet = Convert.ToInt32 (v_UD101.ShortChar05);
string v_Key1 = v_UD101.Key1;
string v_Key2 = v_UD101.Key2;
string v_Key3 = v_UD101.Key3;
string v_Key4 = string.Empty;
string v_Key5 = string.Empty;
var tsUD101 = svcUD101.GetByID (v_Key1, v_Key2, v_Key3, v_Key4, v_Key5);
// svcUD101.GetList (“UD101A”, 0, 0, out v_Bool);

foreach (var v_PartLot in (from PartLot in Db.PartLot
              where PartLot.Company == Session.CompanyID
              && PartLot.PartNum == v_UD101.Key2
              select PartLot))
{
    if (Convert.ToInt32 (v_PartLot.LotNum) >= v_FromPallet && Convert.ToInt32 (v_PartLot.LotNum) <= v_ToPallet)
    {            
        svcUD101.GetaNewUD101A (ref tsUD101, v_Key1, v_Key2, v_Key3, v_Key4, v_Key5);
        
        foreach (var v_NewUD101A in (from NewUD101A in tsUD101.UD101A
                                     where NewUD101A.RowMod == "A"
                                     select NewUD101A))
        {
        PublishInfoMessage("Key1 " + v_Key1, Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual, "Company", "Update");
            v_NewUD101A.Key1 = v_Key1;
            v_NewUD101A.Key2 = v_Key2;
            v_NewUD101A.Key3 = v_Key3;
            v_NewUD101A.Key4 = v_Key4;
            v_NewUD101A.Key5 = v_Key5;
            v_NewUD101A.ChildKey1 = v_PartLot.LotNum;
            v_NewUD101A.CheckBox01 = false;
            v_NewUD101A.CheckBox02 = false;
        }
        
        svcUD101.Update (ref tsUD101);
    }
}

}

Any sharp eyes to see what I am doing wrong? It is working when I comment out the update statement, but nothing is saved of course.

Hi Mikael,

here is one example you can use to create UD110A table records:

using (var txScope = IceContext.CreateDefaultTransactionScope())
{
foreach(var PartLotResult in (from row in Db.PartLot
where row.Company == Session.CompanyID &&
row.PartNum == callContextBpmData.ShortChar01 &&
row.Batch ==callContextBpmData.ShortChar02
select new { row.LotNum }))
{
if (PartLotResult.LotNum!=“”)
{
foreach(var PartBinResult in (from row in Db.PartBin
where row.Company == Session.CompanyID &&
row.PartNum == callContextBpmData.ShortChar01 &&
row.LotNum == PartLotResult.LotNum &&
row.WarehouseCode == callContextBpmData.ShortChar03 &&
row.BinNum == callContextBpmData.ShortChar04
select new { row.OnhandQty }))
{
if (PartBinResult.OnhandQty > 0)
{ //svcUD101.GetaNewUD101A (callContextBpmData.ShortChar01,callContextBpmData.ShortChar02,callContextBpmData.ShortChar03,callContextBpmData.ShortChar04,callContextBpmData.ShortChar05);

                               UD110A newRow = new UD110A();
                               Db.UD110A.Insert(newRow);
                               newRow.Company = Session.CompanyID;
                               newRow.Key1 = callContextBpmData.ShortChar01;
                               newRow.Key2 = callContextBpmData.ShortChar02;
                               newRow.Key3 = callContextBpmData.ShortChar03;
                               newRow.Key4 = callContextBpmData.ShortChar04;
                               newRow.Key5 = callContextBpmData.ShortChar05;
                               newRow.ChildKey1 = PartLotResult.LotNum;
                               newRow.Character01=callContextBpmData.Character01;
                               newRow.Character01=callContextBpmData.Character02;
                               newRow.Character01=callContextBpmData.Character03;
                               newRow.Number01 = PartBinResult.OnhandQty;
          }
       }
    }
  }

Db.Validate();
txScope.Complete();

}

I don’t think you need that loop there and also you don’t need to set Key1 to Key5. Something like:

        svcUD101.GetaNewUD101A (ref tsUD101, v_Key1, v_Key2, v_Key3, v_Key4, v_Key5);

        PublishInfoMessage("Key1 " + v_Key1, Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual, "Company", "Update");

        Ice.Tablesets.UD101ARow v_NewUD101A = tsUD101.UD101A[tsUD101.UD101A.Count-1];

        v_NewUD101A.ChildKey1 = v_PartLot.LotNum;
        v_NewUD101A.CheckBox01 = false;
        v_NewUD101A.CheckBox02 = false;
        
        svcUD101.Update (ref tsUD101);