Publishing Events

You use PeopleCode to publish an event:

  • For custom endpoint subscriptions, create an object of the PSEvent class to publish the event.

  • For certain delivered endpoint subscriptions, instantiate and use delivered applications classes to publish to that endpoint—for example, to the Notification window.

Publishing an Event for KeyValue Pairs

This example demonstrates the publishing of the event PROCESSSTATUSCHANGE with the keys PROCESSID and RUNSTATUS and recipient user, VP1.

  1. Create the event object.

    Local object &EventObject;
    &EventObject = CreateObject("PSEvent");
    
  2. Set values for the keys defined in the event

    &EventObject.SetKeyValuePair("PROCESSID", PSPRCSSTATUS.PROCESSID);
    &EventObject.SetKeyValuePair("STATUS", PSPRCSSTATUS.RUNSTATUS);
    
  3. Add a recipient user for the event.

    In this case, the event will be sent only to user VP1 (if VP1 has subscribed to the event).

    &EventObject.AddRecipient("VP1", 1)

    The the parameters for AddRecipient are:

    Field or Control Description

    recipient

    Specify the recipient using either a user name or a role name.

    recipient type

    Use a value of 1 to indicate a user.

    Use a value of 2 to indicate a role.

  4. Publish the event.

    &EventObject.Publish("PROCESSSTATUSCHANGE");

Publishing an Event for a PeopleCode Rowset

If the data type is PeopleCode rowset, you set the values for the fields in the record and add a recipient.

This example illustrates publishing an event for a PeopleCode rowset. In this example, WORKFLOWAPPROVAL is the server event for which name and status fields are defined. All users assigned the role of Workflow Administrator are added as recipients.

  1. Create the event object.

    Local object &EventObject;
    &EventObject = CreateObject("PSEvent");
    
  2. Set the PeopleCode Rowset for the event.

    Local object &EventObject;
    &rs = CreateRowset(Record.PSPTWKLAPPVL);
    &rs.InsertRow(&i);
    &rs(&i).PSPTWKLAPPVL.NAME.Value = "Test Workflow";
    &rs(&i).PSPTWKLAPPVL.STATUS.Value = 1;
    &EventObject.SetRowsetData(&rs);
    
  3. (Optional) Add a recipient role for the event.

    In this case, the event will be sent only to users who have subscribed to the event and belong to the role: Workflow Administrator.

    &EventObject.AddRecipient("Workflow Administrator", 2);
  4. Publish the event.

    &ret = &EventObject.publish("WORKFLOWAPPROVAL");

Publishing to the Notifications Panel

For example, the Notifications panel endpoint is delivered with a subscription to the PUSHNOTIFICATIONWINDOW event collection as seen previously on the Define Server Event Collections page. Once you have defined and added your custom event to the PUSHNOTIFICATIONWINDOW event collection, you can instantiate and use the PTPN_PUBLISH:PublishToWindow application class to publish your custom event to the Notifications panel.

The following PeopleCode excerpt illustrates the publication of a notification of type alert:

import PTPN_PUBLISH:PublishToWindow;

/* Creates and publishes an alert notification to the current user. */
Local string &KEY = "Student_ID" | PSU_STUDENT_TBL.STUDENT_ID.Value;
Local string &MSGINFO1;
&MSGINFO1 = "You added a new student named " | PSU_STUDENT_TBL.STUDENT_NAME.Value | " with ID " | PSU_STUDENT_TBL.STUDENT_ID.Value | ".";
Local PTPN_PUBLISH:PublishToWindow &newStdnt_alert = create PTPN_PUBLISH:PublishToWindow("NEW_STUDENT", "Student Data");
&newStdnt_alert.SetCategoryTypeFyi(); /* Type for alerts. */
&newStdnt_alert.AddRecipient(%UserId, 1);
&newStdnt_alert.SetMsgKey(&KEY);
&newStdnt_alert.SetMsgInfo(&MSGINFO1);
&newStdnt_alert.SetMsgStateNew();
&newStdnt_alert.SetPriority("3"); /* Low priority */
&newStdnt_alert.Publish("");