Calculation fields are powerful for form customization and used heavily by many Fulcrum users. Since their launch last year, we’ve seen adoption take off and customers are now using them in thousands of different ways.
Calculation expression examples
If you haven’t seen it before, our developers site has in-depth documentation on the various expressions available within the Fulcrum calculation field builder. In addition to this, we’ve created a library of examples that show common use cases for calculation expressions, with code snippets you can copy and paste to learn and use within your own Fulcrum apps.
Let’s take a look at a handful of the useful examples from our library:
Calculate the amount of elapsed time between two dates
If your survey has two separate dates, like a “Permit Expiration Date” and “Today’s Date”, this expression takes two date values and returns the amount of time elapsed between the two:
var date1 = new Date($date_field1);
var date2 = new Date($date_field2);
var timeDiff = Math.abs(date2.getTime() – date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));SETRESULT(diffDays); // to show the difference in days
Populate the day of the week from a date field
This quick formula takes a date field value as input and returns the day of the week:
// define an array of the days of the week to use as a lookup structure
var daysOfWeek = [
‘Sunday’,
‘Monday’,
‘Tuesday’,
‘Wednesday’,
‘Thursday’,
‘Friday’,
‘Saturday’// This converts a date field in the record to a JavaScript date object
var d = DATEVALUE($the_date_field);// If you wanted to use today’s date or any other specific date …
// var d = new Date();
// var d = new Date(‘4/15/1984’);SETRESULT(daysOfWeek[d.getDay()]);
Sum of numbers within child records
This one shows how you can total up values across repeatable (child) record values. An example if where this would be useful is if your repeatable section represents “materials required”, where each item has a dollar value (like on a bill of materials), you could sum up the total value of all items:
REPEATABLESUM($repeatable_field, ‘cost’) // where ‘cost’ is the data name of an item’s total
Generate a star rating from a numeric field between 1 and 5
Your first dose of egregious symbol / emoji usage in a calculation field, this sample shows how to swap boring numeric scoring into cooler-looking star ratings:
Array(FLOOR($rating + 1)).join(‘★’) + Array(FLOOR(6 – $rating)).join(‘☆’)
Totaling scores across child records
And here, if we have child items that each have a “score” field, with a picklist of values like “N/A, 0, 1, 2, 3”, etc. This formula will look through all of the items and sum up the total across all of the subrecords:
var array = REPEATABLEVALUES($name_of_repeatable, ‘data_name_of_choicefield_score’);
var total_score = 0;
for (var i=0; i < array.length; i++){
var value = array[i][‘choice_values’];
var score = 0;
if (value == ‘N/A’){
score = 0
} else {
score = NUM(array[i][‘choice_values’]);
}total_score += score
}
SETRESULT(total_score);
Naturally the possibilities for how data-driven expressions can be used for data capture are limitless — especially when chaining the result of one expression into another. Our developer docs are open source on GitHub, so anyone can contribute their own expression examples if you’ve got something you find useful.