Useful scripts
Blog: Flokzu
In the process form you can define a script field (one of the functions available under calculated field) to write your own validation scripts and calculations in Flokzu.
This post puts together some useful scripts and explains how they work.
Performing arithmetic operations
Since the scripting engine is JavaScript, some adjustments need to be done to perform certain operations. Given two fields A and B, you can add them up using the JavaScript function ‘parseInt’ like this:
parseInt( {{A}} ) + parseInt( {{B}} )
Thus we make sure that the engine will interpret the value of fields A and B as numbers.
Limit number of decimal places
When working with decimal numbers, the result may contain an infinite number of decimal places. To avoid this, we can modify the function and limit the number of decimal places to a certain number (in this case 3):
( {{A}} * {{B}} ).toFixed(3)
Difference in days between two dates
Given two date fields ‘Date A’ and ‘Date B’, the difference in days is solved by this script:
Math.ceil( Math.abs(new Date( {{Date A}} ).getTime() – new Date( {{Date B}} ).getTime()) / (1000 * 3600 * 24));
No matter which date is greater, the difference in days between the values entered will always be correctly calculated.
Showing error messages
In some cases it is necessary to do some type of validation and if it is not fulfilled, to show an error message to the user. This is possible using a script.
Example: Compare if two fields A and B are equal and if they are not, display an error message.
if( {{Field A}} != {{Field B}}) { alert(‘Values don’t match!’); }
Using JavaScript
The type of validations that can be done using a script field is limited. Logical functions such as if, for, case, or variables cannot be used.
A solution to overcoming this limitation is to allow JavaScript to be used. For that you have to start the script with 0; (zero followed by a semicolon) and use the variable newValue to display what you want in that field.
For example:
0;
var CONS = 10;
if ( {{Combo}} == ‘TEST’ ){ newValue = 20 + CONS; }else{ newValue = 30 + CONS; }
If the value of Combo is TEST, the field will display the value 30, otherwise 40.
The post Useful scripts appeared first on Flokzu.
Leave a Comment
You must be logged in to post a comment.