Script for Calculating LTV
I'm trying to create a script to calculate the LTV based on several factors. If the TransactionType contains the word "Purchase" then the calculation is the InitialAdvance divided by the minimum of the PurchasePrice and AsIsValue. If the TransactionType contains the word "Refinance" then the calculation is the InitialAdvance divided by the AsIsValue. The ChatGPT script given hasn't worked property for some reason as listed below, hopefully someone can direct me the right way:
(function() {
// Get the values from the form fields
var initialAdvance = parseFloat(this.getField("InitialAdvance").value);
var purchasePrice = parseFloat(this.getField("PurchasePrice").value);
var asIsValue = parseFloat(this.getField("AsIsValue").value);
var transactionType = this.getField("TransactionType").value;
// Variable to store the LTV calculation result
var ltv = 0;
// Check the transaction type and perform calculations
if (transactionType.includes("Purchase")) {
// Calculate LTV using the minimum of PurchasePrice and AsIsValue
var minValue = Math.min(purchasePrice, asIsValue);
if (minValue !== 0) {
ltv = initialAdvance / minValue;
}
} else if (transactionType.includes("Refinance")) {
// Calculate LTV using AsIsValue
if (asIsValue !== 0) {
ltv = initialAdvance / asIsValue;
}
}
// Set the calculated LTV value with appropriate formatting
event.value = ltv.toFixed(2); // adjust the decimal places as needed
})();
