- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-05-2016 10:52 AM
Hi everyone!
I have the followings requirements:
- Add a user to a Group based on specific conditions;
- If the user already exists on the group do not add another record with an update;
- If we have an update on the database and the user that exists on the group fail to match the specific conditions must be removed from the group;
I have created the following Business Rule on the [sys_user] Table:
var rec1 = new GlideRecord('sys_user_grmember');
rec1.initialize();
rec1.user = current.sys_id;
rec1.group.setDisplayValue('Testing');
rec1.insert();
This script only answer to my first need (1), anyone can help me for the 2 and 3 requirements?
Thanks in advance,
Ricardo Rodrigues
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-05-2016 11:25 AM
Thanks for the update.
One more recommendation that please set filter condition in BR to fire only when title column is changed so that this is not fired everytime. Now add one more script block for your 3rd req.
if(current.title != 'IT') //Replace fieldcolumnname with exact column name
{
var rec1 = new GlideRecord('sys_user_grmember');
rec1.addQuery('user', current.sys_id);
rec1.addQuery('group.name', 'Testing');
rec1.query();
if(rec1.next())
{
rec1.deleteRecord();
}
}
else
{
var rec1 = new GlideRecord('sys_user_grmember');
rec1.addQuery('user', current.sys_id);
rec1.addQuery('group.name', 'Testing');
rec1.query();
if(!rec1.next())
{
rec1.initialize();
rec1.user = current.sys_id;
rec1.group.setDisplayValue('Testing');
rec1.insert();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-05-2016 11:26 AM
I understand the requirement now.
You could add this inside the script, so when it runs on every update, the conditions will be checked.
- var rec1 = new GlideRecord('sys_user_grmember');
- rec1.addQuery('user', current.sys_id);
- rec1.query();
- if(!rec1.next())
- {
- rec1.initialize();
- rec1.user = current.sys_id;
- rec1.group.setDisplayValue('Testing');
- rec1.insert();
- }
- else if(current.title != 'IT'){
- rec1.delete();
- }
**Untested code. Might not work
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-05-2016 11:38 AM
All of these fail to take into account the user AND the group in the general query. Should it not be:
- var rec1 = new GlideRecord('sys_user_grmember');
- rec1.addQuery('group.name', 'Testing');
- rec1.addQuery('user', current.sys_id);
- rec1.query();
- if(!rec1.next())

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-05-2016 11:43 AM
Good Catch Mike. Updated code with the changes.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-08-2016 06:37 AM
Hi Ricardo,
Let me know if that answered your question. If so, please mark it as correct so that others with the same question in the future can find it quickly and that it gets removed from the Unanswered list. Thank you
P.S : If you are viewing this discussion from your "inbox", use the "mark as correct" option under actions. If you are viewing it directly from the thread use the Correct Answer link (red with a star).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-13-2018 03:37 PM
I am trying to do this same thing...but not getting code to work. Here is my version of script:
var rec1 = new GlideRecord('sys_user_grmember');
rec1.addQuery('user', current.sys_id);
rec1.addQuery('group.name', "Becks Managers");
rec1.query();
if(!rec1.next())
{
rec1.initialize();
rec1.user = current.sys_id;
rec1.group.setDisplayValue('Becks Managers');
rec1.insert();
}