Sequence Function Ice.IceContext

Anyone have any ideas on the best way to get a new sequence from an Epicor function?
In a recent thread, Jose recommended to use code like this in a BPM to get a new sequence:

// Bring in Ice.Lib.NextValue.dll
const string sequenceName = "InvcGrpIDAA";
var nextVal = new Ice.Lib.NextValue(Db);    
int nextGroupID = nextVal.GetNextSequence(sequenceName);

I added a reference to Ice.Lib.NextValue.dll but cannot figure out how to pass in Db context. If I give the library access to read data by pulling in a table reference to a random table, this.Db is available but it seems to be of a different type. I get the following error:

Argument 1: cannot convert from 'EFx.MyFunction.Implementation.ILibraryContext' to 'Ice.IceContext'

Any help or thoughts are appreciated

The Db object does not exist in functions until you add a table as a reference,
and then, the context is limited.

I am unsure if the limited Db context will work, but let’s find out.

If it does not, you can make a new one, but then it’s real easy to break the security model
for functions lol.

Edit: Half of that was in your post lol

Thanks for the quick response. Out of curiousity, how do I spin up a new one or is that secret sauce

I’ll share the secret sauce. It’s not exactly a secret, just one of those things you need to be careful
with, as you could very easily do things that are not recommended.

Screenshot 2023-06-28 102802

:rofl:

ErpContext dbContext = new ErpContext();

// Bring in Ice.Lib.NextValue.dll
const string sequenceName = "InvcGrpIDAA";
var nextVal = new Ice.Lib.NextValue(dbContext);    
int nextGroupID = nextVal.GetNextSequence(sequenceName);

//This is just the output of my function
output = nextGroupID.ToString();

If you want to make @Mark_Wonsil happy, we can just build this into a UBAQ and call that.

It would be nice if GetNextSequence was a bonafide service where you just passed in a key. :person_shrugging:

Well, in the meantime, this is so easy, I’ll make a proper function and share it.
Thanks to @josecgomez for pointing it out and @timshuwy for showing us the way.

If you are going to instanciate your own context at least use the context factory service

var Dbcontext = Ice.Services.ContextFactory.CreateContext(); //Ice Context
//or 
var Dbcontext =Ice.Services.ContextFactory.CreateContext<ErpContext>(); //ERP Context

paper bag GIF

So something like this?

using(var dbContext = Ice.Services.ContextFactory.CreateContext<ErpContext>())
{
  var nextVal = new Ice.Lib.NextValue(dbContext);
  int nextNum = nextVal.GetNextSequence("MySequence");
  this.outNextVal = string.Format("{0:00000000}.xml",nextNum); //Ice.SysSequence table
}

Yes, better :slight_smile:

Function:
https://www.epiusers.help/t/lets-share-useful-functions-sharing-is-caring/100371/23