- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-18-2024 06:03 AM
Hello Experts,
I have written the following script to restrict data for the "Assignment Group" field based on the "Assigned To" field. If the "Assigned To" is Fred Luddy, then I want to show all assignments belonging to Fred Luddy. I am calling my script include using an advanced reference qualifier. When I manually pass the sys_id of the user through a background script, I am getting the correct result without any issues. Please guide me on why it's not working.
Script Include:
var BackfillAssignmentGroup_Using_Array = Class.create();
BackfillAssignmentGroup_Using_Array.prototype = {
initialize: function() {
},
getGroups:function(user)
{
if(!user)
{
return;
}
var group=new GlideRecord('sys_user_grmember');
group.addQuery('user',user);
group.query();
var group_Array=[];
while(group.next())
{
group_Array.push(group.getValue('sys_id'));
}
return "sys_idIN"+group_Array.toString();
},
type: ' BackfillAssignmentGroup_Using_Array'
};
Advance Reference Qualifier for Assignment group field.
javascript:new BackfillAssignmentGroup_Using_Array().getGroups(current.assigned_to)
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-18-2024 10:42 AM
Hello @Mark Wood ,
Thank you very much, and I genuinely appreciate your kind words!🤗 & I've implemented your latest suggestions and made the necessary changes. Now, you have two approaches to choose from:
Approach 1: Using Reference Qualifier Only
- Please find the below reference qualifier code:
javascript: if(current.assigned_to !="")'sys_idIN' + new global.ArrayUtil().convertArray(gs.getUser().getUserByID(current.assigned_to.toString()).getMyGroups()); else'active=true';
- Note: While it's often mentioned that using if-else conditions in reference qualifiers might not be considered best practice, I've personally found it effective in many cases. Additionally, I came across an article that also supports this approach. So, feel free to utilize it as needed.
Approach 2: Using Script Include and Reference Qualifier
- Alternatively, if you prefer to avoid if-else conditions in reference qualifiers, you can utilize a script include. I've created a script include named AssignmentGroupReferenceQualifier with the following code:
var AssignmentGroupReferenceQualifier = Class.create();
AssignmentGroupReferenceQualifier.prototype = {
initialize: function() {
},
getAssignmentGroups: function(assignedTo) {
if (!assignedTo || assignedTo.nil()) {
// If Assigned To is empty, return all assignment groups
return 'active=true';
} else {
// If Assigned To is not empty, return groups based on the selected user
return 'sys_idIN' + new global.ArrayUtil().convertArray(gs.getUser().getUserByID(assignedTo.toString()).getMyGroups());
}
},
type: 'AssignmentGroupReferenceQualifier'
};
- To use this script include in your reference qualifier, use the following code:
javascript: new AssignmentGroupReferenceQualifier().getAssignmentGroups(current.assigned_to)
Please let me know if you have any further doubts or concerns so that we can address them accordingly. If everything is clear,
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks & Regards,
Aniket.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-18-2024 07:03 AM
Hi @Mark Wood ,
I have changed your code a bit please check,
var BackfillAssignmentGroup_Using_Array = Class.create();
BackfillAssignmentGroup_Using_Array.prototype = {
initialize: function() {
},
getGroups:function(user)
{
if(!user)
{
return;
}
var groupName=new GlideRecord('sys_user_grmember');
groupName.addQuery('user',user);
groupName.query();
var group_Array=[];
while(groupName.next())
{
group_Array.push(groupName.group.toString());
}
return "sys_idIN"+ group_Array.toString();
},
type: ' BackfillAssignmentGroup_Using_Array'
};
Please mark this comment as Correct Answer/Helpful if it helped you.
Regards,
Swathi Sarang
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-18-2024 08:30 AM
@Mark Wood Thanks for marking my answer as helpful. If it helped you in any way please accept the solution so that it will be beneficial to the future readers with the same query.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-18-2024 07:36 AM
Hello @Mark Wood ,
This can be done without any scripting just by adding below reference qualifier and I have tested the same and its working exactly as per the requirement, so please let me know you views on this.
javascript: 'sys_idIN' + new global.ArrayUtil().convertArray(gs.getUser().getUserByID(current.assigned_to.toString()).getMyGroups());
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks & Regards,
Aniket.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-18-2024 08:02 AM
Hello @Aniket Chavan
Your low-code approach is commendable; however, it falls short of meeting the requirements. If I do not select "assigned to" it displays an empty group list which should not occur. If "assigned to" is empty, I expect to see all the groups. Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-18-2024 10:42 AM
Hello @Mark Wood ,
Thank you very much, and I genuinely appreciate your kind words!🤗 & I've implemented your latest suggestions and made the necessary changes. Now, you have two approaches to choose from:
Approach 1: Using Reference Qualifier Only
- Please find the below reference qualifier code:
javascript: if(current.assigned_to !="")'sys_idIN' + new global.ArrayUtil().convertArray(gs.getUser().getUserByID(current.assigned_to.toString()).getMyGroups()); else'active=true';
- Note: While it's often mentioned that using if-else conditions in reference qualifiers might not be considered best practice, I've personally found it effective in many cases. Additionally, I came across an article that also supports this approach. So, feel free to utilize it as needed.
Approach 2: Using Script Include and Reference Qualifier
- Alternatively, if you prefer to avoid if-else conditions in reference qualifiers, you can utilize a script include. I've created a script include named AssignmentGroupReferenceQualifier with the following code:
var AssignmentGroupReferenceQualifier = Class.create();
AssignmentGroupReferenceQualifier.prototype = {
initialize: function() {
},
getAssignmentGroups: function(assignedTo) {
if (!assignedTo || assignedTo.nil()) {
// If Assigned To is empty, return all assignment groups
return 'active=true';
} else {
// If Assigned To is not empty, return groups based on the selected user
return 'sys_idIN' + new global.ArrayUtil().convertArray(gs.getUser().getUserByID(assignedTo.toString()).getMyGroups());
}
},
type: 'AssignmentGroupReferenceQualifier'
};
- To use this script include in your reference qualifier, use the following code:
javascript: new AssignmentGroupReferenceQualifier().getAssignmentGroups(current.assigned_to)
Please let me know if you have any further doubts or concerns so that we can address them accordingly. If everything is clear,
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks & Regards,
Aniket.