Catalog client script : how can i get group.name from GlideRecord object ?

Nabil EL FILALI
Tera Expert

Hello 

Please need your help on a catalog client scripting :

 

in fact, the alert below displays the sys_id of group record, actually I need to display just the name of the group and every username related to this group.

 

 

thank you !

 

var sys_user_groupGrMembersGR = new GlideRecord("sys_user_grmember");
sys_user_groupGrMembersGR.query();
while(sys_user_groupGrMembersGR.next())
            {
                alert(sys_user_groupGrMembersGR.group + '   ' + sys_user_groupGrMembersGR.user);
            }
}
6 REPLIES 6

SunilKumar_P
Giga Sage

Hi @Nabil EL FILALI , I am in agreement with @Brad Bowman  that, its not recommended to use the Glide Record in client scripts. Please refer to the @Aniket Chavan Glide Ajax script to get the display value of the reference type fields.

 

Regards,

Sunil

Sandeep Rajput
Tera Patron
Tera Patron

@Nabil EL FILALI GideRecord queries inside the client script is a strict no-no in ServiceNow. Instead as suggested by others, you should use GlideAjax instead.

 

Following changes need to be done to achieve this. First create a client callable script include as follows.

 

Screenshot 2023-12-17 at 11.53.02 AM.png

Here is the script for the script include.

 

 

var MyUtils = Class.create();
MyUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getGroupAndGroupMembers: function() {
        var groupDetailArray = [];
        var grMember = new GlideRecord('sys_user_grmember');
        grMember.query();
        while (grMember.next()) {
            // Build the payload. You can return additional data if needed. 
            var result = {
                "group_name": grMember.group.getDisplayValue(),
                "group_member_name": grMember.user.getDisplayValue()
            };
			groupDetailArray.push(result);
        }


        return JSON.stringify(groupDetailArray);
    },

    type: 'MyUtils'
});

 

 

Here is how your client script should look.

 

// client script – contains onLoad function and a callback function

function onLoad() {
    var ga = new GlideAjax('MyUtils'); // MyUtils is the script include name 
    ga.addParam('sysparm_name', 'getGroupAndGroupMembers'); // getGroupAndGroupMembers is the function in the 
    ga.getXMLAnswer(groupDetailParse);
}

// callback function for returning the result from the script include
function groupDetailParse(response) {
    var groupDetail = response.responseXML.documentElement.getAttribute("answer");
    alert(groupDetail);

}

Hope this helps.