Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Javascript error in browser console

Andre241
Tera Contributor

Hello,

 

I have two Catalog Client scripts that are onChange for my record producer. When I go to portal it says "There is a JavaScript error in browser console." Here is my first client script:

 

function onChange(control, oldValue, newValue, isLoading) {

    try {

        if (isLoading || newValue === '') {

            return;

        }

 

        var cleanedValue = newValue.replace(/[^0-9.-]/g, '');

        var regex = /^-?\d+(\.\d{0,2})?$/;

 

        if (!regex.test(cleanedValue)) {

            alert('Please enter a valid number, e.g., 12.00, 344000.00, or -1234.56');

            g_form.setValue(control.name, oldValue);

            return;

        }

 

        var valueFloat = parseFloat(cleanedValue);

        var formattedValue = "$" + valueFloat.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,');  // Format with commas and currency symbol

        g_form.setValue(control.name, formattedValue);

 

        calculateTotalAdjustedAmount();

    } catch (error) {

        console.error('Error in onChange script:', error);

    }

}

 

function calculateTotalAdjustedAmount() {

    try {

        var total = 0;

 

        for (var i = 1; i <= 10; i++) {

            var original = g_form.getValue('original_amount_' + i);

            var adjusted = g_form.getValue('adjusted_amount_' + i);

 

            if (original && adjusted) {

                original = parseFloat(original.replace(/[^0-9.-]/g, '')) || 0;

                adjusted = parseFloat(adjusted.replace(/[^0-9.-]/g, '')) || 0;

 

                total += (original + adjusted); // or total += (original - adjusted) if subtraction is needed

            }

        }

 

        var totalFormatted = "$" + total.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,');  // Format with commas and currency symbol

        g_form.setValue('total_adjusted_amount', totalFormatted);

    } catch (error) {

        console.error('Error in calculateTotalAdjustedAmount function:', error);

    }

}

 

 

My second:

 

function onChange(control, oldValue, newValue, isLoading) {

    try {

        if (isLoading || newValue === '') {

            return;

        }

 

        var cleanedValue = newValue.replace(/[^0-9.-]/g, '');

        var regex = /^-?\d+(\.\d{0,2})?$/;

 

        if (!regex.test(cleanedValue)) {

            alert('Please enter a valid number, e.g., 12.00, 344000.00, or -1234.56');

            g_form.setValue(control.name, oldValue);

            return;

        }

 

        var valueFloat = parseFloat(cleanedValue);

        var formattedValue = "$" + valueFloat.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,');  // Format with commas and currency symbol

        g_form.setValue(control.name, formattedValue);

 

        calculateTotalAdjustedAmount();

    } catch (error) {

        console.error('Error in onChange script:', error);

    }

}

 

function calculateTotalAdjustedAmount() {

    try {

        var total = 0;

 

        for (var i = 1; i <= 10; i++) {

            var original = g_form.getValue('original_amount_' + i);

            var adjusted = g_form.getValue('adjusted_amount_' + i);

 

            if (original && adjusted) {

                original = parseFloat(original.replace(/[^0-9.-]/g, '')) || 0;

                adjusted = parseFloat(adjusted.replace(/[^0-9.-]/g, '')) || 0;

 

                total += (original + adjusted); // or total += (original - adjusted) if subtraction is needed

            }

        }

 

        var totalFormatted = "$" + total.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,');  // Format with commas and currency symbol

        g_form.setValue('total_adjusted_amount', totalFormatted);

    } catch (error) {

        console.error('Error in calculateTotalAdjustedAmount function:', error);

    }

}

 

Can someone please help me figure out what's wrong?

0 REPLIES 0