Skip to main content

Application Package Application Class Coding

PeopleSoft is moving more and more to developing in application packages. Event Mapping uses application packages, REST services use application packages, Fluid pages use application packages, and work centers use application packages. So it is prudent that developers know how to code classes and methods. This blog is to help you get started.


A Class

An extensible program-code-template for creating objects. It's where you define initial values for state (member variables) and implementations of behavior (member methods). 

Class Wikipedia

It defines a set of properties and methods that are common to all objects of one type. It short, describes the contents of the objects that belong to it.

Class simple English Wikipedia

For example a class could be a mountain bicycle. It could have color fields, speed fields, break fields, tire fields, etc. A road bike could be related to the mountain bike class. A road bike would have similar fields to a mountain bike class but not exactly the same. Both a mountain bike and a road bike could be a kind of a third class which could be called a bicycle class. In this way, you could create the parts of the program that are the same for both the mountain bike and the road bike in programming the bicycle class, yet the programmer would be free to program how a mountain is different from a road bike without duplicating all of the programming.

In this example, there are three classes: 1) a class called "mountain bike", 2) a class called "road bike", and 3) a class called "bicycle". These classes would be used to make object. Although there is only one class called "mountain bike", there could be many objects that are created from the class called "mountain bike". And, although there is only one class that is called "road bike", many objects of type road bike could be created from this class. The class, bicycle, is actually general and there would probably not be any objects that were only of the class "bicycle". But a mountain bike would be a kind of bicycle and a road bike would also be a kind of bicycle. So, you could say that both mountain bikes and road bikes are from the same class, bicycle.

PeopleCode Example

Here's an example in PeopleCode

class Bicycle

   method MakeBicycle();

   property number BikeNum;

end-class;

class MountainBike extends Bicycle

   method  MakeMountainBike();

   property number MountainBikeNum;

end-class;

class RoadBike extends Bicycle

   method  MakeRoadBike();

   property number RoadBikeNum;

end-class;

The following code shows a correct way to assign a class object, because RoadBike is a subtype of Bicycle.

local Bicycle &Bref = Create RoadBike();

Methods

Notice the classes have methods

A method is a procedure associated with a message and an object. 

When you create a class, you can define methods and fields in it.

A method is like a function, but it can access the private fields and the other private methods in a class.


Hello Examples

Select File New

Application Package


Save and name it HELLO



Right click on HELLO 
Insert App Class




Create the class hello_world and a method within the class sayHello()

class hello_world

   method sayHello() Returns string;

end-class;

Create the method sayHello

method sayHello

   /+ Returns String +/

   Return "hello " | %OperatorId;

end-method;

To test it create an application engine with one PeopleCode Step

File New

Application Engine




Select PeopleCode


/* Import your package:class */

import HELLO:hello_world;

/* Create an object */

Local HELLO:hello_world &hello = create HELLO:hello_world();

/* Create a new instance of hello_world */

Local string &message = &hello.sayHello();

/* Log the test message */

MessageBox(0, "", 0, 0, &message);

Run the app engine and log the output


Click traffic light


Log to a file 



The log file will look something like this

hello USERNAME (0,0)

 Message Set Number: 0

 Message Number: 0

 Message Reason: hello USERNAME  (0,0) (0,0)

Application Engine program HELLO_TEST ended normally


In an application package, other PeopleCode and SQL is available, so we could improve our hello by getting the user description (which is usually the name)

method sayHello

   Local Record &OPRDEFNRec = CreateRecord(Record.PSOPRDEFN);

   /* Build the select */

   

   Local string &sCriteria = " WHERE OPRID = '" | %OperatorId | "'";

   Local string &SQL_Str = "%SelectAll(:1)" | &sCriteria;

   Local SQL &SQL_OPRDEFN = CreateSQL(&SQL_Str, &OPRDEFNRec);

   

   

   Local boolean &Found = &SQL_OPRDEFN.Fetch(&OPRDEFNRec);

   /+ Returns String +/

   Return "hello " | &OPRDEFNRec.OPRDEFNDESC.Value;

end-method;

Now the log file would look something like this:


hello Richley, Robert W (0,0)
 Message Set Number: 0
 Message Number: 0
 Message Reason: hello Richley, Robert W (0,0) (0,0)
Application Engine program HELLO_TEST ended normally

Properties can be defined in the class. These are accessible to the methods

