• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Script for Calculating LTV

New Here ,
Nov 17, 2024 Nov 17, 2024

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

})();

TOPICS
JavaScript

Views

304

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 17, 2024 Nov 17, 2024

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 18, 2024 Nov 18, 2024

Copy link to clipboard

Copied

Thanks for the reply; I tried that and unfortunately, that didn't fix the error.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 18, 2024 Nov 18, 2024

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 18, 2024 Nov 18, 2024

Copy link to clipboard

Copied

See attached with the script that's returning no result in the LTV field.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 18, 2024 Nov 18, 2024

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 19, 2024 Nov 19, 2024

Copy link to clipboard

Copied

Got it, changed that gield name to match. What "other issues with scripts in the file?"

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 19, 2024 Nov 19, 2024

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 19, 2024 Nov 19, 2024

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 19, 2024 Nov 19, 2024

Copy link to clipboard

Copied

LATEST

Are there any errors now? Can you share the new version of the file?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines