There is a JavaScript error in your browser console - error

v-paulp
Tera Contributor

Hi all ,

There is two reference type variable in catalog which is referring  to same table . and based on value selected to the first variable it's should show values to second variable. I have written below client script and script include but it is showing "There is a JavaScript error in your browser console  "this error message.

client script

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }
    if (oldValue !== newValue) {
        g_form.clearValue('variable_two');
        var ga = new GlideAjax('getfilteroptions');
        ga.addParam('sysparm_name', 'getoptions');
        ga.addParam('sysparm_variable1', newValue);
        ga.getXMLAnswer(updatevalues);
    }

    function updatevalues(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        g_form.setReferenceQual('variable_two', answer);
        g_form.addInfoMessage("Reference Qual set: " + answer);
    }
}

script inlcude

var getfilteroptions = Class.create();
getfilteroptions.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getoptions: function() {
        var variable1 = this.getParameter('sysparm_variable1');
        var arr = ['China (KH) Kangui Plant', 'Mexico (ELTJ) El Lago Plant', 'Mexico (NLTJ) Nellcor Plant', 'Dominican Republic (SIMA) San Isidro Plant', 'Boulder', 'China (CZM) Plant', 'China (KH) Kangui Plant', 'Connecticut (NH) North Haven Plant'];
        var arr1 = [];

        function removeValue(arr, valueToRemove) {
            for (var i = arr.length - 1; i >= 0; i--) {
                if (arr[i] === valueToRemove) {
                    arr.splice(i, 1);
                }
            }
            return arr;
        }

        var req = new GlideRecord('u_service_catalog_filter_variables');
        req.addQuery('sys_id', variable1);
        req.query();
        if (req.next()) {
            var value = req.getDisplayValue('u_name');
        }
        var gr = new GlideRecord('u_service_catalog_filter_variables');
        gr.addEncodedQuery('u_active=true^u_catalog_item=fbaaec411b2989506ed99603b24bcbd2^u_variable_number=2');
        gr.query();
        while (gr.next()) {
            arr1.push(gr.sys_id + '');
            var updatedArray;
            if (arr.includes(value)) {
                updatedArray = removeValue(arr1, 'd5160d7697968e142ac576d6f053af30');
            } else {
                updatedArray = arr1;
            }

        }
        return 'sys_idIN' + updatedArray.join(',');

    },

    type: 'getfilteroptions'

});

please let me know where I am making mistake

 

5 REPLIES 5

ChallaR
Giga Guru

Hi @v-paulp ,

please find the corrected script -

Client script -

 

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

    g_form.clearValue('variable_two');

    var ga = new GlideAjax('getfilteroptions');
    ga.addParam('sysparm_name', 'getoptions');
    ga.addParam('sysparm_variable1', newValue);
    ga.getXMLAnswer(function(response) {
        var answer = response;
        if (answer) {
            g_form.setReferenceQual('variable_two', answer);
            g_form.addInfoMessage("Reference Qual set: " + answer);
        } else {
            g_form.addErrorMessage("Failed to get filter options.");
        }
    });
}

Corrected Script Include

var getfilteroptions = Class.create();
getfilteroptions.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getoptions: function() {
        var variable1 = this.getParameter('sysparm_variable1');
        var arr = ['China (KH) Kangui Plant', 'Mexico (ELTJ) El Lago Plant', 'Mexico (NLTJ) Nellcor Plant', 'Dominican Republic (SIMA) San Isidro Plant', 'Boulder', 'China (CZM) Plant', 'China (KH) Kangui Plant', 'Connecticut (NH) North Haven Plant'];
        var arr1 = [];

        function removeValue(arr, valueToRemove) {
            for (var i = arr.length - 1; i >= 0; i--) {
                if (arr[i] === valueToRemove) {
                    arr.splice(i, 1);
                }
            }
            return arr;
        }

        var req = new GlideRecord('u_service_catalog_filter_variables');
        req.addQuery('sys_id', variable1);
        req.query();
        var value = '';
        if (req.next()) {
            value = req.getDisplayValue('u_name');
        }

        var gr = new GlideRecord('u_service_catalog_filter_variables');
        gr.addEncodedQuery('u_active=true^u_catalog_item=fbaaec411b2989506ed99603b24bcbd2^u_variable_number=2');
        gr.query();
        while (gr.next()) {
            arr1.push(gr.sys_id.toString());
        }

        var updatedArray = (arr.includes(value)) ? removeValue(arr1, 'd5160d7697968e142ac576d6f053af30') : arr1;

        return 'sys_idIN' + updatedArray.join(',');
    },

    type: 'getfilteroptions'
});

 

NOTE -

Key Fixes

  • Replace > with >=.
  • Ensure updatedArray is defined outside the loop.
  • Validate answer before using it.
  • Use sys_idIN format for reference qualifier.

please mark as correct and close the thread if this is helpful

Thanks,

Rithika.ch