API of Remove User From Group
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-03-2022 05:10 AM
Hello Guys,
Anyone Knows the API of Servicenow Remove a user from group.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-03-2022 05:25 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-03-2022 05:27 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-04-2022 06:09 AM
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:
(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>