Using JavaScript in Formula Attributes

"Formula" attribute is a special type that can evaluate a combination of values of other attributes, and can contain mathematical operators and functions. Formula attribute can be as simple as "Total Distance/Fuel" to compute miles per gallon (mpg), as shown above in the "Fuel Records" section of the "Vehicle Logbook", or can be a complex JavaScript code.

NOTE: The JavaScript is evaluated on the server, and does not have access to the HTML DOM (Document Object Model).

For example, in the above "Vehicle Logbook", the value of the field "mpg" is calculated with the following JavaScript:

// get the value of the "Total Distance" attribute for the current row var totalDistance = ROW_ATTR('totalDistance'); // get the value of the "Fuel" attribute for the current row var fuelUsed = ROW_ATTR('fuel'); var currentMpg = (totalDistance/fuelUsed).toFixed(1); // output the value currentMpg;

For the first row, the above expression would expand to:

var totalDistance = 175; var fuelUsed = 8.8; => currentMpg = 19.9

NOTE - each user-defined attribute needs to have a unique identifier that is used in formula expression. Normally, this will be automatically computed from name of the attribute, e.g., the name Total Distance will result in an identifier called totalDistance. However, you may choose to use a different identifier. The identifier needs to be unique within its table.

For example, in the Vehicle Logbook example, the identifier for the attribute Fuel, fuel, is unique to the table Fuel Records. The valid characters for an identifier are the letters a through z, the numbers 0 through 9, the $ (dollar) symbol, and _ (underscore).

The value of the last entry in the formula expression is displayed in the cell. If you want to format the value (e.g., show it in a different color), use the function WRITE().

In addition to referring to simple attributes defined in the same table, formula expression can also refer to attributes that are defined in other tables belonging to the same app. In the Vehicle Logbook example, in the table Fuel Records, the column "My Car" refers to the table "My Car". For a given row, the entry linked to the other table can be gotten by referring to it as:

// get the JSON representation of the car at the current row var vehicleJSON = ROW_LINKED('myCar');

List of available functions

The following JavaScript functions are available for loading and manipulating entries (The sample Vehicle Logbook is used in the examples):

{ "id":"080c8376-8d49-4dd5-a0b6-60b8b75b93a1", "dateCreated":"2013-05-13T01:57:30Z", "lastUpdated":"2013-05-13T01:57:30Z", "date":"2013-02-06", "starred":false, "notes":"", "attr":[ { "identifier":"fuel", "id":"daa7c054-f6ac-45f0-894e-2bbe5c72d62e", "columnType":"DOUBLE", "value":16.5, "name":"Fuel" }, ... ... ], "association":{ "resource":[ { "identifier":"car", "schemaId":"e03f42e1-23ce-102e-9630-327cd4b40910", "id":"e03f42e1-23ce-102e-9630-327cd4b40910", "name":"Honda Pilot" }, ... ] } }

In addition to the above functions, you can also use the popular underscore.js library. You can also use moment.js for working with dates and time.

You can use standard JavaScript objects and functions. JavaScript HTML DOM functions and functions that reference browser objects are not available.

Tip: To see the output of any of the above functions, create a dummy formula attribute and use the standard JavaScript JSON.stringify() method to see the output. For example:

var car = ROW_LINKED('car'); // This will display the representation of car in JSON format. JSON.stringify(car);