Calling Function from Custom Code in In-Transaction Data Directive

Is it possible to call a function from custom code in a data directive (in transaction)? I am using a function to write data to a UD table to act as a change log on tables that don’t have it as an option. It will also be used to capture deleted records for which the Epicor change log does not record.

I thought I had a working solution using a standard data directive (with call function widgets) until multiple records were updated at the same time. So, it seems like in-tran data directive is where this needs to be to capture changes on multiple records updated simultaneously. I guess this makes sense as that is where Epicor’s change log is set?

Custom Code, per other threads I have seen to call the function is below. This has worked on a standard dd but not in-transaction. I tried adding clarification to the row mod to target the correct row, but that didn’t seem to help.

```<foreach (var row in ttTABLE
.Where(r => r.RowMod == “U” || r.RowMod == “A”)) // Only updated or added rows
{
var table = “TABLE”;
var tableKey1 = “test”;
var tableKey2 = “test”;
var tableKey3 = “”;
var type = “Updated”;
var Char03 = “Test”;
var Char04 = “”;
var Char05 = “”;

this.InvokeFunction(“Library”, “Function”,table,tableKey1,tableKey2,tableKey3,type,Char03,Char04,Char05);
}> ```

Error on in-transaction. Same error if I update multiple records or just one.

Any ideas on where to go from here?

What table? That can be fixed.

As for your issue, try this so we can get some more info:

It’s probably a transaction in a transaction issue, so we might need to work around it.

try
{
    foreach (var row in ttTABLE.Where(r => r.RowMod == "U" || r.RowMod == "A")) // Only updated or added rows
    {
        var table = "TABLE";
        var tableKey1 = "test";
        var tableKey2 = "test";
        var tableKey3 = "";
        var type = "Updated";
        var Char03 = "Test";
        var Char04 = "";
        var Char05 = "";

        this.InvokeFunction("Library", "Function", table, tableKey1, tableKey2, tableKey3, type, Char03, Char04, Char05);
    }
}
catch (Exception ex)
{
    throw new Ice.BLException("Failed while invoking Library.Function.\n\n" + ex.ToString());
}

CustXPrt and EquipPlan

Not an expert with custom code. Is there anything else I need to add to get your addition to run?

image

No, show the whole screen.

you lost the first line saying try

Does this give you a better idea?

And you know the saying, "If at first you don’t succeed, Try “try {” again.

Yep…

but you may want to try this first:

FixChangeLog.efxj (1.5 KB)

//Turns on Change Log For a Database Table

try
{
  var record = Db.ZDataTable.FirstOrDefault(x => x.DataTableID == DataTableID);
  
  if(record == null)
  {
      output = "Table not found!";
      return;
  }
  
  record.ChgLogID = DataTableID;
  
  Db.SaveChanges();
  
}
catch(Exception ex)
{
    output = ex.ToString();
}

And for your specific issue… you can try this, but I’d recommend just turning on the Change Log for the table and using that, unless you have a specific reason.

//using System.Transactions;
try
{
    using (var scope = new TransactionScope(TransactionScopeOption.Suppress))
    {
        foreach (var row in ttTABLE.Where(r => r.RowMod == "U" || r.RowMod == "A")) // Only updated or added rows
        {
            var table = "TABLE";
            var tableKey1 = "test";
            var tableKey2 = "test";
            var tableKey3 = "";
            var type = "Updated";
            var Char03 = "Test";
            var Char04 = "";
            var Char05 = "";

            this.InvokeFunction("Library", "Function", table, tableKey1, tableKey2, tableKey3, type, Char03, Char04, Char05);
        }

        scope.Complete();
    }
}
catch (Exception ex)
{
    throw new Ice.BLException("Failed while invoking Library.Function.\n\n" + ex.ToString());
}

Thanks for the change log function. I was able to install for one of the missing the tables. That being said, deleted records are still going to be a problem since it seems that deleting records from the table (EquipPlan) also deletes the changes recorded in ChgLog. I thought I read elsewhere that this behavior is inconsistent from table to table when deleting records.

New error with the in-tran custom code. I guess how difficult is it to get a function to run in-tran? I was thinking it would be similar to a standard directive, but maybe that isn’t the case and I need to rethink other options.

No, it’s consistent. Deletes are gone.

I believe,
This error is created by having the Require Transaction checkbox checked in the Function. (Or not checked)
Basically you are calling something in this funciton which is triggereing another transaction and the system doesn’t like having a transaction inside a transaction.

Since you are invoking from InTran you already have an Open Transaction, and are now trying to crate another inside the function.

Since it’s Friday, you can also vote on improving logging:

Change Log Enhancements and Configurability

I thought there was an idea for external logging too so that the logs are immutable and not lost if the database is corrupted or deleted, but I can’t find it any more.