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).
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;
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
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
/* 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
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;
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;
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;
For a deeper dive, I recommend chapter 1 in PeopleSoft PeopleTools Tips & Techniques by Jim J. Marion









Comments
Post a Comment