- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2024 08:16 PM
Hi Team,
There is one requirement that whenever incident created caller is added to a group. I have written BR to add caller to group.
Now, I need to remove caller from that group if he doesn't have existing incident in incident table. It must not be based on Active field rather incident state field.
Example: David is caller he has incidents with mixed state , since he has incident with Onhold,in progress,new state he must not be removed from group
Example 2: Admin has one incident that is close, no inprog or new, onhold incs so he must be removed from group.
Example 3: Survey user has 2 incs with one new and other cancelled. He should not also removed from group.
Please help me with deletion part logic
Thank You
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-25-2024 12:27 AM
Hello @preethigovi ,
Please write this fix script-
var groupSysId ='c88e9defc31d92d0d1455ebeb001319d'; // Replace with the sys_id of the group
var groupMembers = new GlideRecord('sys_user_grmember');
// groupMembers.addQuery('group', groupSysId);
groupMembers.addEncodedQuery('group=c88e9defc31d92d0d1455ebeb001319d'); // Query all members of the group
groupMembers.query();
while (groupMembers.next()) {
var userId = groupMembers.user.sys_id.toString();
groupMembers.setWorkflow(false);
gs.info('testing' + userId);
//gs.print('HR USERS' + Hrprofile.sys_id.toString())
var a= new sn_jny.testscriptinclude().test(userId,groupMembers);
if(a==true){
groupMembers.deleteRecord();
}
}
and below script include-
var testscriptinclude = Class.create();
testscriptinclude.prototype = {
initialize: function() {
},
test:function(userId,groupMembers){
var activeIncidentCheck = new GlideRecord('sn_jny_journey');
activeIncidentCheck.addEncodedQuery('manager.user='+ userId +'^state=ready^ORstate=draft');
//activeIncidentCheck.addQuery('manager', Hrprofile.sys_id.toString()); // Check if the user is the caller
activeIncidentCheck.query();
gs.info(activeIncidentCheck.getRowCount());
if (!activeIncidentCheck.next()) {
gs.info('members list');
// groupMembers.deleteRecord();
return true;
}
else{
return false;
}
},
type: 'testscriptinclude'
};
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2024 09:24 PM
Hello ,
You can store the state values which you want to prevent by comma separated. and then in same BR take incident table. query states . n remove those user .
Thanks ,
Pratiksha.k

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2024 10:22 PM
@preethigovi You can create an onBefore update business rule on incident table
(function executeRule(current, previous /*null when async*/) {
// Query for other incidents in "New," "In Progress," or "On Hold" states for the same caller
var incGR = new GlideRecord('incident');
incGR.addQuery('caller_id', current.caller_id);
incGR.addQuery('state', 'IN', '1,2,3'); // Assuming states: 1 = New, 2 = In Progress, 3 = On Hold
incGR.query();
// If there are no active incidents, remove the caller from the group
if (!incGR.hasNext()) {
var userGR = new GlideRecord('sys_user_grmember'); // Group Member Table
userGR.addQuery('user', current.caller_id); // Caller ID
//userGR.addQuery('group', <'group sys_id'>); // Add Group Sys ID
userGR.query();
while (userGR.next()) {
userGR.deleteRecord(); // Removes the caller from each group they belong to
}
}
})(current, previous);
Hope this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-25-2024 12:27 AM
Hello @preethigovi ,
Please write this fix script-
var groupSysId ='c88e9defc31d92d0d1455ebeb001319d'; // Replace with the sys_id of the group
var groupMembers = new GlideRecord('sys_user_grmember');
// groupMembers.addQuery('group', groupSysId);
groupMembers.addEncodedQuery('group=c88e9defc31d92d0d1455ebeb001319d'); // Query all members of the group
groupMembers.query();
while (groupMembers.next()) {
var userId = groupMembers.user.sys_id.toString();
groupMembers.setWorkflow(false);
gs.info('testing' + userId);
//gs.print('HR USERS' + Hrprofile.sys_id.toString())
var a= new sn_jny.testscriptinclude().test(userId,groupMembers);
if(a==true){
groupMembers.deleteRecord();
}
}
and below script include-
var testscriptinclude = Class.create();
testscriptinclude.prototype = {
initialize: function() {
},
test:function(userId,groupMembers){
var activeIncidentCheck = new GlideRecord('sn_jny_journey');
activeIncidentCheck.addEncodedQuery('manager.user='+ userId +'^state=ready^ORstate=draft');
//activeIncidentCheck.addQuery('manager', Hrprofile.sys_id.toString()); // Check if the user is the caller
activeIncidentCheck.query();
gs.info(activeIncidentCheck.getRowCount());
if (!activeIncidentCheck.next()) {
gs.info('members list');
// groupMembers.deleteRecord();
return true;
}
else{
return false;
}
},
type: 'testscriptinclude'
};
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.