Copy UD table parent and child record set - sample code

Hey y’all,

For what it’s worth.

I needed to copy a record set in UD100 / UD100A and didn’t want to manually copy each field. From a couple of posts here I constructed this. Maybe it’ll help somewhere along the line.

Note that dsUD100 already contains the record we want to copy from by the time we get to this code.

This is in a function, so we have to get the erpContext.Db at the top, which needs an assembly added on the function. In a BPM you wouldn’t need this part. The credit check assembly was a random thing that worked. All we need from it is the Db. (If there’s a better way to do this, please chime in.)

Enjoy.

Joe

// copy header and detail

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

Ice.Contracts.UD100SvcContract ud100Entry = Ice.Assemblies.ServiceRenderer.GetService<Ice.Contracts.UD100SvcContract>(erpContext.Db, true);

var dsUD100New = new Ice.Tablesets.UD100Tableset();

using (var txScope = IceDataContext.CreateDefaultTransactionScope())
{
  var sourceRow = (from row in dsUD100.UD100 
  where row.Company == Session.CompanyID &&
  row.Key1 == busUnit &&
  row.Key2 == currentCINum &&
  row.Key3 == revisionNum
  select row).FirstOrDefault();
  {
    ud100Entry.GetaNewUD100(ref dsUD100New);
  
    var newRow = (from row in dsUD100New.UD100 
    where row.Company == Session.CompanyID &&
    string.Equals(row.RowMod, IceRow.ROWSTATE_ADDED, StringComparison.OrdinalIgnoreCase)
    select row).FirstOrDefault();
    
    if (newRow != null)
    {
      **BufferCopy.CopyExceptFor(sourceRow, newRow, UD100.ColumnNames.SysRevID, UD100.ColumnNames.SysRowID); // don't copy the sysrev and sysrowid**

      //Add to the data set
      newRow.Key2 = newCINum;
      newRow.Key3 = "0001"; 
      newRow.RowMod = IceRow.ROWSTATE_ADDED;
      
      // Commit to Database
      ud100Entry.Update(ref dsUD100New);

      // copy detail
      foreach(var sourceDtlRow in (from row in dsUD100.UD100A
      where row.Company == Session.CompanyID && 
      row.Key1 == busUnit &&
      row.Key2 == currentCINum &&
      row.Key3 == revisionNum
      select row))
      {
        ud100Entry.GetaNewUD100A(ref dsUD100New, sourceRow.Key1, sourceRow.Key2, sourceRow.Key3, sourceRow.Key4, sourceRow.Key5);
      
        var newDtlRow = (from row in dsUD100New.UD100A 
        where row.Company == Session.CompanyID &&
        string.Equals(row.RowMod, IceRow.ROWSTATE_ADDED, StringComparison.OrdinalIgnoreCase)
        select row).FirstOrDefault();
        
        if (newDtlRow != null)
        {
          BufferCopy.CopyExceptFor(sourceDtlRow, newDtlRow, UD100A.ColumnNames.SysRevID, UD100A.ColumnNames.SysRowID);
    
          //Add to the data set
          newDtlRow.Key2 = newCINum;
          newDtlRow.Key3 = "0001"; 
          newDtlRow.RowMod = IceRow.ROWSTATE_ADDED;
          
          // Commit to Database
          ud100Entry.Update(ref dsUD100New);
        }
      }
    }
  }

  // Complete Transaction
  txScope.Complete();
}
1 Like