Scenario 3: EMI Form
- Last UpdatedJun 25, 2024
- 2 minute read
EMI (Equated Monthly Installment) form scenario shows how you can use scripts to automatically add line items on a Base Form container on data change event.
Requirements
An EMI form which:
-
captures information about principal, rate, and tenure.
-
automatically calculates the EMI amount, interest amount, principal reduction, and balance due using the EMI formula and displays it as a line item for each installment number.
EMI Formula
EMI = (P*r) (1+r)^n / (1+r)^n - 1
where:
P = Principal (amount of loan).
r = Rate of interest per installment period.
n = Number of installments in the tenure.

Design
-
Create a form.
-
Add three Number controls on the form to capture principal, rate, and tenure.
-
Add a Base Form container on the form and set the Display Mode to Grid, to create line items.
-
Add five Text controls on the Base Form container to display the installment number, EMI amount, interest amount, principal reduction, and balance due, as a line item.

Scripts
Code the script as follows for the On Data Change property of the Tenure control to automatically calculate the EMI and display the line items:
var tenure =control.findByXmlNode("Tenure").value;
var interest = control.findByXmlNode("Rate").value;
var principal =control.findByXmlNode("Principal").value;
if (principal === 0 || interest === 0 || tenure === 0)
{
return;
}
var grid=control.findByXmlNode("Details");
var principal1= parseFloat(principal);
var interest1 = parseFloat(interest );
var tenure1 = parseFloat(tenure );
var ipm =parseFloat(parseFloat(interest1) / parseFloat("1200.0"));
var EMId =((principal1 * ipm) * parseFloat(Math.pow(parseFloat(ipm + 1), tenure1))) / parseFloat(parseFloat((Math.pow(parseFloat(ipm+1), tenure1))) - 1);
var EMI =Math.round(EMId);
var count=grid.records().length;
for (i=count; i>0; i--)
{
grid.removeAt(i);
}
for (j=0; j<tenure; j++)
{
var newRow=grid.addRecordToBottom();
newRow.findByXmlNode("InstallmentNumber").value = j+1;
if (j===0)
{
newRow.findByXmlNode("PrincipalReduction").value = Math.round((EMI-(principal/100)));
newRow.findByXmlNode("EMIAmount").value =EMI;
newRow.findByXmlNode("InterestAmount").value = Math.round((principal)/100);
newRow.findByXmlNode("BalanceDue").value = Math.round(principal - (EMI-(principal/100)));
}
else
{
var newprincipal=grid.records()[j-1].findByXmlNode("BalanceDue").value;
newRow.findByXmlNode("EMIAmount").value =EMI;
newRow.findByXmlNode("PrincipalReduction").value =Math.round((EMI-(newprincipal/100)));
newRow.findByXmlNode("InterestAmount").value =Math.round((newprincipal)/100);
newRow.findByXmlNode("BalanceDue").value = Math.round(newprincipal - (EMI-(newprincipal/100)));
}
}