Adding Users to Group via the API

Raymond Setchfi
Kilo Explorer

Hi 

 

I am looking for some assistance if someone could assist. I can create the user which is all fine and well using the API, but when researching to add that user to a group. It appears that I need to look at using GlideRecord. 

I had a look into this further to see how its done etc through the communities, but it appears that this is to setup business rules, or something along those lines instead of actually adding it through the api. 

Can someone please advise a complete novice, on how to add a user to a group using the API? Also how this would look like within Postman?

Thanks 

6 REPLIES 6

ggg
Giga Guru

here is the code that will do it:

var user_sysid = '62826bf03710200044e0bfc8bcbe5df1';
var group_sysid = 'b85d44954a3623120004689b2d5dd60a';
var gr = new GlideRecord('sys_user_grmember');
gr.initialize();
gr.setValue('user', user_sysid);
gr.setValue('group', group_sysid);
gr.insert();

variations on this depend upon how you are adding the user to the group?

through a job? another record?

 

joel_ruiz1
Tera Expert

You'll want to use a POST pointed at the sys_user_grmember table.

Asbj_rn
Mega Expert

 

Hi, 

 

user group relationships are stored in a table named sys_user_grmember.

you could simply do this with the table API. given that you know the sys ids of the user and the group. 

 

find_real_file.png

To do this with by group/user names require a scripted rest api (preferably). Other wise you will need to have your business logic regarding searching and returning the proper accounts/groups stored on the consuming side of the integration, which is less than ideal.

 

Raymond Setchfi
Kilo Explorer

Thank you for the response in this

 

@Asbjørn, yes. I am looking to avoid attempting to do this through business rules. I'd rather allow the script to do the creation of the user and then the adding into a particular group. 

I tried what you have suggested on the screenshot. It appears that I am receiving a bad request when doing so. 

{
    "error": {
        "message": "Exception while reading request",
        "detail": "Cannot decode: java.io.StringReader@6a0562"
    },
    "status": "failure"
}

 

Is there something which I am missing?