Writing Java code for a function
After you create a function and define its input and output parameters, you write the Java code that defines what the function does. You enter the Java code on the Java tab of the Function rule form. You can specify classes to import and exceptions that might be thrown on the Imports & Exceptions tab.
- Open or create a function.
- On the Java tab, in the Java source
field, enter the Java code to invoke when the function is called.
Note the following:
- You can access classes in the java.util and java.lang packages, the classes that are defined in the Pega Engine API, and the classes that are imported into your library. To access other classes, enter the class names in the Imports & Exceptions tab.
- For information about the Pega Engine API, access the Javadoc documentation from the Dev Studio header by clicking Resources > Engine API.
- Your Java code must be Java 7 compliant.
- You can write inner classes, but you cannot write child methods. Inner classes can contain methods.
- 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. - You can obtain a reference to the PublicAPI interface by calling
ThreadContainer.get().getPublicAPI();
(ThreadContainer.get()
can sometimes return null.) - You can call a function in a different library by specifying the generated java
package of the library. For example, to return a value from the getDataSystemSetting
function in the Utilities library, type
return com.pegarules.generated.pega_rules_utilities.getDataSystemSetting(owningRuleSet,setting);
- When Pega Platform is running in the enterprise tier of a Java EE application server, applications cannot directly read from or write to the server file system. To minimize maintenance issues with custom Java code, do not use native java.io routines. Instead, use the PRFile class (part of the Engine API), which provides similar methods with the same names.
- Click Save.
- Select the Function ready to be compiled? check box when you are ready.
-
Click
Test function compilation
to compile your function. If errors are displayed, fix the errors, and repeat this step. Repeatedly save and test until there are no errors.
- If a class "cannot be resolved to a type" error occurs, enter the fully qualified class name on the Imports & Exceptions tab, in the Packages imported field.
- If an "unhandled exception type" error occurs, enter the fully qualified exception class name on the Imports & Exceptions tab, in the Exceptions thrown field.
- Click Generate function to generate a local version of the function and its library.
- Test your function. For information on calling a function, see Methods for calling a function.
- When you finish testing, click Generate library to deploy the library to your active node and cluster.
Previous: Creating a function |