Strange Client Includes Issue

colinlin
Giga Contributor

Hi!  I have a Script Include, see below.  I have a client script on a catalog form where I can type something in a text field and the text will be sent to the script include for querying.  It shows "unhandled exception in GlidesAjax" error. But what's strange is, as you can see when I do gs.log and check the system log, I do see the searchTerm is expected. 

If I hardcode the line below

searchTerm = this.getParameter('sysparm_searchTerm');

to 

searchTerm = "test", it works.  I'm so confused why the getParameter would give an error. I also copied the client script below.

 

Client Includes

 

var GetGrcEntitiesGlobal = Class.create();
GetGrcEntitiesGlobal.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    getEntities: function(searchTerm) {
        if (!searchTerm) {
            searchTerm = this.getParameter('sysparm_searchTerm');
            gs.log('GetGrcEntitiesGlobal called with searchTerm: ' + searchTerm);
        }

        var entities = [];

        if (!searchTerm || searchTerm.length < 3) {
            return global.JSON.stringify(entities);
        }

        var grcEntityGR = new GlideRecord('sn_grc_profile');
        grcEntityGR.addActiveQuery();
        grcEntityGR.addQuery('name', 'CONTAINS', searchTerm); // Filter based on the search term
        grcEntityGR.query();

        while (grcEntityGR.next()) {
            entities.push({
                sys_id: grcEntityGR.getValue('sys_id'),
                name: grcEntityGR.getValue('name')
            });
        }

        var result = global.JSON.stringify(entities);
        return result;
    },

    type: 'GetGrcEntitiesGlobal'
});

 

 

Client Script

 

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

    if (isLoading || newValue.length < 3) {
        return;
    }
    var grcEntityField = g_form.getControl('ci_reference');

    if (grcEntityField) {
        var ga = new GlideAjax('global.GetGrcEntitiesGlobal');
        ga.addParam('sysparm_name', 'getEntities');
        var search_term = newValue.toString();
        ga.addParam('sysparm_searchTerm', search_term);
        ga.getXMLAnswer(function(response) {
            var entities = JSON.parse(response);

            grcEntityField.innerHTML = '';
            g_form.addOption('ci_reference', '', '-- Select a Profile After Search --');

            for (var i = 0; i < entities.length; i++) {
                g_form.addOption('ci_reference', entities[i].sys_id, entities[i].name);
            }
        });
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

3 REPLIES 3

Runjay Patel
Giga Sage

Hi you have not followed glideajx formate

in script include method you can pass as a parameter.

you need to get that value from client scripts using this.getParameter.

 

in Clint script use getXML instead of getXMLAnswer.

check this blog for glideAjax format: https://servicenowwithrunjay.com/glideajax-in-servicenow/

 

Accept the solution if it helped

colinlin
Giga Contributor

I have figured this out! The reason is I had to add a String() to the searchTerm explicitly for some reason, and that worked!  Hope this helps others who might run into this.

colinlin
Giga Contributor

Not sure if my previous reply was sent successfully.  Couldn't see it. So sending again. I have figured this out. The searchTerm has to be wrapped with String explicitly for some reason.  Hope this helps others who might run into similar issues.