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

Hi @Gopal14 

What is the correlation between the incident group and the CAB group on the incident form? The CAB group is actually not related to the incident. What is your use case, or are you following a specific practice?

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Hi @Dr Atul G- LNG ,

 

I have different group, I have given CAB group as a Example

@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