Filtering the ‘Assignment group’ to display only groups for the ‘Requested For’ value.

Anshika2
Kilo Expert

Hi, I am having one custom field 'Opened By Group' on one of my catalog item. It is having another field 'Requested For'.  

The requirement is that when we select any user in 'Requested for' the the reference field 'opened by group' should show only the name of groups to which the requested for user belongs to. Groups that starts with name SN and are active should show in the list. Otherwise if there is no group then it should only show the group' information'.

Here is my script include for the same.

 

function GenericRequestOpenedBy() {
    try {
        var user = current.requestor;
        var assignment_groups = [];
        var grMembers = GlideRecord('sys_user_grmember');
        grMembers.addQuery('user', user);
        grMembers.addEncodedQuery('groupSTARTSWITHSN^group.active=true');
         grMembers.query();
       
        while (grMembers.next()) {
           assignment_groups.push(grMembers.group.sys_id);
        }
        if (grMembers.getRowCount() == 0) {
            assignment_groups.push('d79cde4c6f1211007227181d5d3ee4ec');
        }
        return "sys_idIN" + assignment_groups;
    } catch (e) {
        gs.log("Error in IncidentOpenedByGroups :SI " + e);
    }

}

 

Kindly help to achieve this.

1 ACCEPTED SOLUTION

Try below:

var GetGroup = Class.create();
GetGroup.prototype = {
	initialize: function() {
	},

GenericRequestOpenedBy : function (requestor) {
        var user = requestor;
        var assignment_groups = [];
        var grMembers = GlideRecord('sys_user_grmember');
        grMembers.addQuery('user', user);
        grMembers.addEncodedQuery('group.nameSTARTSWITHSN^group.active=true');
         grMembers.query();
       while (grMembers.next()) {
           assignment_groups.push(grMembers.group.sys_id);
        }
        if (grMembers.getRowCount() == 0) {
            assignment_groups.push('d79cde4c6f1211007227181d5d3ee4ec');
        }
        return "sys_idIN" + assignment_groups;
    },
	type: 'GetGroup'
};

 

GetGroup should be name of your script include.

 

If requestedFor is variable then use below reference qualifier.

javascript: new GetGroup ().GenericRequestOpenedBy(current.variables.requestor.toString());

 

If it is column on table then use below:

javascript: new GetGroup ().GenericRequestOpenedBy(current.requestor.toString());

 

Thanks,

Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

View solution in original post

19 REPLIES 19

Try below:

var GetGroup = Class.create();
GetGroup.prototype = {
	initialize: function() {
	},

GenericRequestOpenedBy : function (requestor) {
        var user = requestor;
        var assignment_groups = [];
        var grMembers = GlideRecord('sys_user_grmember');
        grMembers.addQuery('user', user);
        grMembers.addEncodedQuery('group.nameSTARTSWITHSN^group.active=true');
         grMembers.query();
       while (grMembers.next()) {
           assignment_groups.push(grMembers.group.sys_id);
        }
        if (grMembers.getRowCount() == 0) {
            assignment_groups.push('d79cde4c6f1211007227181d5d3ee4ec');
        }
        return "sys_idIN" + assignment_groups;
    },
	type: 'GetGroup'
};

 

GetGroup should be name of your script include.

 

If requestedFor is variable then use below reference qualifier.

javascript: new GetGroup ().GenericRequestOpenedBy(current.variables.requestor.toString());

 

If it is column on table then use below:

javascript: new GetGroup ().GenericRequestOpenedBy(current.requestor.toString());

 

Thanks,

Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Hi Anil,

It is working now!!!

 

Thanks a lot for your support.

Hi @Anil Lande ,

I am having one more thing, you have passed the requestor value in function and then used it to show list of groups of requestor but if we want to display the groups of both requestor and requested by then how we will do that here?

Kindly reply.

Bit late though. Good to know you got it working.

Hi @Jaspal Singh ,

I am having one more thing, you have passed the requestor value in function and then used it to show list of groups of requestor but if we want to display the groups of both requestor and requested by then how we will do that here?

Kindly reply. Below is the script:

var GetGroup = Class.create();
GetGroup.prototype = {
    initialize: function() {},

    GenericRequestOpenedBy: function(requestor) {
        var user = requestor;
        var assignment_groups = [];
        var grMembers = GlideRecord('sys_user_grmember');
        grMembers.addQuery('user', user);
        grMembers.addEncodedQuery('group.nameSTARTSWITHSN^group.active=true');
        grMembers.query();
        while (grMembers.next()) {
            assignment_groups.push(grMembers.group.sys_id);
        }
        if (grMembers.getRowCount() == 0) {
            assignment_groups.push('d79cde4c6f1211007227181d5d3ee4ec');
        }
        return "sys_idIN" + assignment_groups;
    },
    type: 'GetGroup'
};

 

Reference qualifier : javascript: new GetGroup().GenericRequestOpenedBy(current.variables.requestor.toString());