class hello_world

   method sayHello() Returns string;

  /* New method to demonstrate  access to property person */

   method sayHellotoPerson() Returns string;

  /* Define property */

   property string person;

end-class;

method sayHellotoPerson
 /*Notice person is accessible here and predicated with & */
   &person = "Bob";
   /+ Returns String +/
   Return "hello " | &person;
end-method;

Log file will contain:
hello Bob (0,0)
 Message Set Number: 0
 Message Number: 0
 Message Reason: hello Bob (0,0) (0,0)
Application Engine program HELLO_TEST ended normally

Passing parameters 

Parameters can be passed to methods. 

class hello_world
   method sayHello() Returns string;
   method sayHellotoPerson() Returns string;
    /* add example for passing parameter */
   method sayHelloto(&name As string) Returns string;
   property string person;
end-class;

/* Add the method that will receive the parameter */
method sayHelloto
   /+ &name as String +/
   /+ Returns String +/
   Return "hello " | &name;
end-method;

/* Modify the sayHello to call the method sayHelloto */

method sayHello
   /+ Returns String +/
   Local Record &OPRDEFNRec = CreateRecord(Record.PSOPRDEFN);
   /* Build the select */
   
   Local string &sCriteria = " WHERE OPRID = '" | %OperatorId | "'";
   Local string &SQL_Str = "%SelectAll(:1)" | &sCriteria;
   Local SQL &SQL_OPRDEFN = CreateSQL(&SQL_Str, &OPRDEFNRec);
   
   
   Local boolean &Found = &SQL_OPRDEFN.Fetch(&OPRDEFNRec);
   /* Return "hello " | &OPRDEFNRec.OPRDEFNDESC.Value; */
   Return %This.sayHelloto(&OPRDEFNRec.OPRDEFNDESC.Value);
end-method;

Notice the system variable %This. %This is a reference to the hello_world class.It qualifies the sayHellopto method to the hello_world class. 

The log file would contain:
hello Richley, Robert W (0,0)
 Message Set Number: 0
 Message Number: 0
 Message Reason: hello Richley, Robert W (0,0) (0,0)

Inheritance

We can extend the class hello_world. The extended class inherits all the methods and properties of the class hello_world.

/* Import the class that we will  extend */
import HELLO:hello_world;

/* extend the class */
class userhello extends HELLO:hello_world
   method sayHelloToUser(&oprid As string) Returns string;
end-class;

method sayHelloToUser
   /+ &oprid as String +/
   /+ Returns String +/
   Local string &name;
   Local Record &OPRDEFNRec = CreateRecord(Record.PSOPRDEFN);
   /* Build the select */
   
   Local string &sCriteria = " WHERE OPRID = '" | &oprid | "'";
   Local string &SQL_Str = "%SelectAll(:1)" | &sCriteria;
   Local SQL &SQL_OPRDEFN = CreateSQL(&SQL_Str, &OPRDEFNRec);
   
   Local boolean &Found = &SQL_OPRDEFN.Fetch(&OPRDEFNRec);
   &name = &OPRDEFNRec.OPRDEFNDESC.Value;
   
   Return %Super.sayHelloto(&name);
   
end-method;

Notice the use of the system variable %Super. This references the parent class, the "superclass. %Super points the class that the current class extends. 

Test it
Create a new PeopleCode step in the application engine HELLO_TEST
Click on step01. Insert Step/Action


Add code:
/* Import new class */
import HELLO:userhello;

/* Create object */
Local HELLO:userhello &hello = create HELLO:userhello();


/* For the fun of it. loop through some OPRDEFN records */

Local string &message;

Local Record &OPRDEFNRec = CreateRecord(Record.PSOPRDEFN);
/* Build the select */

Local string &Criteria = " WHERE ACCTLOCK = 0";
Local string &SQL_Str = "%SelectAll(:1) " | &Criteria;
Local SQL &SQL_OPRDEFN = CreateSQL(&SQL_Str, &OPRDEFNRec);

Local boolean &Found = &SQL_OPRDEFN.Fetch(&OPRDEFNRec);
Local number &row = 0;
While (&Found And
      &row < 5)
   &message = &hello.sayHelloToUser(&OPRDEFNRec.OPRID.Value);
   &Found = &SQL_OPRDEFN.Fetch(&OPRDEFNRec);
   &row = &row + 1;
   MessageBox(0, "", 0, 0, &message);
