Reference Qualifier

abhijee
Tera Contributor

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)

 

1 ACCEPTED SOLUTION

Hello @abhijee ,

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';

AniketChavan_1-1710783600689.png

 

  • 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'
};

AniketChavan_0-1710783497473.png

 

 

  • To use this script include in your reference qualifier, use the following code:
javascript: new AssignmentGroupReferenceQualifier().getAssignmentGroups(current.assigned_to)

 

AniketChavan_2-1710783708821.png


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.

 

View solution in original post

11 REPLIES 11

Maddysunil
Kilo Sage

@abhijee 

Your code seems to be fine, also working with background script.I have made some changes and put some logs ..Could you please try once.

 

var BackfillAssignmentGroup_Using_Array = Class.create();
BackfillAssignmentGroup_Using_Array.prototype = {
    initialize: function() {
    },
    getGroups: function(user) {
        if (!user) {
            gs.info("User is empty or null.");
            return '';
        }
        
        var group = new GlideRecord('sys_user_grmember');
        if (!group.isValid()) {
            gs.error("sys_user_grmember table is not valid.");
            return '';
        }
        
        group.addQuery('user', user);
        group.query();
        
        var groupArray = [];
        while (group.next()) {
            groupArray.push(group.getValue('group'));
        }
        
        if (groupArray.length > 0) {
            return "sys_idIN" + groupArray.join(',');
        } else {
            gs.info("No assignment groups found for user: " + user);
            return ''; // No assignment groups found
        }
    },
    type: 'BackfillAssignmentGroup_Using_Array'
};

 

Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

Harish KM
Kilo Patron

Hi @abhijee can you put like this and try

group.addQuery('user',user.toString());

Regards
Harish

Ranjit Nimbalka
Mega Sage

Hi @abhijee 

there is an issue with your script on the line  

group_Array.push(group.getValue('sys_id'));

here you are getting the "sys_id" of the "sys_user_grmember" table record and we want group "sys_id" for assignment group just replace this line by 

group_Array.push(group.getValue('group'));

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.

 

Regards,

Ranjit

Anirudh Pathak
Giga Sage

Hi @abhijee ,

You are fetching "sys_id" of Group Members record, what you need is "sys_id" of the group record.

Update your script include to below code - 

if(!user)
	{
return;

	}
var groupGr=new GlideRecord('sys_user_grmember');
groupGr.addQuery('user',user);
groupGr.query();
var group_Array=[];
while(groupGr.next())
{
group_Array.push(groupGr.group.getValue('sys_id'));
}
return "sys_idIN"+group_Array.toString();