Index was outside the bounds of the array in Data Directive Custom Code on UD100

one last shot in the dark before I try on our end. I am not sure why the conversion on the key1 isn’t working. Are there blanks in the key1 field?

lastRecord = (from ud100Rows in Db.UD100.OrderByDescending(i => int.Parse(i.Key1))
				select ud100Rows).First();

@knash sorry but still assigning 1 to every record.

Yeah, I don’t get it either. I’ll probably futz with it again later.

Don’t worry about us Debbie, that one with the leading zeros should be fine.

We’re just a bit odd…

Did you tried to use the latest way for the Key1 field in UD101 table?? I get duplicate key value error, it does creates the Key1 value the first time but it does not do increament and gives me that error, i checked the SQL side as well, and i do not see that column being gen for that NextSeq Db…what am i missing??

Explain more. I believe most of us have switched to using GetNextSequence instead of rolling our own.

I have a function library shared somewhere that’ll do the dirty work for you.

@dishant I stuck with the Custom Code below which is working fine for us. Custom code is in a Method Directive Pre-Processing.

int lastRecord_ID = 0;
Ice.Tables.UD100 lastRecord = null;

try
{
    lastRecord = (from ud100Rows in Db.UD100
                    orderby ud100Rows.Key1 descending
                    select ud100Rows).First();
}
catch {};

if(lastRecord != null) lastRecord_ID = Convert.ToInt32(lastRecord.Key1);

foreach(var record in ds.UD100.Where(rec => rec.RowMod == "A"))
{
    lastRecord_ID++;
    
    int leadingZeros = 10;

    string fmt = new String('0', leadingZeros) + "#";
    
    record.Key1 = lastRecord_ID.ToString(fmt);
}

@klincecum I have run into a glitch with this code since the 2024.1.10 upgrade. We are seeing duplicate record id’s for some users but not others. Having the users Clear Cache seems to stop the dups…not sure for how long. We are encouraging our users to clear cache frequently.
Any thoughts on how to prevent? I have this code in a Pre-Processing Method Directive on Ice.BO.UD100.Update

Switch over to using GetNextSequence.

Gotta run, but I have a function library shared somewhere on here, maybe you or a nice person will find it.

Holler back if you need help, be a bit though.

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

1 Like

Thanks @josecgomez ! I am new to using Functions but was able to get GetNextSequence working between the link you shared and a CodaBears YouTube video. However, the GetNextSequence starts at 1 where I need to start with the UD100.Key1 value of the last record (e.g. 00000001394). I have been playing with the SetNextSequence but not having any luck. Any suggestions?

My function library has the SetNextSequence in there.

You can set it to the next one, and if you need the leading zeros, add that for your key.

I am very new to using functions in BPMs. Do you have an example of what the BPM would look like using the SetNextSequence?
I assume I use a Method Directive - correct? I am currently assigning the sequential number using Ice.BO.UD100.Update. Do I do the SetNextSequence here or somewhere else? Do I create variables for the parameters?

1 Like

You’d only use that once, after that, getnextsequence would return the next in order automatically.

I’ve got a problem I’m working on at the moment but I can come back with a simple example.

Ok I see you have that part already.

Ok, all you need to do is go grab my function library, and use the resthelp, or swagger to run the function SetNextSequence to the value you want next, just once.

Every time you call GetNextSequence for that key, it will bring int back and you just need to add your leading zeros like before.

Thank You! This was a learning experience as I have never used resthelp or swagger but was able to find some documentation and figured it out. I had to add an API Key and Access Scope that included the SetNextSequence function before I could run the function in resthelp.

The leading zeros were required in the original custom code to get around an issue where after the sequence number 10 it kept assigning the number 10 to each subsequent row. I don’t believe the leading zeros are required with the GetNextSequence function or at least from what I am seeing.

One question…can I assume that the sequence returned in GetNextSequence only applies to the sequenceKey and not across the environment? I am using “UD100.Key1” as my sequenceKey so expecting the sequence I set to only apply to this table/field. Apologize if this is a silly question but this whole process has been new to me.

1 Like

It’s specific to the key itself. The key can be any string.