Solved

BPM Code Request: Create UDCodes Record

  • 7 October 2021
  • 3 replies
  • 797 views

Userlevel 4

I'm working on a data directive for maintaining a list within the UDCodes table, and I’m asking for help with the appropriate Epicor and/or BO calls, to create a new UDCodes record.  It needs to be in C# code, because the list of records to be created is in a C# List.  I have an example of code where an entry is added to the UD12 table based on this “GetService” setup…

using(var svc = Ice.Assemblies.ServiceRenderer.GetService<Ice.Contracts.UD12SvcContract>()) {

...then a record is created in code and we see 

    ds.UD12.Add(UD12);

    BOUpdErrorTableset boUpdateErrors = svc.UpdateExt(ref ds, false, true, out errorOccurred);

...but when I put the service renderer GetService “using” statement in my directive, I get the error message saying “CS0234    The type or namespace name 'UD12SvcContract' does not exist in the namespace 'Ice.Contracts' (are you missing an assembly reference?)”  In the code design window there is a button for Usings & References; can anyone advise me on what to put in there for the similar calls for the UDCodes table?  Or even for this one for UD12?

I know there are multiple ways to create records in a C# directive, and don’t have a recent working example of code that would create a record in the database, except for UDXX tables which the examples tell me are simpler to work with than other table entries.

Any ideas on how I could add a record to the UDCodes table in C# code, would be much appreciated.

Thanks much,

...Monty.

icon

Best answer by mwilson 8 October 2021, 15:25

View original

3 replies

Userlevel 1

Monty,

When dealing with UD records, you can go directly to the table as such:

 

  UD20 = new Ice.Tables.UD20();
  Db.UD20.Insert(UD20);
  UD20.Company = callContextClient.CurrentCompany;
  UD20.Key1 = Your Key data;
  UD20.Key2 = Key2;
  UD20.Key3 = Key3;
  UD20.Key4 = Key4;
  UD20.Key5 = Key5;

Userlevel 3

Hi @mwilson 

i agree with @jim_rogers  mate, go straight to your UD table and use txScope subroutine code to Add and/or Update, you just need to insert it at the relevant trigger to capture the data you want, it could be ttData or from Db (after getting it of course )

Ice.Tables.UD01 UD01;

using (var txScope = IceContext.CreateDefaultTransactionScope())
      {
        UD01 newRow = new UD01();
        Db.UD01.Insert(newRow);
        newRow.Company = Session.CompanyID;
        newRow.Key1 = xxxxxxxx;
        newRow.Key2 = xxxxxxxx;
        newRow.Key3 = xxxxxxxx;
        newRow.Key4 = xxxxxxxx;
        newRow.Character01 = xxxxxxx;
        newRow.CheckBox01 = true; // or false
        Db.Validate();
        txScope.Complete();
      }

 

Userlevel 4

Got it working; thanks one and all!  I’m starting with the straightforward approach as you suggested, although I really do want to learn how to use the BO calls, references, usings, and contract service renderers!

 

First task is to find the next available CodeID for the class of interest:

Code section that finds next available CodeID key:

 

      // in case UserCode records need to be added, find the next CodeID to be used

      var recMaxUserCode = (from UDCodes_pRow in Db.UDCodes

        where UDCodes_pRow.Company == Session.CompanyID

        && UDCodes_pRow.CodeTypeID == "Silen_Cmp"

        orderby UDCodes_pRow.CodeID descending

        select UDCodes_pRow ).FirstOrDefault();   // note orderby and descending (DESC) to get max CodeID

 

             int intNextUserCode = Convert.ToInt32(recMaxUserCode.CodeID) + 1;

 

Then when it comes time to add a UDCodes table entry:

          using (var txScope = IceContext.CreateDefaultTransactionScope())

          {         

            Ice.Tables.UDCodes recUDCodes = new UDCodes();

            Db.UDCodes.Insert(recUDCodes);

            recUDCodes.Company = Session.CompanyID;

            recUDCodes.CodeID = intNextUserCode.ToString();

            recUDCodes.CodeTypeID = "Silen_Cmp";

            recUDCodes.CodeDesc = strAdd;

            recUDCodes.LongDesc = strParentSearchString;

            intNextUserCode++;

            Db.Validate();

            txScope.Complete();

          } // end using scope

Reply