Docs‎ > ‎CA Live API Creator‎ > ‎Create‎ > ‎API Properties‎ > ‎

Logic Libraries

When you write rules using JavaScript, you will often benefit from using pre-existing libraries.

Each project can use any number of libraries, which can be selected in the Logic Designer. You should only select those libraries that you will in fact use, since there is a cost in CPU and memory associated with each additional library.

Once you have added a library to a project, you can immediately take advantage of it and start using its facilities in your rules.  Check the [x] Used box and save to allow a selected library to become active and loaded in this API project.


Managing Libraries



Adding a User Library

Use the API Properties/Library/User Library sub tab and..
  1. Click on the CREATE NEW LIBRARY button
  2. The dialog below will prompt for the type of library (javascript or a Java JAR file).  
  3. Click the Choose File button to find your JavaScript / Java library
  4. Once you save, remember to select used[x] and save for the library to become active in this API project.


Remember to check the used [x] checkbox to enable this library inside this API.


Reloading an Updated a Library

If you update your library code, you must reload it:
  1. Click the Edit button for the revised library in the screen shown at the top of the page
    • That presents the Logic Library dialog shown above
  2. Click Choose File, and selected your altered code
  3. Press Save


Using Libraries

Below are some examples of using Libraries.

This is an example of a JavaScript wrapper function. This 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;
}
  

To call from an Event rule:

See this in the Business to Business example.

In the Demo API, the following as 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 you should see the message 'Send email (stub) to....' response.


Pattern: library initialization

In this case, a simpler approach could avoid the configure call.  But this sample illustrates a useful pattern for initializing libraries.

Using the moment.js library example

Let's assume that 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.

The easiest way to do this might be to use the excellent moment.js library:

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

This could also be done with Java objects, of course.