This example shows how to provide two calculated fields on an Add or Edit screen layout. This could be achieved by using business rules, but this examples serves the purpose of showing alternative methods of achieving similar results. The key difference is that business rules are executed via an Ajax call back to the server, while the JavaScript approach downloads the code to the client browser where it is executed.
The example involves two fields named VAL1 and VAL2. Numeric data entered into these two fields will be summed and the result placed in a third field named VAL_SUM, and the two fields will be multiplied and the result placed in a fourth field named VAL_PRODUCT. All four fields have a display type of Number. To set up this requirement, take the following steps –
function valOf(which) { if (! which) return 0; var val = parseInt(which, 10); if (isNaN(val)) return 0; return val; } function calc() { document.editForm.p_val_product.value = document.editForm.p_val1.value * document.editForm.p_val2.value; var a = document.editForm.p_val1.valueOf; document.editForm.p_val_sum.value = valOf(document.editForm.p_val1.value) + valOf(document.editForm.p_val2.value); return; }