Difference between revisions of "Functions"
imported>Aeric m (Text replace - 'Setup > Develop > ' to 'Designer > Logic > ') |
imported>Aeric |
||
Line 1: | Line 1: | ||
'''Designer > Logic > Functions''' | |||
{{:Function}} | {{:Function}} | ||
Revision as of 00:39, 3 June 2011
Designer > Logic > Functions Function
Writing Functions
Functions work exactly like methods in Java code, and are written as if coding a method body in a Java class. You can consider functions as your own personal library of Java code.
Functions are written as if you are writing the body of a method. The platform injects the code you write into a Java class, adds a method signature, compiles it, and stores the bytecode.
The following Java API classes are implicitly imported into functions:
Accessing Parameters
The Java API functionParams object contains key-value pairs for the parameters passed to the current function:
- When making a exec call, it is possible to specify a Parameters object as an argument
Within a called function, access to the parameters is through the functionParams object. This example gets a parameter named number that was passed to the current function:
String accountNumber = functionParams.get("number");
Restrictions
The Java code in functions follows the same restrictions as in classes. A complete list of restrictions is available in the Governors section.
Code Samples
See Function Code Samples for example code that can be modified and used to create
Calling a Function
Functions are invoked via exec, and take arguments passed in functionParams Objects.
Functions can be invoked from:
- exec calls when you Invoke a Java Method From a Data Policy
- exec call from Java Classes
- From JSP Pages:
- Using exec calls from embedded Java code
Calling a Function from a Data Policy
This example calls a function named calculate_estimated_profit which takes in account’s revenue and calculates the profit as 30% of the revenue.
float revenue = params.getFloat("revenue", 0.0); Parameters params = Functions.getParametersInstance(); params.add("revenue", revenue); float profit = Functions.exec("calculate_estimated_profit", params);
Calling a Function from a JSP Page
This example calls a someFunction from Java code embedded in a JSP page.
<% String result = someFunction(); %>
Calling a Function from a Class
This example shows calling a function named add_task from a class.
import com.platform.api.*; import java.util.*; public class ExecAddTask implements Controller { public ControllerResponse execute(HashMap params) throws Exception { String action = (String)params.get("action"); if(action == null || action.equals("")) { return null; } else { return addTasks(params); } } private ControllerResponse addTasks(HashMap params) throws Exception { ControllerResponse cr = new ControllerResponse(); Result result = null; try { Parameters addOptions = Functions.getParametersInstance(); addOptions.add("object_id", "CASE"); String objectID = (String)params.get("objectID"); String recordID = (String)params.get("recordID"); if (objectID!= null && !objectID.equals("") && recordID!= null && !recordID.equals("")) { addOptions.add("object_id", objectID); addOptions.add("record_id", recordID); } Functions.exec("add_task", addOptions); Functions.debug("Message:" + result.getMessage()); params.remove("action"); cr.setData(result); cr.setTargetPage("ManageCases.jsp"); } catch(Exception e) { cr.setTargetPage("ManageCases.jsp"); cr.setMessage(e.getMessage()); Functions.debug("Message:" + result.getMessage()); } return cr; } }
Manage Functions
Users that have the Customize Objects permission can Add, Edit or Delete a Function
Add a Function
- Click Designer > Logic > Functions > New Function
- Complete the following required information
- Title
- Name of the Function
- Function Name
- Programmatic name of the Function; Only alphanumeric characters and underscore allowed; In this example, enter: add_task
- Description
- Description of the function, how it works, circumstances for use
- Function Button
- Optionally, add a function from a list of predefined functions, defined in Java API
- Enter the code into the editing area, then click the [Save] button to save the code
Edit a Function
- Click Designer > Logic > Functions
- Select the function to edit
- Edit the following fields:
- Title
- Name of the Function
- Function Name
- Programmatic name of the Function; Only alphanumeric characters and underscore allowed; In this example, enter: add_task
- Description
- Description of the function, how it works, circumstances for use
- Function Button
- Optionally, add a function from a list of predefined functions, defined in Java API
- Edit the code in the editing area, then click the [Save] button to save the code or click [Cancel] to stop the process
Delete a Function
- Click Designer > Logic > Functions
- Click the name of the function to delete
- Click the [Delete] button or click [Cancel] to stop the process
Editing Functions in Eclipse
Functions can also be added, edited, and deleted using the Eclipse Plug-In.