Difference between revisions of "Functions"

From LongJump Support Wiki
imported>Aeric
imported>Aeric
Line 1: Line 1:
'''Designer > Logic > Functions'''
'''Designer > Logic > Functions'''


{{:Function}}
{{Deprecated|Functions have been deprecated. Existing functions can be edited, but it is no longer possible to create them. Instead of writing functions, the recommended practice is to write methods in Java [[Classes]], in order to gain the benefits of an improved [[Development Experience]].}}
 
A ''function'' is reusable Java code that can be called from:
:* Standard Java classes, to create custom business logic.
:* Data policies, to do sophisticated processing.
:* JSP pages, to enhance the operation of the page.


==Writing Functions==
==Writing Functions==

Revision as of 01:16, 4 June 2011

Designer > Logic > Functions

Warn.png

DEPRECATED: Functions have been deprecated. Existing functions can be edited, but it is no longer possible to create them. Instead of writing functions, the recommended practice is to write methods in Java Classes, in order to gain the benefits of an improved Development Experience.


A function is reusable Java code that can be called from:

  • Standard Java classes, to create custom business logic.
  • Data policies, to do sophisticated processing.
  • JSP pages, to enhance the operation of the page.

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 Edit or delete an existing Function 

Edit an Existing 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 an Existing 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