How to add ref qualifier to backfill assignment group script include e.g. to include / filter by group type also?

jas101
Tera Expert

Hi guys, there are a few historic posts on this but I cannot see one marked with a 'Correct answer' so I hope someone can deliver the goods (help)!

Basically we utilise the 'BackfillAssignmentGroup' script include so our Task table 'Assignment group' field has the Reference qual: javascript:new BackfillAssignmentGroup().filterGroupsonUser(current.assigned_to);

We have recently started utilising the Group 'Type' field (table: sys_user_group_type) functionality so we want to include this within this Ref Qual also i.e. keep the backfill behaviour but also only ensure that Groups with Type value 'assignment' appear.

How can we best achieve this please? I am assuming the existing script include will need to be modified (as opposed to being able to update the Ref Qual itself) but I have not had much like with the couple of aforementioned posts (which might explain why none have been marked as 'Correct answer'). Current script include below.

Thanks as always.

//Usage javascript:new BackfillAssignmentGroup().filterGroupsonUser(current.assigned_to);

var BackfillAssignmentGroup = Class.create();
BackfillAssignmentGroup.prototype = {
    initialize: function() {
    },
	  // Filter out groups that are not the provided type
   filterGroupsonUser: function(cUser) {
	   
		var gp = ' ';
		var a = cUser;
		//return everything if the assigned_to value is empty
		if(!cUser)
		return;
		 
		//sys_user_grmember has the user to group relationship
		var grp = new GlideRecord('sys_user_grmember');
		grp.addQuery('user',cUser);
		grp.query();
	   
		while(grp.next()) {
			
			if (gp.length > 0) {
					//build a comma separated string of groups if there is more than one
					gp += (',' + grp.group);
					
			}
			else {
					gp = grp.group;
					
			}
		}
	return 'sys_idIN' + gp;
	// return Groups where assigned to is in those groups we use IN for lists
   
	   
   },

	type: 'BackfillAssignmentGroup'
}
1 ACCEPTED SOLUTION

One can change the code very easy to support array of types (like ["assignment", "library"]) instead of the string (like "assignment"). First of all one have to change the call of filterGroupsonUser method to

javascript:new BackfillAssignmentGroupByType().filterGroupsonUser(current.assigned_to, ["assignment", "library"])

The modified code of filterGroupsonUser method of BackfillAssignmentGroupByType class could be the following:

/* Usage:
javascript:new BackfillAssignmentGroupByType().filterGroupsonUser(current.assigned_to,
       ["catalog", "itil"]);
*/

var BackfillAssignmentGroupByType = Class.create();
BackfillAssignmentGroupByType.prototype = {
    initialize: function() {
    },
    // Filter out groups that are not the provided type
    filterGroupsonUser: function (userSysId, groupTypeNames) {
       
        //return everything if the assigned_to value is empty
        if (!userSysId) {
            return;
        }
         
        //sys_user_grmember has the user to group relationship
        var grp = new GlideRecord("sys_user_grmember");
        grp.addQuery("user", userSysId);
       
        var condition;
        var groupType = new GlideRecord("sys_user_group_type");
        groupTypeNames.forEach(function (groupTypeName) {
            if (groupType.get("name", groupTypeName)) {
                if (condition === undefined) {
                    condition = grp.addQuery("group.type", "CONTAINS",
                                             groupType.getUniqueValue());
                } else {
                    condition.addOrCondition("group.type", "CONTAINS",
                                             groupType.getUniqueValue());
                }
            }
        });
        grp.query();

        var groups = [];
        while (grp.next()) {
            groups.push(grp.getValue("group"));
        }
        return "sys_idIN" + groups.join();
        // return Groups where assigned to is in those groups we use IN for lists
    },
    type: "BackfillAssignmentGroupByType"
};

I used addQuery for the first type and addOrCondition for the rest types.

View solution in original post

12 REPLIES 12

I've realised the below part of the script include requires modification - how can we amend this so if 'Assigned to' is empty the 'Assignment group' look-up on group type is still performed?

Thanks.

 

 //return everything if the assigned_to value is empty
        if (!userSysId) {
            return;
        }

I agree with you. Try the following modification:

/* Usage:
javascript:new BackfillAssignmentGroupByType().filterGroupsonUser(current.assigned_to,
       ["catalog", "itil"]);
*/

var BackfillAssignmentGroupByType = Class.create();
BackfillAssignmentGroupByType.prototype = {
    initialize: function() {
    },
    // Filter out groups that are not the provided type
    filterGroupsonUser: function (userSysId, groupTypeNames) {
       
        //return everything if the assigned_to value is empty
        var typeIds = [], groupType = new GlideRecord("sys_user_group_type");
        if (!userSysId) {
            groupTypeNames.forEach(function (groupTypeName) {
                if (groupType.get("name", groupTypeName)) {
                    typeIds.push(groupType.getUniqueValue());
                }
            });
            return "typeIN" + typeIds.join();
        }
         
        //sys_user_grmember has the user to group relationship
        var grp = new GlideRecord("sys_user_grmember");
        grp.addQuery("user", userSysId);
       
        var condition;
        groupType = new GlideRecord("sys_user_group_type");
        groupTypeNames.forEach(function (groupTypeName) {
            if (groupType.get("name", groupTypeName)) {
                if (condition === undefined) {
                    condition = grp.addQuery("group.type", "CONTAINS",
                                             groupType.getUniqueValue());
                } else {
                    condition.addOrCondition("group.type", "CONTAINS",
                                             groupType.getUniqueValue());
                }
            }
        });
        grp.query();

        var groups = [];
        while (grp.next()) {
            groups.push(grp.getValue("group"));
        }
        return "sys_idIN" + groups.join();
        // return Groups where assigned to is in those groups we use IN for lists
    },
    type: "BackfillAssignmentGroupByType"
};

Sarah Austria
Giga Guru

This script is working for me. However, for the below part of the script where I need to return everything. I need to include all active groups only. How can I add that in this part of the script? Please help. thanks

 

        //return everything if the assigned_to value is empty
        var typeIds = [], groupType = new GlideRecord("sys_user_group_type");
        if (!userSysId) {
            groupTypeNames.forEach(function (groupTypeName) {
                if (groupType.get("name", groupTypeName)) {
                    typeIds.push(groupType.getUniqueValue());
                }
            });
            return "typeIN" + typeIds.join();
        }