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.

Download the App Designer to 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.

Comments

Kunal Gaurav

Hey Colton,

Appreciate your effort in creating this utility.I tried importing the file attached by you in your blog , however its not working for me.Request you to provide code for the PSM_RUN_PC application package.

Colton Fischer

I am not sure why this is not working for you. Below is the code for the RunPC Class in the PSM_RUN_PC Application Package. Please let me know if you still have issues.

class RunPC method Run(); end-class;

method Run

end-method;

Filipe

Hello Colton,

First, you have great posts about %metadata, congrats.

I’m trying to simulate the “Validate Syntax” function from PeopleCode editor using %metadata, but without success until now.

The result more closer that i got was with the below command: &bResult = &defn.UpdateProgram(&appClassText, &strErr, &int1, &int2);

Do you have some information about this?

Thanks!

Colton

Yes, the UpdateProgram method can be used to simulate the App Designer “Validate Syntax” function. The Boolean result from this method should tell you if the PeopleCode syntax that you are trying to save to the PeopleCode program is valid or not.

The first parameter to this method is the PeopleCode program String that you want to save (validate in your case) to the PeopleCode program definition. The second, third, and fourth parameters are output variables as types String, Integer, Integer respectively. For these output variable parameters, you simply pass blank variables to the method and they will be populated with values after the call to the method is made if the PeopleCode syntax is invalid. The second parameter will hold the error text, while the third and fourth will hold the start and end position of where the error occurred in the passed in PeopleCode String.

Check out this part of the Online PeopleCode Editor video demo to see the “Validate Syntax” functionality in action. You can download and install the Online PeopleCode Editor project from GitHub if you are interested in seeing other neat features of the %Metadata API in action.

Leave a comment

Your email address will not be published. Required fields are marked *

Loading...