Prefix and Random Number

,

Afternoon,

Can anyone help me with a little code… I’m currently in GetaNewUD30 - Post Processing

I have a variable of LoadNum and I need to get that Field which is LoadNum_c on UD30_UD with the following ABC10001 etc it also sets the Key1 on UD30 before setting LoadNum_c

Here is what I got already… I had a similar issue which was resolved but now I’m adding a prefix which is making it even more complex.

image

Unfortunately I’m getting this…

image

The numbers aren’t going up 10002, 10003, 10004… :frowning:

Is it your desire that the “highest” (alpha-numberically) key with a specified prefix be incremented?

Like if you have UD30 records with Key1: 1001, 1100, TEA111, and TEA1111

And you add a record with prefix TEA, the Key1 field for that ne record would TEA1112 ??

"TEA"+"1112" where the "1112" is 1) "1111" converted to a number, 2) that number incremented by 1, and finally 3) converted back to a string

Can you just make Key1 your prefix in Key2 the sequence?

That would make things a tad easier.

Either way, you have the issue of the number of digits in the “numeric part”, and the fact that the whole thing is a string. sorting the numeric part would go:
1, 10, 100, 101, 102, ...109, 11, 110, 111, 112... 119, ...

So you might want to pad the numeric part with leading zeros. Then the “Order By” in the LINQ query would be straightforward.

Thanks Calvin,

In simple terms.

I simply want to set Key1 to TEA1000 and when a user presses New it will create TEA1001 and so on.

I’m guessing this would become very difficult because it’s a string? Is there no way to write so it will automatically increment? I tried copying and modifying your last help but it messed it all up because it was a string and not an number field.

Do you have any example code that could kick start me? Happy to fill in the blanks and may need to ask you more questions.

Thank you for your time, really appreciate it.

Is it always going to be the same prefix? Or you pass the desired Prefix along and could have records like:

ABC0001
ABC0002
TEA0001
XYZ0001
XYZ0002
ABC0003
TEA0002
PDQ0001

(That’s the order they were created)

If yes to varying prefixes, then the LINQ query would have the Where clause include a match for the prefix as well. If the desired prefix was TEA (and the records above already existed), The query would return "TEA0002". You could then take that result and use a string substitution (subbing “”for“TEA”). Then you'd have the string "0002"which could be converted to an integer. From there you'd add one to the int, convert back to a strin (don't forget to pad with leading zeros!), tack on the prefix, and you'd have“TEA0003” as your new value.

The following “does something” :wink:

And maybe even close to what you want:
(the Show Message blocks are just for my debugging, and I use Character01 inplace of your LoadNum_c)

image

First Set Arg/Var sets strLoadNum with expression:

Db.UD30
  .Where(r => r.Company == callContextClient.CurrentCompany)
  .Select(r => r.Character01)
  .DefaultIfEmpty("0000")
  .OrderByDescending(r => r)
  .First()

The second set Arg/Var sets intLoadNum with expression

Convert.ToInt32(strLoadNum)+1

The Set field widget sets UD30.Key1 of the added row to the expression:

"TEA"+ intLoadNum.ToString("0000") 

the second set field sets the Character01 field (LoadNum_c for you) with expression:

intLoadNum.ToString("0000")
1 Like

Hopefully cow!

Thank you I will test it tomorrow and let you know as it’s 21:00 here.

Does knowing all of this just come with experience of epicor over the years or have you done training courses? I’d really like to be stronger in c#. I have the books and linq books slowly getting there.

Thank you Calvin. If I could buy you a beer I would. :beer: