Raising Events

After you determine that an occurrence of the business event exists that the event in the Event Manager framework represents, you can raise the event in the Event Manager framework.

The Event Manager framework provides an API that creates and raises events. The API code is:

import EOEN_MVC:EOEN_MODEL:EOENInterface;
Local EOEN_MVC:EOEN_MODEL:EOENInterface &myEvent;
         Local Record &recContext;
         
         &myEvent = create EOEN_MVC:EOEN_MODEL:EOENInterface("", 0);
         
         &recContext = &rsContextRec(1).GetRecord(Record.CONTEXT_REC);
         &myEvent.AddContextRecord(&recContext);
         &myEvent.RaiseEvent("EventName");

Pass the name of the event to the Event Interface, which returns an instance of the event object. Add the context record and then execute the RaiseEvent method on the event object.

Important! Instantiating an event object does not automatically raise the event. You must invoke the event's RaiseEvent method to raise the event and initiate subsequent Event Manager framework processing.

In the following example, assume that we have created an event called AssignmentMilitaryRankChanged. This event represents the business event of assigning or updating the military rank of a person's assignment (Job record) instance. Whenever the rank attribute of the assignment (Job record row) changes, we want to raise the event. Here is the pseudocode to add to the JOB_MIL.MIL_WORN_RANK:SavePostChange PeopleCode event for the Job component:

import EOEN_MVC:EOEN_MODEL:EOENInterface;

/**********************************************************/
/* FUNCTION:    GetPriorRank                              */
/* Inputs:      &rsJOB As Rowset, &CurrentRow as number   */
/* Returns:     none                                      */
/*                                                        */
/* Get Prior Worn Rank                                    */
/**********************************************************/
Function GetPriorRank(&rsJOB As Rowset, &CurrentRow As integer, &PRIOR_ROW As integer, &PRIOR_RANK As string);
   
   Local date &PRIORDT = Date3(1900, 1, 1);
   Local integer &PRIORSEQ = 0;
   Local date &JOB_Effdt = &rsJOB(&CurrentRow).JOB.EFFDT.Value;
   Local integer &JOB_Effseq = &rsJOB(&CurrentRow).JOB.EFFSEQ.Value;
   Local integer &i;
   
   &PRIOR_ROW = 0;
   &PRIOR_RANK = " ";
   For &i = 1 To &rsJOB.RowCount;
      Local date &FETCH_EFFDT = &rsJOB(&i).JOB.EFFDT.Value;
      Local integer &FETCH_EFFSEQ = &rsJOB(&i).JOB.EFFSEQ.Value;
      If (&FETCH_EFFDT < &JOB_Effdt Or
            (&FETCH_EFFDT = &JOB_Effdt And
               &FETCH_EFFSEQ < &JOB_Effseq)) And
            (&FETCH_EFFDT > &PRIORDT Or
               (&FETCH_EFFDT = &PRIORDT And
                  &FETCH_EFFSEQ > &PRIORSEQ)) Then
         &PRIORDT = &FETCH_EFFDT;
         &PRIORSEQ = &FETCH_EFFSEQ;
         &PRIOR_ROW = &i;
      End-If;
   End-For;
   If &PRIOR_ROW > 0 Then
      &PRIOR_RANK = &rsJOB(&PRIOR_ROW).GetRowset(Scroll.JOB_MIL)(1).JOB_MIL.MIL_WORN_RANK.Value;
   End-If;
End-Function;

/***************************************************************************/
/* FUNCTION:    RaiseMilitaryRankEvent                                     */
/* Inputs:      JOB_MIL rowset, Current row                                */
/* Returns:     none                                                       */
/*                                                                         */
/* Check if MilitaryRankEvent event should be raised                       */
/* if a change has been done:                                              */
/*    - Worn rank changed                                                  */
/*    - Row deleted and the worn rank is different from the previous one   */
/*    - Row inserted and the worn rank is different from the previous one  */
/* the event will be raised                                                */
/***************************************************************************/
Function RaiseMilitaryRankEvent(&rsJOB As Rowset)
   If INSTALLATION.MILITARY = "Y" Then
      Local integer &PRIOR_ROW;
      Local string &PRIOR_RANK;
      Local boolean &RaiseEvent = False;
      Local integer &i;
      
      For &i = 1 To &rsJOB.RowCount
         /* Get current Job row and Worn Rank value */
         Local Row &rowJOB = &rsJOB.GetRow(&i);
         Local Rowset &rsJOB_MIL = &rowJOB.GetRowset(Scroll.JOB_MIL);
         /*************************/
         /* Rank has been updated */
         /*************************/
         If &rsJOB_MIL(1).JOB_MIL.MIL_WORN_RANK.IsChanged Or
               (&rowJOB.JOB.EFFDT.IsChanged And
                  Not &rowJOB.IsNew) Then
            If All(&rsJOB_MIL(1).JOB_MIL.MIL_WORN_RANK.Value) Then
               &RaiseEvent = True;
               Break;
            End-If;
            
         Else
            /***************/
            /* New JOB row */
            /***************/
            If &rowJOB.IsNew Then
               /* Get previous Rank */
               GetPriorRank(&rsJOB, &i, &PRIOR_ROW, &PRIOR_RANK);
               If &PRIOR_ROW > 0 Then
                  If &rsJOB_MIL(1).JOB_MIL.MIL_WORN_RANK.Value <> &PRIOR_RANK And
                        All(&rsJOB_MIL(1).JOB_MIL.MIL_WORN_RANK.Value) Then
                     &RaiseEvent = True;
                     Break;
                  End-If;
               Else
                  /* No previous Rank */
                  If All(&rsJOB_MIL(1).JOB_MIL.MIL_WORN_RANK.Value) Then
                     &RaiseEvent = True;
                     Break;
                  End-If;
               End-If;
            Else
               /****************************/
               /* JOB row has been deleted */
               /****************************/
               If &rowJOB.IsDeleted Then
                  /* Get previous Rank */
                  GetPriorRank(&rsJOB, &i, &PRIOR_ROW, &PRIOR_RANK);
                  If &PRIOR_ROW > 0 Then
                     If &rsJOB_MIL(1).JOB_MIL.MIL_WORN_RANK.Value <> &PRIOR_RANK And
                           All(&rsJOB_MIL(1).JOB_MIL.MIL_WORN_RANK.Value) Then
                        &RaiseEvent = True;
                        Break;
                     End-If;
                  Else
                     /* No previous Rank */
                     If All(&rsJOB_MIL(1).JOB_MIL.MIL_WORN_RANK.Value) Then
                        &RaiseEvent = True;
                        Break;
                     End-If;
                  End-If;
               End-If;
            End-If;
         End-If;
      End-For;
      If &RaiseEvent Then
         
         Local EOEN_MVC:EOEN_MODEL:EOENInterface &myEvent;
         Local Record &recContext;
         
         &myEvent = create EOEN_MVC:EOEN_MODEL:EOENInterface("", 0);
         
         &recContext = &rsJOB_MIL(1).GetRecord(Record.JOB_MIL);
         &myEvent.AddContextRecord(&recContext);
         &myEvent.RaiseEvent("AssignmentMilitaryRankChanged");
      End-If;
   End-If;
End-Function;

If GetLevel0()(1).GetRowset(Scroll.JOB).GetRow(CurrentRowNumber(1)).RowNumber = 1 Then
   RaiseMilitaryRankEvent(GetLevel0()(1).GetRowset(Scroll.JOB));
End-If;

The lines in the preceding code that deal with the action of raising the event are:

Local EOEN_MVC:EOEN_MODEL:EOENInterface &myEvent;
         Local Record &recContext;
         
         &myEvent = create EOEN_MVC:EOEN_MODEL:EOENInterface("", 0);
         
         &recContext = &rsJOB_MIL(1).GetRecord(Record.JOB_MIL);
         &myEvent.AddContextRecord(&recContext);
         &myEvent.RaiseEvent("AssignmentMilitaryRankChanged");

Important! During the course of raising an event, the framework causes certain tracking and logging information to be stored in the database. Because of this, events can be raised only from PeopleCode events that normally allow database updates. More specifically, events can be raised only from SavePreChange, SavePostChange, and Workflow PeopleCode events.