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

Anil Lande
Kilo Patron

Hi,

Please try below script.

function GenericRequestOpenedBy() {
    try {
        var user = current.requestor;
        var assignment_groups = [];
        var grMembers = GlideRecord('sys_user_grmember');
        grMembers.addQuery('user', user);
        grMembers.addEncodedQuery('group.nameSTARTSWITHSN^group.active=true');   // made changes here
         grMembers.query();
       //group.nameSTARTSWITHSN^group.active=true this is correct filter  
        while (grMembers.next()) {
           assignment_groups.push(grMembers.group.sys_id);
        }
        if (grMembers.getRowCount() == 0) {
            assignment_groups.push('d79cde4c6f1211007227181d5d3ee4ec');
        }
        return "sys_idIN" + assignment_groups.toString();  // also used toString() here
    } catch (e) {
        gs.log("Error in IncidentOpenedByGroups :SI " + e);
    }

}

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

I hope you are calling this function in advanced Reference qualifier of 'Opened By Group' like below:

javascript: new ScriptIncludeName().GenericRequestOpenedBy()

And backend name of Requested For is 'requestor'.

 

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

yes I m doing the same.

Hi, Still it is going in if loop and showing the information service group only. And I tried using logs to check it is showing the value of user undefined.

May be I am not getting requestor value. 

PFA the pictures. I haven't used any client script. I m calling it in advance reference qualifier.