End-While;

Log file will look something like this:

hello James Bond (0,0)
 Message Set Number: 0
 Message Number: 0
 Message Reason: hello James Bond (0,0) (0,0)

hello Mickey the Mouse (0,0)
 Message Set Number: 0
 Message Number: 0
 Message Reason: hello Mickey the Mouse (0,0) (0,0)

hello Smith, John (0,0)
 Message Set Number: 0
 Message Number: 0
 Message Reason: hello Smith, John (0,0) (0,0)

hello Charles S Brown  (0,0)
 Message Set Number: 0
 Message Number: 0
 Message Reason: hello Charles S Brown (0,0) (0,0)

hello Kent, Clark J (0,0)
 Message Set Number: 0
 Message Number: 0
 Message Reason: hello Kent, Clark J (0,0) (0,0)
Application Engine program HELLO_TEST ended normally


Real Life Example

So how does this look in real life?

Here's an example for creating a REST service

Create a class SOME_REST_POST and create 2 methods 1 for the request and one for the errors:

class SOME_REST_POST implements PS_PT:Integration:IRequestHandler

   method OnRequest(&_MSG As Message) Returns Message;

   method OnError(&pRequestMsg As Message) Returns string;

end-class;

Create the method for the request:

method OnRequest

   /+ &_MSG as Message +/

   /+ Returns Message +/

   /+ Extends/implements PS_PT:Integration:IRequestHandler.OnRequest +/

   Local Message &returnMsg;

/* Here you would put code to build the response to be returned. */

   Return &returnMsg;

end-method;


In the service request, we link the application package method


When REST service is invoked, the method OnRequest is executed.

For the full example see: PeopleSoft REST services example

For a deeper dive, I recommend chapter 1 in  PeopleSoft PeopleTools Tips & Techniques by Jim J. Marion

Comments

Popular posts from this blog

PeopleSoft REST services

PeopleSoft Rest Service     Here is a step by step example of how to create a REST service in PeopleSoft.  The first example passes one row of data via the URL. The response includes multiple po lines.  The second example passes JSON in the body with no parameters in the URL. The response is one row per row in the request.   1)     Create template document for inbound parameter(s) (the request). 1)     One or more element names. 2)     Create a view record of the outbound data (the response). 3)     Create template document. 1)     Element names, Collections and Compounds. 2)     Map the document elements to fields in your view 4)     Link messages to document 5)     Create your response message which structures ...

Scanning Barcode and QR Code with JavaScript in PeopleSoft

Creating a mobile app with JavaScript is simple and common. Simply find a JavaScript Library and write the code to use it. But how do we do that with PeopleSoft This Blog will give you an example. 1st we need JavaScript Library to Scan both QR and Barcode InstaScan (only did BarCode) https://github.com/schmich/instascan Zxing (did both)  https://github.com/zxing-js/ Great libraries exist, but how do we get that into PeopleSoft? We'll need to add video/camera to Mobile Inventory Pages.  Can this be added? Will the page be able to access the camera? The answer is in PeopleSoft PeopleTools Tips & Techniques by Jim Marion Chapter 6 JavaScript for the PeopleSoft Developer JavaScript Libraries, pages 240-241 IScript/Messages Step 1 Get mimified version of Zxing https://unpkg.com/@zxing/library@0.14.2/umd/index.min.js PeopleTools -> Utilities-> Administration-Message Catalog Add Minified Code to Description Yes, you are correctly seeing that. Store the code in the message c...

Simple PeopleSoft No Code SOAP Web Service

What is a Web Service Simply put a web service is  a method of communication between devices over the World Wide Web (WWW) Characteristics: Uses Request/Response messages over HTTP/S  Hypertext Transfer Protocol The foundation of data communication over WWW Used to send and receive webpages and files on the internet. Messages are text readable such as XML and JSON XML - Extensible Markup Language (rules for encoding documents that are both human and machine readable) JSON – JavaScript Object Notation (file format that uses human readable text)  What is SOAP Simple Object Access Protocol Lightweight protocol for exchanging structured information Use HTTP POST to send XML (Extended Markup Language)\ Provides both Asynchronous and Synchronous  Asynchronous – Doesn’t wait for a response; it is NOT immediate; callbacks happen only when the requested resource is ready Synchronous – Expects an immediate return of data; waits for a response SOAP Example - Web Service from a ...