How to Generate and Assign Unique Sequence Number in PeopleCode

Assigning a unique number to the Sequence Number field (or simply a number field) is something we have to do in some point as a PeopleSoft developer. The typical scenario is, you have a record which store multiple set of details and you need to identify each row uniquely. So adding a Sequence Number to the record is pretty common approach and each time you add a row to the record then the Sequence Number will get incremented and assign the next available number to the newly added row. Throughout PeopleSoft Applications (any PeopleSoft application) you can find numerous examples. Most of the time we want to auto assign a sequence number for records which are in Scroll Areas or Grids.
To achieve this you can write a function in a FieldFormula event that implement the algorithm to generate the unique sequence number for a given record. Then, you can basically use that function in RowInset (for example) or any other event as you want.

Below function is implemented in FieldFormula Event

/*This function will Increment and assign unique number to the SEQNBR field;
Input Parameter - Record Object (passed as reference)
This example assume that your record has a field called SEQNBR - (PeopleSoft delivered number field)
You can modify this function to pass your custom Number Field as well, 
so that you don't need to assume SEQNBR is a field in the passing record*/
Function GenerateSeqNbr(&recName As Record)
   
   Local integer &intMaxSeqNbr = 0;
   Local integer &I, &intSeqNbr;
   Local Rowset &RS = GetLevel0()(1).GetRowset(@("Scroll." | &recName.Name));
   
   For &I = 1 To &RS.ActiveRowCount;
      &intSeqNbr = &RS(&I).GetRecord(@("Record." | &recName.Name)).SEQNBR.Value;
      If &intSeqNbr > &intMaxSeqNbr Then
         &intMaxSeqNbr = &intSeqNbr;
      End-If;
   End-For;
   
   &intSeqNbr = &intMaxSeqNbr + 1;
   &recName.SEQNBR.Value = &intSeqNbr;
   
End-Function;
/* To call the function*/
Declare Function GenerateSeqNbr PeopleCode MY_DRV_RECORD.SEQNBR FieldFormula;

/*call the GenerateSeqNbr function for the current record from calling event*/
GenerateSeqNbr(GetRecord());
SHARE

Ayesha Wee

    Blogger Comment
    Facebook Comment

3 comments :

  1. delivered functions: GetNextNumber,GetNextNumberWithGaps and GetNextNumberWithGapsCommit.

    ReplyDelete
  2. Anonymous 24 July 2015 at 06:54: Those are useful functions too.

    ReplyDelete
  3. What if the seqnum is on level 2. How do you tell the level 2 which level 1 row you are on?

    ReplyDelete