Docs‎ > ‎API Creator‎ > ‎Create your API Project‎ > ‎API Properties‎ > ‎

Logic Libraries

When you write rules using JavaScript, you will often benefit from using pre-existing libraries. Logic libraries are re-usable solutions for patterns. API projects can use any number of libraries, which you can select in API Creator. Select only those libraries that you will use, since there is a cost in CPU and memory associated with each additional library.

After you have added a library to an API project, you can use its facilities in your rules by invoking classes and methods directly from JavaScript.

Add a User Library to an API Project

  1. In your API project, select API Properties, Library, User Library, Create New Library.
    The Logic library  window opens.
  2. Enter the type of library (javascript or a Java JAR file), click Choose File to find your JavaScript/Java library and save your changes.
  3. Enable/activate the library inside this API project by selecting the Used checkbox and then clicking Save.
    The classloader activates and loads the selected library in your API project.

Reload an Updated Library

If you update your library code, you must reload it.
  1. Click Edit for the revised library.
    The Logic Library dialog opens.
  2. Click Choose File, select your altered code, and save your changes.

Use Libraries: JavaScript Wrapper Function Example

In this example of a JavaScript wrapper function, the wrapper can call out to Java or other services:
function sendEmail(){ 
var result = {};
var msg = "error";
var configSetup = {
to  : "to" ,
from : "from",
title : "title",
text: "text"
};
result.configure = function configure(myconfig){
configSetup.to    = myconfig.to || "to";
configSetup.from  = myconfig.from || "from";
configSetup.title = myconfig.title || "title";
configSetup.text  = myconfig.text || "text";
 };
 
 result.send = function send(){
   try{
    //call my mail interface here
msg = "Send email (stub) to: " + configSetup.to + ", from: " + configSetup.from + " body text: "+ configSetup.text; 
} catch(e) {
return e;
}
return msg;
};
return result;
}

Call from an Event Rule

In the Demo API project, the following is a Commit event on the PurchaseOrder object, for the Insert operation:

var mail = sendEmail();
var config = {
to: 'user@gmail.com',
from: 'sales@acme.com',
title: 'This is an email test',
text: 'This is the email text body'
};
mail.configure(config);
log.debug(mail.send());

In the Log event viewer, the message 'Send email (stub) to....' response displays.

For more information about viewing how to call reusable solutions from rules, see Business to Business Example.

Pattern: library initialization

A simpler approach could avoid the configure call. This sample illustrates a useful pattern for initializing libraries.

Use Libraries: moment.js Library Example

In this example, you have a table called Orders, with two attributes: order_date and shipping_date. You would like to have a computed attribute called processing_delay showing the time elapsed between these two dates. You can use the moment.js library or use Java objects.

In this example, the moment.js library is used, as follows:

var orderDate = currentBean.order_date.getTime();
var shippingDate = currentBean.shipping_date.getTime();
currentBean.processing_delay = moment(orderData).from(shippingData, true);

For more information about the moment.js library, see the moment.js site.

Invoke Java Library Classes

Prerequisite: You have added a library to an API project.

You can use the library's facilities in your rules by invoking classes and methods directly from JavaScript. Precede the java package with the keyword Packages. For example, your Java class contains the following:

package bigpackage.smallpackage;

public class MyClass {
  static int add(int a, int b) {
     return a+b;
  }
}

To invoke the Java class from JavaScript, issue the following command:

var myResult = Packages.bigpackage.smallpackage.MyClass.add(1,2);  // invoke from JavaScript