- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-13-2023 06:03 AM
Hi everyone,
i have wrote a business rule. And it should always assign the assigment group of the current logged in user where it starts with "SN-CC" but its still take the first group he found from the user as a assignment group.
Where is my error? Thanks for help.
after insert br:
(function executeRule(current, previous /*null when async*/) {
var isCCMember = false;
var ccGroup = null;
var uID = gs.getUserID();
var groupArr = [];
// Get all active groups where the user is a member
var grmember = new GlideRecord('sys_user_grmember');
grmember.addQuery('user', uID);
grmember.addQuery('group.active', true);
grmember.query();
while (grmember.next()) {
// Push the group sys_id and name into the group array
var groupInfo = {
sys_id: grmember.group.toString(),
name: grmember.group.name.toString()
};
groupArr.push(groupInfo);
//gs.info("Group Info: " + JSON.stringify(groupInfo));
}
for (var i = 0; i < groupArr.length; i++) {
if (groupArr[i].name.indexOf('SN-CC') !== -1) {
isCCMember = true;
ccGroup = groupArr[i].sys_id; // Use sys_id instead of the name
// gs.addInfoMessage("assignedGroups+ I am" + groupArr[i].name);
}
}
// Determine the assigned group based on conditions
var assignedGroup = isCCMember ? ccGroup : (groupArr.length > 0 ? groupArr[0].sys_id : null);
//gs.addInfoMessage("Assigned Group: " + assignedGroup);
// Make sure 'current' is referring to the correct record
var currentRecord = new GlideRecord('sn_customerservice_case'); // Replace 'your_table_name' with the actual table name
if (currentRecord.get(current.sys_id)) {
currentRecord.assignment_group = assignedGroup;
currentRecord.update();
// gs.addInfoMessage("Assignment Group set to: " + assignedGroup + currentRecord.assignment_group);
} else {
gs.addErrorMessage("Unable to find the current record.");
}
// Set the assigned group to the current record
//current.assignment_group = assignedGroup;
})(current, previous);
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-13-2023 06:16 AM - edited 12-13-2023 06:28 AM
Are you getting expected values in the logs throughout the script, or which one is not correct? Get rid of the last GlideRecord - you don't need or want to query for the record that is getting created. This BR should run before insert since you are affecting the same record that is getting created. This way you won't need another update in the script. If your assigned group log is incorrect, I would first off not assign null to any variable value, rather an empty value ''. If this log is incorrect the if statement is not being evaluated correctly, so try changing isCCMember = 'false' then assigning 'true' if the name matches, then use isCCMember = 'true'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-13-2023 06:41 AM
you should use before insert BR
Try this script
var gr = new GlideRecord("sys_user_grmember");
gr.addQuery("user", gs.getUserID());
gr.addEncodedQuery("group.nameLIKESN-CC");
gr.setLimit(1);
gr.query();
if (gr.next()) {
current.assignment_group = gr.getValue('group');
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-13-2023 06:16 AM - edited 12-13-2023 06:28 AM
Are you getting expected values in the logs throughout the script, or which one is not correct? Get rid of the last GlideRecord - you don't need or want to query for the record that is getting created. This BR should run before insert since you are affecting the same record that is getting created. This way you won't need another update in the script. If your assigned group log is incorrect, I would first off not assign null to any variable value, rather an empty value ''. If this log is incorrect the if statement is not being evaluated correctly, so try changing isCCMember = 'false' then assigning 'true' if the name matches, then use isCCMember = 'true'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-13-2023 06:28 AM
Hi,
There are couple things you can correct.
1.Create before business rule against case table
2.Use the same script logic to populate the current assignment group of "case table " (You don't need to glide case table again in this case ).
Regards,
Luxo
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-13-2023 06:29 AM
Hi @JohnDF
I guess the following line of code is the problem as a dot-walk to a referenced record is not possible
name: grmember.group.name.toString()
Try instead:
name: grmember.group.getRefRecord().getValue('name')
Maik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-13-2023 06:41 AM
you should use before insert BR
Try this script
var gr = new GlideRecord("sys_user_grmember");
gr.addQuery("user", gs.getUserID());
gr.addEncodedQuery("group.nameLIKESN-CC");
gr.setLimit(1);
gr.query();
if (gr.next()) {
current.assignment_group = gr.getValue('group');
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader