EFx - Error getting context variable the second time in the same function

Hey folks,

I’m processing some stuff in a function driven by a button on a modified UD form. I use LINQ queries several times in the function, which requires a context variable to be set from an assembly. Example below.

A function where I’m reporting quantity runs just fine. And the function that receives to stock did, too, until we added a second company. Unless that was just coincidence.

Here’s the code from the first decision block:

// validate coil warehouse bin

bool billetBinGood = true;

var context = (Erp.ErpContext)Ice.Services.ContextFactory.CreateContext();
var erpContext = new Erp.Internal.Lib.CCredChk(context); **// used this library/assembly only for erpContext**

foreach(var ud102A_xRow in (from row in erpContext.Db.UD102A
where row.Company == Session.CompanyID &&
row.Key1 == jobNum &&
row.Key2 == jobSeq && 
row.Completed_c == true &&
row.Received_c == false
orderby row.Key1 ascending
orderby row.Key2 ascending
orderby row.ChildKey1 ascending
select row))

{
  var whseBin_xRow = (from row in erpContext.Db.WhseBin 
  where row.Company == Session.CompanyID &&
  row.WarehouseCode == ud102A_xRow.CoilWarehouseCode_c &&
  row.BinNum == ud102A_xRow.CoilBinNum_c
  select new {row.Company, row.WarehouseCode, row.BinNum}).FirstOrDefault();
  
  if (whseBin_xRow == null)
  {
    billetBinGood = false;
  }
}

return billetBinGood;

That works fine. But the next condition block looks a lot like that running through the same table, looking for different conditions. It gets an error when it hits this statement:

var context = (Erp.ErpContext)Ice.Services.ContextFactory.CreateContext();

Here’s the error:

System.Data.Entity.Core.EntityException: The underlying provider failed on Open.
 ---> System.PlatformNotSupportedException: This platform does not support distributed transactions.
   at System.Transactions.Distributed.DistributedTransactionManager.GetDistributedTransactionFromTransmitterPropagationToken(Byte[] propagationToken)
   at System.Transactions.TransactionInterop.GetDistributedTransactionFromTransmitterPropagationToken(Byte[] propagationToken)
   at System.Transactions.TransactionStatePSPEOperation.PSPEPromote(InternalTransaction tx)
   at System.Transactions.TransactionStateDelegatedBase.EnterState(InternalTransaction tx)
   at System.Transactions.EnlistableStates.Promote(InternalTransaction tx)
   at System.Transactions.Transaction.Promote()
   at System.Transactions.TransactionInterop.ConvertToDistributedTransaction(Transaction transaction)
   at System.Transactions.TransactionInterop.GetExportCookie(Transaction transaction, Byte[] whereabouts)
   at System.Data.SqlClient.SqlInternalConnection.GetTransactionCookie(Transaction transaction, Byte[] whereAbouts)
   at System.Data.SqlClient.SqlInternalConnection.EnlistNonNull(Transaction tx)
   at System.Data.ProviderBase.DbConnectionPool.PrepareConnection(DbConnection owningObject, DbConnectionInternal obj, Transaction transaction)
   at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.Open()
   at Epicor.Data.Provider.EpiConnection.Open() in C:\_releases\ICE\ICE4.2.200.0\Source\Server\Framework\Epicor.System\Data\EpiProvider2\EpiConnection.cs:line 241
   at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext](TTarget target, Action`2 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
   at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(DbConnection connection, DbInterceptionContext interceptionContext)
   at System.Data.Entity.Core.EntityClient.EntityConnection.Open()
   --- End of inner exception stack trace ---
   at System.Data.Entity.Core.EntityClient.EntityConnection.Open()
   at Ice.Services.ContextFactory.CreateContext() in C:\_releases\ICE\ICE4.2.200.0\Source\Server\Framework\Epicor.System\Services\ContextFactory.cs:line 70
   at EFx.CoilProcessing.Implementation.ReceiveCoilsToStockCombinedImpl.C003_CustomCodeCondition()

I’ve tried to figure out how I can add that “erpContext” variable to the function’s variable list and set it once at the beginning of the function, but I can’t figure out the type or what service to get it from.

Any easy answers out there before I start recoding that function?

Thanks,

Joe

Hmm.

I noticed I had unintentionally checked this box:

image

The field help isn’t hooked up and I’m not interested enough to dig into the help to find out what it is.

FWIW.

Joe

You don’t need to create a new context really - you can use the Db context. On the Function Library References, add tables UD102A and WhseBin. Then your calls can look like this:

bool billetBinGood = true;

foreach(var ud102a in Db.UD102A.With(LockHint.NoLock).Where(u => u.Company == CompanyID && u.Key1 == jobNum && u.Key2 == jobSeq && u.Completed_c && !u.Received_c).OrderBy(u => u.Key1).ThenBy(u => u.Key2).ThenBy(u => u.ChildKey1))
{
    var whseBin = Db.WhseBin.With(LockHint.NoLock).Where(w => w.Company == CompanyID && w.WarehouseCode == ud102a.CoilWarehouseCode_c && w.BinNum == ud102a.CoilBinNum_c).FirstOrDefault();
  
    if(whseBin == null)
    {
        billetBinGood = false;   
    }
}

return billetBinGood;

Can’t test it because I don’t have those UD fields present, but should be fine.

Circling back to this. Thanks, Mark.

Joe

@markdamen already answered you. What are you having trouble with?

Just thanking him for his help, in an unclear manner. :slight_smile:

1 Like

Thanks! If it worked please tick that answer as the solution for anyone reading this in the future. Cheers.