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.

Unhandled exception in GlideAjax

Anish9515
Tera Contributor
function onChange(control, oldValue, newValue, isLoading) {
    // Check if loading or newValue is empty
    if (isLoading || newValue === '') {
        return;
    }

    // Create GlideAjax instance
    var ga = new GlideAjax('BISwCatDecomUtilAJAX');
    ga.addParam('sysparm_name', 'multilinetextversion');
    ga.addParam('sysparm_sys_id', newValue);

    // Use getXML method instead of getXMLAnswer
    ga.getXML(handleGlideAjaxResponse);
}

// Callback function to handle the response
function handleGlideAjaxResponse(response) {
   
        var answer = response.responseXML.documentElement.getAttribute("answer");

        alert("Suresh is " + answer);

        // Use meaningful variable names
        var multilineTextVersion = answer;
        alert("The input is " + multilineTextVersion);

        var parts = multilineTextVersion.split(/\),/);

        if (parts.length <= 1) {
        g_form.setDisplay('sw_cat_item_removed', true);
        g_form.setReadOnly('sw_cat_item_removed', true);
        } else {
            // If there are more than one part
        g_form.setDisplay('sw_cat_item_removed', false);
        }
    }
 
multilinetextversion: function() {
        var Productversion = [];
        var biproduct = [];

        var software = this.getParameter('sysparm_sys_id');
        var gr = new GlideRecord('u_biproductgroupingproduct');
        gr.addQuery('sys_id', software);
        gr.query();
        while (gr.next()) {

            var gr1 = new GlideRecord('u_biproduct');
            gr1.addQuery('sys_id', gr.u_product_id);
            gr1.query();
            if (gr1.next()) {
                biproduct.push(gr1.sys_id.toString());

                var gr2 = new GlideRecord('u_biproductversion');
                gr2.addQuery('u_product_id', 'IN', biproduct.toString());
                gr2.addEncodedQuery('u_product_version_status=Active');
                gr2.query();
                while (gr2.next()) {
                    Productversion.push(gr2.u_product_version_name.toString());
                }
            }
        }
        return Productversion.toString();
    },
 
Can anyone help me how to fix on this i got Unhandled exception in GlideAjax
Actually code is working fine i got java script browser console error
8 REPLIES 8

Tai Vu
Kilo Patron
Kilo Patron

Hi @Anish9515 

In your Client Script at this line.

var parts = multilineTextVersion.split(/\),/);

It should be like below, isn't it?

var parts = multilineTextVersion.split(",");

 

Cheers,

Tai Vu

Anish9515
Tera Contributor

My ouput in parts is Zip 21.x (Device, EN, Win),7-Zip 22.x (Device, EN, Win),7-Zip 16 (Device, EN, Win),7-Zip 18 (Device, EN, Win),7-Zip 15 (Device, EN, Win),7-Zip 19 (Device, EN, Win),7-Zip 23.x (Device, EN, Win)

 

I wanna separate Zip 21.x (Device, EN, Win)

7-Zip 22.x (Device, EN, Win) 
Like this 
In my scenario it doesn't work 

var parts = multilineTextVersion.split(",");

Amit Gujarathi
Giga Sage
Giga Sage

HI @Anish9515 ,
I trust you are doing great.
You can refer to below updated code for the same.

Client-Side Script

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

    var glideAjax = new GlideAjax('BISwCatDecomUtilAJAX');
    glideAjax.addParam('sysparm_name', 'getProductVersions');
    glideAjax.addParam('sysparm_sys_id', newValue);
    glideAjax.getXML(handleResponse);
}

function handleResponse(response) {
    try {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (!answer) throw new Error("No response received from server");

        var productVersions = answer.split(/\),/);
        var display = productVersions.length > 1;

        g_form.setDisplay('sw_cat_item_removed', !display);
        g_form.setReadOnly('sw_cat_item_removed', !display);
    } catch (error) {
        console.error("Error in processing GlideAjax response:", error);
        // Handle the error appropriately
    }
}

 

Server-Side Script (BISwCatDecomUtilAJAX Script Include)

var BISwCatDecomUtilAJAX = Class.create();
BISwCatDecomUtilAJAX.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getProductVersions: function() {
        var softwareId = this.getParameter('sysparm_sys_id');
        var productVersions = this.fetchProductVersions(softwareId);
        return productVersions.join(',');
    },

    fetchProductVersions: function(softwareId) {
        var versions = [];
        var productQuery = new GlideRecord('u_biproductgroupingproduct');

        productQuery.addQuery('sys_id', softwareId);
        productQuery.query();

        while (productQuery.next()) {
            var versionQuery = new GlideRecord('u_biproductversion');
            versionQuery.addQuery('u_product_id', productQuery.u_product_id);
            versionQuery.addEncodedQuery('u_product_version_status=Active');
            versionQuery.query();

            while (versionQuery.next()) {
                versions.push(versionQuery.u_product_version_name.toString());
            }
        }

        return versions;
    },

    type: 'BISwCatDecomUtilAJAX'
});

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



 If I select a software product field that has only one version, it works fine, and if I select a software product field that has many versions, it works fine as well but i got java script console error i.e Error in processing GlideAjax response:

 

It's cause whenever software product keep changes in form level before submit javascript console error occurs