Copy link to clipboard
Copied
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
})();
Copy link to clipboard
Copied
The problem is this line, most likely:
if (transactionType.includes("Purchase")) {
Try this, instead:
if (transactionType.indexOf("Purchase")!=-1) {
Same with the other instance of includes.
Copy link to clipboard
Copied
Thanks for the reply; I tried that and unfortunately, that didn't fix the error.
Copy link to clipboard
Copied
Are there any errors in the JS Console when you use the script? Can you share the file with us?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
In the script you used the field name "TransactionType" but in your file it has a space "Transaction Type".
There are other issues with scripts in the file.
Copy link to clipboard
Copied
Got it, changed that gield name to match. What "other issues with scripts in the file?"
Copy link to clipboard
Copied
In LTARV field instead of SFN use custom script, or you will constantly get error, because field will try to divide with zero when "AsRepairedValue" is empty.
Copy link to clipboard
Copied
Got it, fixed as well. Unfortunately the issue with the LTV script is still not generating a result. Not sure what else I need to revise for the main issue of the post.
Copy link to clipboard
Copied
Are there any errors now? Can you share the new version of the file?