Get a first look at what's coming. The Developer Passport Australia Release Preview kicks off March 12. Dive in! 

Question on Script include

ashutoshpat
Tera Contributor

I wrote a script include in Employee center core and a client script in the same application for a record producer.

There I have a requirement to check if Requested for is a member of HR groups or not.

I created a field and marking it based on the script include and client script



Client script 

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

if (isLoading)
return;

// If requested_for is cleared
if (!newValue) {
g_form.setValue('requested_for_is_member_of_hrops', 'false');
return;
}

var hrOpsGroup = 'de66fff31b263614454e0ed3604bcbfb';

var ga = new GlideAjax('CheckGroupMembership');
ga.addParam('sysparm_name', 'isUserInGroup');
ga.addParam('sysparm_user', newValue);
ga.addParam('sysparm_group', hrOpsGroup);

ga.getXMLAnswer(function(answer) {

if (answer === 'true') {
g_form.setValue('requested_for_is_member_of_hrops', 'true');
} else {
g_form.setValue('requested_for_is_member_of_hrops', 'false');
}

// Optional debug
alert('HR Ops Member: ' + answer);
});
}

Script include:


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

isUserInGroup: function() {

var userId = this.getParameter('sysparm_user');
var groupId = this.getParameter('sysparm_group');

if (!userId || !groupId)
return 'false';

var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('user', userId);
gr.addQuery('group', groupId);
gr.query();

return gr.hasNext().toString();
}

});

ANy suggestions what I am missing?

it works but also gives an error of  "ErrorThere is a JavaScript error in your browser console"

Any help is highly appreciated.


Thank yoU!
14 REPLIES 14

Hello @Ashutosh4 ,

 

As I can see in your error screenshot there is "Unhandled exception in glide Ajax" :

Screenshot 2026-03-01 175928.png

So I would recommend, Instead of returning just a string, wrap your values in an object, JSON.stringify() it in the Script Include, and JSON.parse() it in the client script. That way you avoid unhandled exceptions and can easily extend your GlideAjax responses with more data later.

Plz refer this : 

Unhandled exception in GlideAjax 

If my response helped mark as helpful and accept the solution.

Gaurav Shirsat
Mega Sage

Hello

Please Make Sure Below Points are correct:

1) Script Include is Client Callable i.e Glide AJAX enabled is checked : Reason is - it Extends the OOB AbstrractAjaxProcessor Script Include. If it isn't True, Glide Ajax Call will always be False

2) Script Include Should be in Global OR Same Scope ideally

3) I doubt alert will help here : use console.log('HR Ops Member:', answer);

Try the below Code Which I have Written. I have not tested, it as I don't keep same set up as you in PDI 

Catalog Client Script :

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    if (!newValue) {
        g_form.setValue('requested_for_is_member_of_hrops', 'false');
        return;
    }

    var hrOpsGroup = 'de66fff31b263614454e0ed3604bcbfb';

    var ga = new GlideAjax('CheckGroupMembership');
    ga.addParam('sysparm_name', 'isUserInGroup');
    ga.addParam('sysparm_user', newValue);
    ga.addParam('sysparm_group', hrOpsGroup);

    ga.getXMLAnswer(function(answer) {

        var isMember = (answer === 'true');

        g_form.setValue('requested_for_is_member_of_hrops', isMember ? 'true' : 'false');

        // check your alert
        console.log('HR Ops Member:', answer);
    });
}
====================================================================
Script Include :
var CheckGroupMembership = Class.create();
CheckGroupMembership.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

    isUserInGroup: function() {

        var userId = this.getParameter('sysparm_user');
        var groupId = this.getParameter('sysparm_group');

        if (!userId || !groupId)
            return 'false';

        var gr = new GlideRecord('sys_user_grmember');
        gr.addQuery('user', userId);
        gr.addQuery('group', groupId);
        gr.query();

        return gr.hasNext() ? 'true' : 'false';
    }

});
 
Thanks and Regards
Gaurav ShirsatBackground Script-1.pngBackground Script-2.png

Ashutosh4
Tera Expert

Tried, still throws the error

 

Gaurav Shirsat
Mega Sage

Along with my above answer, you can refer my sample attached code and rebuild your work.

Ashutosh4
Tera Expert

Guys there was an issue with how the script include was being called.

Many Thanks!