Condition in Business Rules
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-05-2024 04:24 AM
Hi,
I have a scenario where if I check the SD checkbox, it should add the current user to Service Desk group, and when unchecked, it should remove him from the group. The code that I used is working fine. But there is a problem. Once the user from the current record is added to the Service Desk group, it should not be added again, similarly, once the current user is removed, it should not attempt to be removed again. So, there should be a condition that needs to be added, but I can't figure it out. Kindly help.
(function executeRule(current, previous /*null when async*/ ) {
if (current.u_sd == true) {
var gr = new GlideRecord('sys_user_grmember');
gr.initialize();
gr.group = 'd625dccec0a8016700a222a0f7900d06';
gr.user = current.sys_id;
gr.insert();
gs.addInfoMessage(current.name + ' is added to the Service Desk group');
} else if (current.u_sd == false) {
var gr1 = new GlideRecord('sys_user_grmember');
gr1.addQuery('group', 'd625dccec0a8016700a222a0f7900d06');
gr1.addQuery('user', current.sys_id);
gr1.query();
if (gr1.next()) {
gr1.deleteRecord();
gs.addInfoMessage(current.name + ' is deleted from Service Desk group');
}
}
})(current, previous);
Regards
Suman P.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-05-2024 04:29 AM - edited ‎12-05-2024 04:30 AM
Hi @Community Alums
I believe you can use some field or value to the specific user as a flag, and when you query the user use this flag to guarantee that one will not be selectable anymore.
I hope to be useful!
^^
Leandro Bezerra
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-05-2024 04:53 AM
Hello,
Check the below and update your script,
if (current.u_sd == true && validateGrpMember(current.sys_id) == 'false') {
var gr = new GlideRecord('sys_user_grmember');
gr.initialize();
gr.group = 'd625dccec0a8016700a222a0f7900d06';
gr.user = current.sys_id;
gr.insert();
gs.addInfoMessage(current.name + ' is added to the Service Desk group');
} else if (current.u_sd == false && validateGrpMember(current.sys_id) == 'true') {
var gr1 = new GlideRecord('sys_user_grmember');
gr1.addQuery('group', 'd625dccec0a8016700a222a0f7900d06');
gr1.addQuery('user', current.sys_id);
gr1.query();
if (gr1.next()) {
gr1.deleteRecord();
gs.addInfoMessage(current.name + ' is deleted from Service Desk group');
}
}
function validateGrpMember(user) {
var grGrpMem = new GlideRecord('sys_user_grmember');
grGrpMem.addQuery('group', 'd625dccec0a8016700a222a0f7900d06');
grGrpMem.addQuery('user', user);
grGrpMem.query();
if (grGrpMem.hasNext()) {
return 'true';
} else {
return 'false';
}
}
If my answer helped you in any way, please then mark it as helpful or correct. This will help others finding a solution.
Kindly mark it correct and helpful if it is applicable.
Your feedback not only supports me, but also helps others in the community who may have similar questions!
Regards,
Abdul Fathah