Check whether user is part of a group or not ? & if user is not part of the group then add the user to the group

Karunakar3
Kilo Contributor

Hi All, 

Need your help on below requirement.

Below is the form, once i click on Order Now ; script should check whether selected user is a part of selected group or not ?

find_real_file.png

If user is not part of group then add the user to the selected group(which is selected in the form) and display message saying "User added successfully to the group" else display message saying "User is already part of the group"

 

Thanks in Advance....

 

2 REPLIES 2

Meera11
ServiceNow Employee
ServiceNow Employee

Write ON Submit Catalog Script. Perform below steps

 

var grUser = g_form.getUniqueValue("your_field");

var grGroup = g_form.getUniqueValue("your_group_Value")

 

var ggr = new GlideRecord("sys_user_group");
    ggr.query('sys_id', grGroup);
    while (ggr.next()) {
        groupSysId = ggr.sys_id.toString();
    }

var gmgr = new GlideRecord("sys_user_grmember");

gmgr.addQuery('sys_id,grUser)

gmgr.query();

if(gmgr.next()){

 

alert('User is already part of the group');

}

else{

alert('User added successfully to the group');

}

 

Please Mark Answer Correct , if you found my solution Correct

Vijay Pawar3
Tera Contributor

Its not advisible to use glide record in the client script, moreover it wont work from scoped app.

1) Create a onSubmit Client Script, use glideAjax to call Script include (this has to be a synchronous call), send user and group as paremeters

2) In script include check if user is part of group by querying sys_user_grmember

var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('user', yourUser);
gr.addQuery('group', yourGroup);
gr.query();

if(gr.next()){
return true
}else{
gr.initialize();
gr.user = yuorUser;
gr.group = yuorGroup;
gr.update();
return false
}

3) In client script if true is retuned alert('User already exists'), if false is returned alert('User added successfully') and return false(if you dont want to submit RITM as your job is done.)