Client Scripts
Calculate Field Values Dynamically
Automatically calculate field values based on other fields (e.g., calculate total cost, days between dates, percentage).
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
// Exit if form is loading
if (isLoading) {
return;
}
// Example 1: Calculate total cost (quantity × unit price)
var quantity = parseFloat(g_form.getValue('u_quantity')) || 0;
var unitPrice = parseFloat(g_form.getValue('u_unit_price')) || 0;
var totalCost = quantity * unitPrice;
// Set the calculated total with proper formatting
g_form.setValue('u_total_cost', totalCost.toFixed(2));
// Optional: Show currency formatted value
if (totalCost > 0) {
var formattedTotal = '$' + totalCost.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,');
g_form.showFieldMsg('u_total_cost', 'Total: ' + formattedTotal, 'info');
}
// Example 2: Calculate percentage
// Uncomment to enable
/*
var completed = parseFloat(g_form.getValue('u_completed_items')) || 0;
var total = parseFloat(g_form.getValue('u_total_items')) || 1; // Avoid division by zero
var percentage = (completed / total) * 100;
g_form.setValue('u_completion_percentage', percentage.toFixed(1));
*/
// Example 3: Calculate days between dates
// Uncomment to enable
/*
var startDate = g_form.getValue('u_start_date');
var endDate = g_form.getValue('u_end_date');
if (startDate && endDate) {
var start = new Date(startDate);
var end = new Date(endDate);
// Calculate difference in days
var diffTime = Math.abs(end - start);
var diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
g_form.setValue('u_duration_days', diffDays);
g_form.showFieldMsg('u_duration_days', diffDays + ' days between dates', 'info');
}
*/
// Example 4: Calculate discount and final price
// Uncomment to enable
/*
var originalPrice = parseFloat(g_form.getValue('u_original_price')) || 0;
var discountPercent = parseFloat(g_form.getValue('u_discount_percent')) || 0;
var discountAmount = originalPrice * (discountPercent / 100);
var finalPrice = originalPrice - discountAmount;
g_form.setValue('u_discount_amount', discountAmount.toFixed(2));
g_form.setValue('u_final_price', finalPrice.toFixed(2));
*/
// Example 5: Calculate average
// Uncomment to enable
/*
var value1 = parseFloat(g_form.getValue('u_value_1')) || 0;
var value2 = parseFloat(g_form.getValue('u_value_2')) || 0;
var value3 = parseFloat(g_form.getValue('u_value_3')) || 0;
var average = (value1 + value2 + value3) / 3;
g_form.setValue('u_average', average.toFixed(2));
*/
// Example 6: Calculate age from date of birth
// Uncomment to enable
/*
var dob = g_form.getValue('u_date_of_birth');
if (dob) {
var birthDate = new Date(dob);
var today = new Date();
var age = today.getFullYear() - birthDate.getFullYear();
var monthDiff = today.getMonth() - birthDate.getMonth();
if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
g_form.setValue('u_age', age);
}
*/
}How to use it
1. Create onChange Client Scripts for relevant fields (quantity, price, dates, etc.) 2. Customize the calculation logic for your use case 3. Update field names to match your table structure 4. Uncomment additional calculation examples as needed 5. Consider adding the same script to onLoad to calculate values when form opens 6. Test with various numeric values and edge cases (zero, negative, decimals)
Adapt the table names, fields, and conditions to your instance. Test the behavior in a development environment before using it in production.