How to get all group names from sys_user_grmember table through client scripts.

Navya8
Kilo Contributor

Hello,

I have tried getting group names from the sys_user_grmember table by querying through email of user.

However, I am getting sysids of groups of a user. 

I have used the following client script.

Thanks in advance 

Navya

..................Client Script.....................................................................

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

var groups = [];

var gr = new GlideRecord('sys_user_grmember');


gr.addQuery('user.email', g_form.getValue('email'));
gr.query();


while (gr.next())
{
groups.push(gr.group);
}
alert(groups);

1 ACCEPTED SOLUTION

Hi,

please use while(gr.next()) instead of if(gr.next())

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you cannot dot walk 1 more level to get group nam

So please use GlideAjax and Script include as GlideRecord is not good practice in client script

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Script below

Script Include: It should be client callable

var checkRecords = Class.create();
checkRecords.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    
    checkRecordPresent: function(){

        var email = this.getParameter('sysparm_userEmail');            
        var arr = [];
        var gr = new GlideRecord('sys_user_grmember');
        gr.addQuery('user.email', email);
        gr.query();
        if(gr.next()){
            arr.push(gr.group.name.toString());
        }
        return arr.toString();
    },
    
    type: 'checkRecords'
});

Client Script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
	
var ga = new GlideAjax('checkRecords');
    ga.addParam('sysparm_name', "checkRecordPresent");
    ga.addParam('sysparm_userEmail', g_form.getValue('email')); 
    ga.getXMLAnswer(function(answer){
        if(answer != ''){
            alert('Groups'+answer);
        }
    });
    //Type appropriate comment here, and begin script below

}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hello Ankur,

Everything is working fine but only one group is getting as alert.

It is not returning whole array of groups. Getting only one group.

 

Regards

Navya

Navya8
Kilo Contributor

Hi Ankur,

When if() was replaced with while() in script includes, I am getting all groups of user.

 

Thank you,

Navya