How to populate user in assigned to field

Gopal14
Tera Contributor

Hi Team,

 

I have two assignment groups, if any user is same in both the assignment groups then I need to populate that user in assigned to field.

 

Ex: CAB Approval group is one of the group(Every time this group is common), second group is Incident management(this group will change, this is based on selection of the assignment group), Now If any user is same in this both the groups then I need to populate that user in assigned to field.

 

If more than one user, then need to populate remaining users in watchlist field

1 ACCEPTED SOLUTION

@Gopal14 

then you can use onchange client script on Group field and use GlideAjax

check if any user is common between CAB and Incident Management

If yes then return that user and set it

Client Script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }
    var assignmentGroupId = g_form.getValue('assignment_group');

    var ga = new GlideAjax('CheckCommonGroupMembers');
    ga.addParam('sysparm_name', 'getCommonMember');
    ga.addParam('assignmentGroupId', assignmentGroupId);
    ga.getXMLAnswer(function(response) {
        if (response) {
            g_form.setValue('assigned_to', response);
        } else {
            g_form.clearValue('assigned_to');
        }
    });
}

Script Include: It should be client callable

var CheckCommonGroupMembers = Class.create();
CheckCommonGroupMembers.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    getCommonMember: function() {
        var cabGroupId = 'CAB_GROUP_SYS_ID'; // Replace with the actual sys_id of the CAB group
        var commonMember = '';
        var assignmentGroupId = this.getParameter('assignmentGroupId');

        var incidentGroupMembers = this._getGroupMembers(assignmentGroupId);
        var cabGroupMembers = this._getGroupMembers(cabGroupId);

        for (var i = 0; i < incidentGroupMembers.length; i++) {
            if (cabGroupMembers.indexOf(incidentGroupMembers[i]) !== -1) {
                commonMember = incidentGroupMembers[i];
                break;
            }
        }

        return commonMember;
    },

    _getGroupMembers: function(groupId) {
        var members = [];
        var gr = new GlideRecord('sys_user_grmember');
        gr.addQuery('group', groupId);
        gr.query();
        while (gr.next()) {
            members.push(gr.user.sys_id.toString());
        }
        return members;
    },

    type: 'CheckCommonGroupMembers'
});

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

7 REPLIES 7

GopikaP
Mega Sage

Hi @Gopal14 , How are those assignment groups decided, or are you only considering those two assignment groups? 

How are you populating assignment group first? 

Gopal14
Tera Contributor

Hi @GopikaP 

 

On incident form I am creating new incident, selecting Incident Management group in assignment group field. Now it needs to check with CAB group(this group we are not selecting in any ware, we need to check this group).  If in this two groups, if any group members is same, then I need to populate that user in assigned to field.

Ankur Bawiskar
Tera Patron
Tera Patron

@Gopal14 

your requirement is not clear.

How is the group selected? which form are you referring?

share some screenshots and script you tried

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar 

 

On incident form I am creating new incident, selecting Incident Management group in assignment group field. Now it needs to check with CAB group(this group we are not selecting in any ware, we need to check this group).  If in this two groups, if any group members is same, then I need to populate that user in assigned to field.