- 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:15 AM
Sorry Veena, I didn't get you. Can you please repeat.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-05-2016 11:29 AM
Oops. I think I understood the requirement wrong and I had a question from that. I got it now. thanks anyway
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-05-2016 11:01 AM
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();
}
This would satisfy your 2nd requirement. For the 3rd, i think we need to add one more condition in the if(). something like, rec1==current. I'm not sure if that works that way.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-05-2016 11:19 AM
About my third requirement:
1 - The condition to enter in the group(Testing) is to have "IT" in the column "Title"(on sys_user table).
2 - I run the script and my user(Ricardo) enter on the group because he has IT on the column "Title";
3 - Now the same user(Ricardo) has been updated and now he doesn't have IT on the column "Title, so we should be deleted from the group "Testing".
Do you understand? Thank you.

- 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();
}
}