how to add users to group using script

SAI ESWAR1
Kilo Contributor

like I created a form with two fields user and group when I select a particular user in user field and particular group in group field by clicking on submit button. The user should be added to that group 

 

1 ACCEPTED SOLUTION

rajneeshbaranwa
Giga Guru

You can use below script:

 

var gr = new GlideRecord('sys_user_grmember');

gr.addQuery('group',{{current.variables.group}});

gr.initialize();

gr.user = {{current.variables.user}};

gr.insert();

 

Best practice is to use this code in script include and invoke this script include from client script.

 

 

View solution in original post

4 REPLIES 4

Chaitanya Redd5
Tera Guru

Hi,

Not sure that what form you are referring to in your question. But yes you can achieve this via scripting. The relationship of the user and group are defined in "sys_user_grmember" table. You can create a record and establish the user and group relationship. Deletion of the same record will remove the user from group.

Kindly mark my answer as Correct and helpful based on the Impact.

Regards,

Chaitanya

rajneeshbaranwa
Giga Guru

You can use below script:

 

var gr = new GlideRecord('sys_user_grmember');

gr.addQuery('group',{{current.variables.group}});

gr.initialize();

gr.user = {{current.variables.user}};

gr.insert();

 

Best practice is to use this code in script include and invoke this script include from client script.

 

 

Yes we need server side scripting, Just a bit of modification in your script:

var gr = new GlideRecord('sys_user_grmember');
gr.initialize();
gr.group=current.variables.variable_name_group;//field name or the object where group sys_id is stored on the form
gr.user = current.variables.variable_name_user;//field name or the object where user sys_id is stored on the form.
gr.insert();

querying with group doesn't make any sense, since you are already inserting a new record by initializing a new record on 'sys_user_grmember'. So why not put all the details directly on form when you initialize.

Also, not sure is it a catalog item? If yes, It can be done via run script activity in the workflow itself and script include would not be required.

Aswartha
Mega Contributor

Hi sai,

fix this code in workflow(Run script) Activity.

 

var gr = new GlideRecord("sys_user_grmember");

gr.initialize();

gr.group = current.variables.group; 

gr.user = current.variables.user;

gr.insert();

 

Thank you