API of Remove User From Group

Shravan Kurup
Kilo Contributor

Hello Guys,

Anyone Knows the API of Servicenow Remove a user from group.

3 REPLIES 3

Jaspal Singh
Mega Patron
Mega Patron

Did you try using REST API. Navigate to System Web Services >> REST >> Rest API explorer.

find_real_file.png

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Who would consume the API and what they would send?

It would depend on that which type of API would be best for you

Regards
Ankur

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

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi,

Group can be removed using table API on "sys_user_grmember" table. API is  "https://<instance name>.service-now.com/api/now/table/sys_user_grmember"

This will require sys_id of the record in the sys_user_grmember table. To delete specifying user id and group name, create a Scripted REST API.

Example:

find_real_file.png

find_real_file.png

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
    var queryParams = request.queryParams;
    var userId = queryParams.userid.toString();
    var groupName = queryParams.groupname.toString();
    if (userId.length < 1 || groupName.length < 1) {
        response.setError(new sn_ws_err.NotFoundError('userid and groupName must be specified.')); // error
    } else {
        try {
            var grUserGroup = new GlideRecord('sys_user_grmember');
            grUserGroup.addActiveQuery();
            grUserGroup.addQuery('user.user_id', userId);
            grUserGroup.addQuery('group.name', groupName);
            grUserGroup.query();
            if (grUserGroup.next()) {
                if (grUserGroup.deleteRecord()) {
                    return { // delete record
                        "message": 'OK'
                    };
                } else {
                    response.setError(new sn_ws_err.ServiceError('unable to delete specified user/group.')); // error - record not found
                }

            } else {
                response.setError(new sn_ws_err.NotFoundError('specified user/group was not found.')); // error - record not found
            }
        } catch (e) {
            var internalError = new sn_ws_err.ServiceError();
            internalError.setStatus(500);
            internalError.setMessage("Internal error");
            internalError.setDetail(e.message);
            response.setError(internalError);
        }
    }
})(request, response);

The Scripted REST API can then be called using curl command.

curl -u <userid to login to servicenow>:<password> -X DELETE https://<instance name>.service-now.com/api/<namespace>/userrole?userid=<user id>&groupname=<group name>

https://docs.servicenow.com/bundle/rome-application-development/page/integrate/custom-web-services/c...