Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Why is this so difficult? Creating an alert to show when a caller is a member of a particular group.

JennyBell
Tera Contributor

I have run this scenario in the community, Google and Copilot multiple ways and there are so many different ways this can be done I'm getting confused because each one I try does not work.  Can someone provide a script to do the following:  

 

When a name is entered into the caller id field on the Incident form, check if the caller id is a member of a group.


Caller_ID:  Jen Nocode

Group: Pilot_Participant

If, yes Jen Nocode is a member of the Pilot Participant group = show an alert telling the call taker the user is a pilot participant

 

If No, no alert needed

 

Some solutions provided had me create a Business Rule and a Client Script, some had just a Client Script.

 

Thanks!

 

 

 

 

6 REPLIES 6

JessicaLanR
Kilo Guru
Kilo Guru

You can do this cleanly with one onChange Client Script + one Script Include (client-callable). The check must run on the server (groups are in server tables), so the Client Script calls the Script Include via GlideAjax and shows a message only when the user is in the group.

Below is a drop-in solution. Replace the group name or sys_id as needed.

 

 

  • Name: GroupChecker

  • API name: GroupChecker (global scope is fine)

  • Check “Client callable”

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

    /**
     * GlideAjax entry point
     * @returns {String} "true" or "false"
     */
    isUserInGroup: function () {
        var userId = this.getParameter('sysparm_user_id');    // sys_id from caller_id
        var groupId = this.getParameter('sysparm_group_id');  // optional: sys_id of the group
        var groupName = this.getParameter('sysparm_group_name'); // optional: name of the group

        if (!userId)
            return "false";

        // Resolve group sys_id (prefer explicit sys_id if provided)
        var targetGroupId = '';
        if (groupId) {
            targetGroupId = groupId;
        } else if (groupName) {
            var g = new GlideRecord('sys_user_group');
            g.addQuery('name', groupName);
            g.setLimit(1);
            g.query();
            if (g.next())
                targetGroupId = g.getUniqueValue();
        }

        if (!targetGroupId)
            return "false";

        // Check membership
        var m = new GlideRecord('sys_user_grmember');
        m.addQuery('user', userId);
        m.addQuery('group', targetGroupId);
        m.setLimit(1);
        m.query();
        return m.hasNext() ? "true" : "false";
    },

    type: 'GroupChecker'
};

 

Ajay_Chavan
Kilo Sage

You're right - this should be simple! Here's a clean, working solution with just two components:

Solution: Script Include + Client Script

Step 1: Create Script Include

 
var GroupMembershipChecker = Class.create();
GroupMembershipChecker.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    checkGroupMembership: function() {
        var userSysId = this.getParameter('sysparm_user_id');
        var groupName = this.getParameter('sysparm_group_name');
        
        if (!userSysId || !groupName) {
            return 'false';
        }
        
        // Check if user is member of the specified group
        var memberGR = new GlideRecord('sys_user_grmember');
        memberGR.addQuery('user', userSysId);
        memberGR.addQuery('group.name', groupName);
        memberGR.query();
        
        if (memberGR.hasNext()) {
            return 'true';
        } else {
            return 'false';
        }
    },

    type: 'GroupMembershipChecker'
});

 

Step 2: Create Client Script

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue == '') {
        return;
    }
    
    // Call server-side script to check group membership
    var ga = new GlideAjax('GroupMembershipChecker');
    ga.addParam('sysparm_name', 'checkGroupMembership');
    ga.addParam('sysparm_user_id', newValue);
    ga.addParam('sysparm_group_name', 'Pilot_Participant');
    
    ga.getXMLAnswer(function(answer) {
        if (answer == 'true') {
            // User is a pilot participant - show alert
            alert('PILOT PARTICIPANT: This caller is part of the Pilot Participant program!');
            
            // Optional: Add visual indicator
            g_form.addInfoMessage('This caller is a Pilot Participant');
        }
        // If answer is 'false', do nothing (no alert needed)
    });
}

 

Glad I could help! If this solved your issue, please mark it as Helpful and Accept as Solution so others can benefit too.*****Chavan A.P. | Technical Architect | Certified Professional*****

@JennyBell - did you check?

Glad I could help! If this solved your issue, please mark it as Helpful and Accept as Solution so others can benefit too.*****Chavan A.P. | Technical Architect | Certified Professional*****

anurampalli
Tera Guru

OnChange client script invoking a client callable script include should work. I agree with both answers above.