Manage Functions

You can create endpoints to execute your JavaScript in  and return a JSON response. These endpoints are referred to as functions.
lac31
You can create endpoints to execute your JavaScript in
CA Live API Creator
 and return a JSON response. These endpoints are referred to as functions.
Create a Function
Functions that are not associated to tables or resources are considered top-level endpoints.
  1.  With your API open, in the Create section, click
    Functions
    .
    The By function tab displays by default.
  2. Click 
    Create Function
    .
    The Definition tab for the new function displays.
  3. Enter a name for your function (or accept the default name), the function's JavaScript code, and then save your changes. Give the function a meaningful name. You call functions using this name in the URL. 
  4. (Optional) On the Parameters tab, complete the following and then click 
    Add Parameter 
    to add additional parameters and click 
    Save
     to save the parameters:
    • Define the parameter names and the type (number, string, or boolean).
    • Specify whether the Parameter is required.
    • Include a comment to describe the parameter.
  5. (Optional) On the Resources tab, associate this function to one or more tables or resources. Enter each table (with database prefix) or resource in a new line. Associate allows the function to access the specific row object inside the function. For example:
    // table function "getPromotionDays" for finance:promotion
    var start = new Date(row.begin_date);
    var end = new Date(row.end_date);
    var days = (end - start) / 1000 / 60 / 60 / 24;
    return {result: 'Number of promotion days: ' + days};
  6. On the Documentation tab, describe the purpose of this function by defining comments and the return type expected from the function. The comments and return type are included in your Swagger API documentation.
Your function is created and is active.
Access the Parameters Variable
You can access the parameters that are passed in to your function using the 
parameters
 variable. You can use the parameters within the function logic, for example:
var x = parameters.parm1;
Call Functions
You can call functions in the following ways:
Call Functions as Endpoints
Within your function, you have access to the request (
req
) object, JavaScript libraries, and any Java JAR files included in your 
classpath
. Your function returns a JavaScript object, which is converted to JSON or translated if you are using XML output.
Call a function as an endpoint using the following syntax:
 v1/<function name> or v1/<table name>/<pkey>/<function name>
For example:
log.debug("calling myFunction");
var parms = {};
var settings = { "headers": {"Authorization" : "CALiveAPICreator demo_full:1"}};
var url = req.baseUrl + 'v1/myFunction';
log.debug(url);
var resp = SysUtility.restGet(url, parms, settings);
log.debug(resp);
Call Functions with Parameters
You can call functions using the function name with parameters using the following syntax:
<function name>?<parameter 1>=<value>&<parameter 2>=<value>
For example:
log.debug("calling function add");
var parms = {parm1: 1, parm2: 2};
var settings = { "headers": {"Authorization" : "CALiveAPICreator demo_full:1"}};
var url = req.baseUrl + 'v1/add';
log.debug(url);
var resp = SysUtility.restGet(url, parms, settings);
log.debug(resp);
Call Functions using the SysUtility.getFunction Method
You can call top-level functions using the
SysUtility.getFunction
method, for example:
var parms = {parm1: 1, parm2: 2};
var resp = SysUtility.getFunction("myfunction", parms);
Call Functions on Tables or Resources
You can call functions on tables or resources and pass arguments (as illustrated in this B2B example):
http://localhost:8080/rest/default/b2bderbynw/v1/nw:Employees/1/
sendMail?title="Title from URL ARG"
When you associate functions to tables or resources, the function can access the current 
row
 object.
You can access the current 
row
 object by associating functions with tables or normal resources. You cannot update the 
row
 object using functions.
Call Functions from Rules
You can call functions from rules. The following example is from the Demo API. This example calls the
getDiscPct
function by modifying the formula rule for 
demo:LineItem
 table:
// Discounted price*qty
log.debug("calling function getDiscPct");
// derive discount amount based on qty ordered 
var parms = {"qty": row.qty_ordered};
var settings = { "headers": {"Authorization" : "CALiveAPICreator demo_full:1"}};
var url = req.baseUrl + 'v1/demo:LineItem/' + row.lineitem_id + '/getDiscPct';
var discPct = SysUtility.restGet(url, parms, settings);
log.debug(discPct);
return row.product_price * row.qty_ordered * discPct;
Call JavaScript Libraries from a Function
For more information about how to call JavaScript libraries from a function, see the Business to Business Example
Function Permissions
As an admin, you can secure functions by setting the permissions at the role-level. By default, roles have permission to execute all functions. You can also set permissions to specific functions for a given role. If your auth token has more than one role with differing permissions to a function, the execute permission takes precedence.
For more information about how to grant permissions to roles to execute all functions and controlling a role's permission to execute specific functions, see Role-Based Endpoint Access.