Ensure Total Price Calculation Waits for Quantity Input After Reference is Set

HARINIR
Tera Contributor

I am facing an issue where the total price calculation only works if the quantity field is filled before retrieving the reference using g_form.getReference('u_string_3', getDetails);

However, in my requirement, the quantity field comes after the product field, meaning the reference should be fetched first. The total price calculation should wait until the user fills the quantity field. Once the user enters a value in the quantity field, the total price should be calculated correctly and should not display "NaN."

How can I ensure that the total price calculation waits for the user to input the quantity after the reference is set?

 

Code:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    //Type appropriate comment here, and begin script below

    if (!newValue == '') {
        g_form.setSectionDisplay('product_details', true);
        g_form.getReference('u_string_3', getDetails);
    }

}

function getDetails(details) {
    var priceStr = details.u_price_3 ? details.u_price_3.replace(/,/g, '') : "0";
    var price = parseFloat(priceStr) || 0;
    g_form.setValue('u_string_4', details.u_choice_2);
    g_form.setValue('u_price_2', price);
    g_form.setValue('u_string_5', details.u_string_2);
    var quantityStr = g_form.getValue('u_quantity');
    var quantity = parseInt(quantityStr.trim(), 10);
    if (quantity > 0) {
        calculateTotalPrice(quantity, price);
    }

    function calculateTotalPrice(quantity, price) {
        var totalPrice = price * quantity;
        g_form.setValue('u_float_1', totalPrice);
    }
}
0 REPLIES 0