Compile and Run PeopleCode Online

There are many times where I come across some sample PeopleCode on the internet and I want to execute the PeopleCode in my environment to see the output.  The route I take to test drive some PeopleCode usually involves me opening up an existing object in App Designer, pasting in the sample code, and then going to the PIA to see the results.  I find this process rather tedious to perform just to see the output of some sample code.  Not to mention, I have to make sure I go back into App Designer and clean up the object I modified.  More often than not, I already have a PeopleSoft session open in my web browser when I am exploring PeopleCode online.  So what I decided to do was build an online utility for compiling and running PeopleCode directly in the PIA.  In this post, I will share this helpful utility for anyone that is interested in this functionality.

This solution follows a recurring theme of using the %metadata Application Package to update PeopleTools-managed objects through PeopleCode.  I have a couple of recent posts on this topic, one is on understanding the %metadata Application Package and the other is about using the %metadata Application Package to create an online PeopleCode event editor.  The technicalities of this solution are very similar to that of the online PeopleCode editor project.

CLICK HERE to download the App Designer project for this utility.  Unzip the file and copy the project from file into App Designer.

The only setup that is needed to use this utility is the assignment of a Permission List. Login to the PIA and assign the PSM_RUN_PC Permission List to a role that you want to have access to the PeopleCode executer.  The user that gets access to the PeopleCode executer must also have the necessary privileges to update Application Package objects.  This is because the utility updates an Application Class method with the inputted PeopleCode to run.

After performing the security setup, you can login as the privileged user.  The utility can be found by navigating to Main Menu > PSM Projects > Run PeopleCode.

Run PeopleCode

On this page you will be greeted with an empty input box, a save button, and a run button.  The usage of the utility is simple:  Paste in some PeopleCode, click the save button to compile the PeopleCode, and then click the run button to execute the PeopleCode.

In this example I wanted to run some sample PeopleCode to output the PS_HOME and PS_CFG_HOME environment variables.

Local string &sOutput;
&sOutput = "PS_HOME: " | GetEnv("PS_HOME") | Char(10);
&sOutput = &sOutput | "PS_CFG_HOME: " | GetEnv("PS_CFG_HOME");
MessageBox(0, "", 0, 0, &sOutput);

After pasting in the code and clicking the save button, the PeopleCode is ready to be executed. In this example clicking the run button results in a messagebox displaying the environment variables.

Output

How it Works

The PeopleCode behind the save button is responsible for updating the Application Class PeopleCode of the generic RunPC Application Class within the PSM_RUN_PC Application Package.  The code uses the %metadata Application Package to compile and save the Application Class PeopleCode.  Here is the code behind the save button that updates the Application Class PeopleCode using %metadata:

import %metadata:Key;
import %metadata:PeopleCodeProgram:*;
import %metadata:AppPackageDefn:*;

Local %metadata:PeopleCodeProgram:PeopleCodeProgram_Manager &mgrPeopleCodeProgram = create %metadata:PeopleCodeProgram:PeopleCodeProgram_Manager();
Local number &int1, &int2;
Local string &strErr;
Local boolean &bSaved, &bResult;
Local %metadata:PeopleCodeProgram:PeopleCodeProgram &defn;

/* Create the key to reference the generic RunPC Application Class */
Local %metadata:Key &key = create %metadata:Key();
&key.AddItem(key:Class_ApplicationPackage, "PSM_RUN_PC");
&key.AddItem(key:Class_ApplicationClass, "RunPC");
&key.AddItem(key:Class_Method, "OnExecute");

/* Display an error if the object definition doesn't exist */
If Not &mgrPeopleCodeProgram.DefnExists(&key) Then
 throw CreateException(0, 0, "Definition Error");
End-If;

/* Get an updateable object definition so that we can overwrite the existing PeopleCode */
&defn = &mgrPeopleCodeProgram.GetDefnToUpdate(&key);

/* Insert the new PeopleCode into the Run method within the boilerplate Application Class code */
&appClassText = "class RunPC method Run();end-class; method Run " | PSM_RUN_PC_WK.PCTEXT.Value | " end-method;";

/* Attempt to compile the Application Class PeopleCode */
&bResult = &defn.UpdateProgram(&appClassText, &strErr, &int1, &int2);

/* Display an error if the code failed to compile */
If Not (&bResult) Then
 /* I think &int1 and &int2 can be used to determine where exactly in the code the error occured */
 throw CreateException(0, 0, MsgGetText(158, 20153, "PeopleCode Error") | " " | &strErr);
End-If;

/* Attempt to update the object definition */
&bSaved = &defn.UpdateDefn();

/* Display an error if the object definition failed to update */
If Not (&bSaved) Then
 throw CreateException(158, 20152, "Error saving new PeopleCode.");
End-If;

/* Enable the Run button and disable the Save button */
PSM_RUN_PC_WK.BUTTON1.Enabled = True;
PSM_RUN_PC_WK.BUTTON.Enabled = False;

If the provided PeopleCode is invalid, then an error would be displayed to the user when they click the save button.  If the code compiles and saves successfully, then the following code behind the run button is used to execute the updated Application Class method that contains the PeopleCode to run:

import PSM_RUN_PC:RunPC;

Local PSM_RUN_PC:RunPC &oRunPC = create PSM_RUN_PC:RunPC();

/* Execute the updated Run method */
try
 &oRunPC.Run();
catch Exception &e
 /* Enable the Save button and disable the Run button */
 PSM_RUN_PC_WK.BUTTON.Enabled = True;
 PSM_RUN_PC_WK.BUTTON1.Enabled = False;
 throw CreateException(0, 0, &e.ToString());
end-try;

/* Enable the Save button and disable the Run button */
PSM_RUN_PC_WK.BUTTON.Enabled = True;
PSM_RUN_PC_WK.BUTTON1.Enabled = False;

While this is a helpful utility, it should be well understood that enabling this type of functionality for users in the PIA could result is some serious security implications.  I would also like to point out that the delivered Application Class Tester utility within Enterprise Components could potentially be leveraged to perform similar functionality as the utility presented in this post.  If you are interested in playing with that utility, then it can be found under Main Menu > Enterprise Components > Component Configurations > Application Class Tester.

Leave a Reply

Your email address will not be published.