Setting UD Table Key using C# Expression failed on Convert.ToInt32

I am using UD03, I have created a dashboard for people to create new entry for UD03.

When user create a new entry on UD03, I want Key2 to auto assign to the maximum value of Key2 + 1 with the matching Key1 value.

For example, if the data in UD03 is as follows. If I create a new entry when Key1 = 1000, Key2 should automatically assign to 4.

|1000| 1|
|1000| 2|
|1000| 3|
|1200| 1|
|1200| 2|
|1300| 7|
|1300| 8|

I have created a BPM on Pre processing update where Key2 is empty, it will assign Key 2 using this Expression

((from ud03Row in Db.UD03
where ud03Row.Key1 == ttResultsRow.UD03_Key1; ud03Row.Key2 != "";
select Convert.ToInt32(ud03Row.Key2)).Max()+1).ToString()

It looks to me that as soon as I put the Convert.ToInt32 into the expression, the code will not execute…

If I remove the Convert.ToInt32, the expressiong works fine.

I ran the code on LinqPad 5 with Convert.ToInt32 and works fine…

Tried debugging and I couldn’t get any exception…

Version is Epicor 10…2.200.4

Try not casting inside your linq query. Assign and do all that before it you can.

To cast in EF6 you can use

1 Like

To expand on Haso’s remark: you can use these two directives:
-replicate Key2 to Number02 using a directive on record add.
-On get new record, assign Key2 from Max(number02 + 1).ToString()