PeopleCode | Defaulting a field by setting it to the next available value
A. What’s Next?
A common programming requirement is to create a field that automatically defaults to the next available value whenever a new row is created. The following code added to the ‘RowInit’ event of the field in question will perform this task. This particular example assumes that a field called SEQNUM exists on a ‘Level 1’ scroll area.
/* Declare Variables */ Local integer &maxNum, &i; Local Record &level1Rec; Local Rowset &level1RS; /* Only execute this code if the field is currently blank */ If None(RECORD_NAME.SEQNUM) Then &level1RS = GetLevel0().GetRow(1).GetRowset(Scroll.RECORD_NAME); &maxNum = 0; /* Determine current maximum number */ For &i = 1 To &level1RS.ActiveRowCount &level1Rec = &level1RS.GetRow(&i).RECORD_NAME; If Value(&level1Rec.SEQNUM.Value) > &maxNum Then &maxNum = Value(&level1Rec.SEQNUM.Value); End-If; End-For; /* Assign next available number */ RECORD_NAME.SEQNUM = &maxNum + 1; End-If;
Note that this code does not take gaps into account. It is merely searching for the current maximum value and then adding ‘1’ onto this value for the new row. For most purposes – where the actual number makes no difference – this code will do the job fine.