Search code examples
genexus

It's possible to use the function max() in a variable with character type?


I need to get the max number of an character record in my database.

Yes, this column is defined Character but contains a sequence of numbers, we can't change in database the type of this record to numeric.

So, i try to get the max number of this character in this way:

&embacod = str(max(EmbaCod.ToNumeric(), + 1))

But i receive:

error spc0026: Formula 'max( val( EmbaCod ) ) ' cannot be evaluated in this program.

Also tried:

&EmbaCods = DpEmbalagens()
for &P0126 in &EmbaCods order (&P0126.EmbaCod.ToNumeric())
    &embacod = &P0126.EmbaCod
    exit
endfor

There's a workaround to get this max character number?


Solution

  • The max aggregate/select function is defined only for attributes and not expressions.

    The workaround is to define a Numeric formula in the same transaction where EmbaCod is defined:

    EmbaCodFor of type Numeric(...) with formula EmbaCod.ToNumeric()

    and then in your code use

    &embacod = str(max(EmbaCodFor))