The CreatorCon Call for Content is officially open! Get started here.

getting alert "There is a JavaScript error in your browser console" in catalog form

Amit Dey1
Tera Contributor

hi all getting that error on loading  of the form and making changes in the selected variable in catalog onchange client script

 

 

 

Script include

getAppids: function() {
        var appSysids = this.getParameter('sysparm_value');
        var prodID = this.getParameter('sysparm_prod');
        var sysIDS = appSysids.split(',');
        var appid = "";
        var data = [];

        for (var i = 0; i < sysIDS.length; i++) {

            var prodgr = new GlideRecord('u_business_service_attributes');
            var encoded = 'u_owned_application_sLIKE' + sysIDS[i] + '^u_product_id!=' + prodID;
            prodgr.addEncodedQuery(encoded);
            prodgr.query();
            if (prodgr.next()) {
                var appgr = new GlideRecord('cmdb_ci_business_app');
                appgr.addQuery('sys_id', sysIDS[i]);
                appgr.query();
                if (appgr.next()) {
                    if (appgr.install_status == '0' || appgr.install_status == '4') {
						appname = appgr.name;
                        appid = appgr.u_air_attribute.u_application_id.getValue().toString();
                        appowner = appgr.u_air_attribute.u_program_manager.getDisplayValue().toString();
                    } else if (appgr.install_status == '1') {
						appname = appgr.name;
                        appid = appgr.u_air_attribute.u_application_id.getValue().toString();
                        appowner = appgr.u_air_attribute.u_service_operations_manager.getDisplayValue().toString();
                    }

                }
                data = appname + ',' + appid + ',' + appowner;
                return data;
            }
        }
    },




catalog client script



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

    //Type appropriate comment here, and begin script below
    var productID = g_form.getValue('cat_product_id');
    var appids = g_form.getValue('cat_owned_application_s').split(',');
    var ga = new GlideAjax('AIRProductClientUtils');
    ga.addParam('sysparm_name', 'getAppids');
    ga.addParam('sysparm_value', appids);
    ga.addParam('sysparm_prod', productID);
    ga.getXML(selectedAppids);

    function selectedAppids(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        var result = answer.split(',');
        if (result[0] != null) {
            alert('The application "' + result[0] + '"  with AIR ID: "' + result[1] + '" has already been associated with another product. Please contact the application team manager: "' + result[2] + '" for any remapping or choose another application/AIR ID.');
            appids.pop();
            g_form.setValue('cat_owned_application_s', appids.join());
			
        }

    }

}

thank you for your help in advance

3 REPLIES 3

johnfeist
Mega Sage

Hi Amit,

 

All the supported browsers have debugging features, it's just a matter of turning the relevant one(s) on.  When I get that message, it usually just requires going into the console to see what the error is.  Beyond that, if you are still not resolving the issue, I'd say embed debugging statements (g_form.addInfoMessage() in your script so that you can see where your error is.

Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster

hi @johnfeist  I got this error "function Error() { [native code] }" what would be the reason ?

Community Alums
Not applicable

Hi @Amit Dey1 ,

 

I think the error message "function Error() { [native code] }" is a generic error indicating that a JavaScript error has occurred, but it doesn't provide specific details about the error. This typically means that there is an issue in your script that is causing it to fail.

I would recommend to to review the scripts and adding further logs to identify the cause-

The below code should help you in the same-
Before that ensure that the script include is Client callable.

 

Modified Script Include-

var AIRProductClientUtils = Class.create();
AIRProductClientUtils.prototype = {
    initialize: function() {},

    getAppids: function() {
        var appSysids = this.getParameter('sysparm_value');
        var prodID = this.getParameter('sysparm_prod');
        var sysIDS = appSysids ? appSysids.split(',') : [];
        var appname = "";
        var appid = "";
        var appowner = "";
        var data = [];

        for (var i = 0; i < sysIDS.length; i++) {
            var prodgr = new GlideRecord('u_business_service_attributes');
            var encoded = 'u_owned_application_sLIKE' + sysIDS[i] + '^u_product_id!=' + prodID;
            prodgr.addEncodedQuery(encoded);
            prodgr.query();
            if (prodgr.next()) {
                var appgr = new GlideRecord('cmdb_ci_business_app');
                appgr.addQuery('sys_id', sysIDS[i]);
                appgr.query();
                if (appgr.next()) {
                    if (appgr.install_status == '0' || appgr.install_status == '4') {
                        appname = appgr.name;
                        appid = appgr.u_air_attribute.u_application_id + ''; // Ensure this is a string
                        appowner = appgr.u_air_attribute.u_program_manager.getDisplayValue() + ''; // Ensure this is a string
                    } else if (appgr.install_status == '1') {
                        appname = appgr.name;
                        appid = appgr.u_air_attribute.u_application_id + ''; // Ensure this is a string
                        appowner = appgr.u_air_attribute.u_service_operations_manager.getDisplayValue() + ''; // Ensure this is a string
                    }
                }
                data.push(appname + ',' + appid + ',' + appowner);
            }
        }
        return data.join('|'); // Use a delimiter that won't be in the data
    },

    type: 'AIRProductClientUtils'
};

 

Modified Client Script-

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

    try {
        var productID = g_form.getValue('cat_product_id');
        var appids = g_form.getValue('cat_owned_application_s').split(',');
        var ga = new GlideAjax('AIRProductClientUtils');
        ga.addParam('sysparm_name', 'getAppids');
        ga.addParam('sysparm_value', g_form.getValue('cat_owned_application_s')); // Ensure it matches the parameter in Script Include
        ga.addParam('sysparm_prod', productID);
        ga.getXMLAnswer(function(response) {
            try {
                var answer = response.responseXML.documentElement.getAttribute("answer");
                console.log('Response received: ' + answer);
                if (answer) {
                    var entries = answer.split('|');
                    for (var i = 0; i < entries.length; i++) {
                        var result = entries[i].split(',');
                        if (result.length >= 3 && result[0]) {
                            alert('The application "' + result[0] + '" with AIR ID: "' + result[1] + '" has already been associated with another product. Please contact the application team manager: "' + result[2] + '" for any remapping or choose another application/AIR ID.');
                            appids.pop();
                            g_form.setValue('cat_owned_application_s', appids.join(','));
                        } else {
                            console.error('Unexpected response format: ' + entries[i]);
                        }
                    }
                } else {
                    console.error('Empty or null response received.');
                }
            } catch (e) {
                console.error('Error processing the response: ' + e.message);
            }
        });
    } catch (e) {
        console.error('Error in onChange script: ' + e.message);
    }
}

The above code will ensure the error handling and detailed logging of the errors during the script execution.

If you need further loggings please add, like if you want to see the what data is being returned.

 

Note: Ensure that the parameters are correctly passed and retrieved.

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!

 

Thanks & Regards,

Sanjay Kumar