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.

API to get all the groups using the user_name

SN Rookie
Giga Expert

Hi Team,

I require a generic REST API, which when used should provide me with all the groups when I pass the user_id.

How can I achieve that?

2 REPLIES 2

R_B_2
Tera Contributor

Without going into specific details - my approach would be: 

1. Create script include for the API functionality. (query sys_user_grmember table). Here I would implement functionality of retrieving group data.

Example for group names would be:

getUserGroupNames: function(userId){
    var response = [];

    var gr = new GlideRecord('sys_user_grmember');
        gr.addQuery('user', userId);
        gr.query();
    
    while(gr.next()){
        response.push(gr.getDiplayValue('group')); 
    }

    return response;
}

2. Create scripted REST API where you call script include for each endpoint. : https://docs.servicenow.com/bundle/kingston-application-development/page/integrate/custom-web-servic...

Kumar35
Tera Expert

this should give the group list you are looking for

 

GET https://servicenowinstance/api/now/table/sys_user_grmember?sysparm_query=user.user_name%3DUserid