Functions

From LongJump Support Wiki
Revision as of 00:41, 3 June 2011 by imported>Aeric

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:


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

Lock-tiny.gif

Users that have the Customize Objects permission can Add, Edit or Delete a Function 

Add a Function

  1. Click Designer > Logic > Functions > New Function
  2. 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
  3. Enter the code into the editing area, then click the [Save] button to save the code

Edit a Function

  1. Click Designer > Logic > Functions
  2. Select the function to edit
  3. 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
  4. 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

  1. Click Designer > Logic > Functions
  2. Click the name of the function to delete
  3. 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.