You are here: Reference > Activity methods > Java in activity steps

  Using Java in an activity step

Methods and instructions by function

Most activities can be defined using only methods and instructions. Some activities require inline Java.

Reviewing generated Java

Activities execute as server-side Java, so it is technically simple to include custom Java statements within your activities. However, experience with designing, coding, and testing Java is required.

Whenever you Save an activity, the system validates Java code from information in the form and in other rule instances. This generated Java is read-only. To review the generated Java code, select Actions > View Java.

The window provides a preview of the Java that implements this rule instance. The Java code presented is similar to but not identical to the Java that executes at runtime, which includes Java code inlined from other rule instances and reflects rules in the requestor's RuleSet list.

The first time a requestor session selects this activity for execution, the rules assembly occurs again, generating and compiling Java that is specific to the RuleSet list and other context of that requestor. The next execution of the same execution by that requestor, or other requestors with identical access, does not require assembly or compilation.

The Java instruction

To have PRPC execute your custom Java within the generated Java for an activity, add inline Java into an activity step. (These are known as Java steps.)

Inline Java steps can introduce complex, difficult-to-debug processing into your application. Use this facility only when necessary. Following the SmartBuild guardrails, avoid Java steps when other approaches can achieve the same computational result. Activities containing a Java step are flagged with a warning message when you save the Activity form, and when you run the Guardrails tool.

  1. Type Java in the Method field of the step.
  2. Click to expand the Method field. A text box appears in which you can enter normal Java code and PRPC-specific Java.

Your Java code can use two sources of PRPC-specific Java:

In addition, your Java can call methods and functions in external libraries, if all these conditions are true:

See PDN article About the Process Commander class paths. Consider Connect JAVA rules or Connect EJB rules as alternatives to Java steps.

Because the Java you write in an activity step is inline Java, you cannot import your Java libraries or add Java import statements. However, you can refer to Java classes using a fully qualified class name. For example:

PRPC cannot accept:

Vector x = new Vector();

However, you can write:

java.util.Vector x = new java.util.Vector();

Use the String.equals() Java method, not the == or != operators, to compare the value of a property to a literal string or to the value of another property.

Use the Loop button to identify the iteration of the Steps array. See Activity form - Completing the Steps tab - Entering loops.

Calling PublicAPI Java methods

For a complete list of available PublicAPI Java methods, consult the Public API JavaDocs. By convention, the tools variable identifies the PublicAPI class.

For example, the two Java methods getProperty() and getParameter() get and set property values on a page:

In many cases, your Java code needs to reference variables defined in the step's generated Java. For example, the generated Java often uses the variable myStepPage to refer to the current step page. (The step page must be present on the clipboard before the Java step starts.)

 

Calling function rules

In addition to the normal Java libraries, your Java step can call functions in the Rule-Utility-Function class. This class contains functions (Rule-Utility-Function rule type), organized into libraries (Rule-Utility-Library rule type).

Each function rule belongs to a RuleSet. To call a function rule from a Java step, refer to each library by its RuleSet and library name. For example, a standard library named String is defined in the Pega-RULES RuleSet. To reference this library in your Java, use this syntax:

pega_rules_string.

After the dot, enter the name of the String library function using exact case, and any required parameters:

pega_rules_string.equalsIgnoreCase ("ABC123DEF", pyWorkPage.Account);

Reserved variable names

Certain variables have assigned purposes and are reserved. See Activity rules — Reserved names for parameters and local variables.

Java EE enterprise support

Advanced feature TipWhen executing in the enterprise tier of a Java EE application server, applications cannot directly read from or write to the server file system. In such situations, you cannot use native java.io routines. Instead, use the Virtual File Interface (part of the Public API) provides similar methods. In Web tier installations, the Virtual File Interface methods merely call the corresponding native Java methods.

Checking for security issues

As a best practice for activities that contain hand-crafted Java, run the Rule Security Analyzer to check for code that may expose your application to security vulnerabilities.

Checking status

Technically, the text Java in the Method column does not reference a PRPC method. But like methods, the Java instruction updates the pxMethodStatus property. See How to test method results using a transition.

Definitions Java EE, Virtual File Interface
Related topics About Function rules
About Library rules

How to set up a Java development environment

PRPC for Java developers — Concepts and terms
Examples — Inline Java and PublicAPI

UpMethods and instructions by function