Preventing duplicate when creating records in sys_user_grmember

sethhumphrey
Mega Guru

I have a catalog item to request additions and deletions to ServiceNow groups.  Flow Designer handles the adds and deletes after group manager approval perfectly; however, duplicate additions pass are accepted.

Option 1) Remove existing group member user records from selection reference field (person_add). I wasn't sure where to start with this option.  This would be ideal if you have a solution.

Option 2) Create a Catalog Client Script to prevent duplicates from being submitted.  Attempt of this (below) is not working.  What am I missing?

Catalog Item

find_real_file.png

Variables

find_real_file.png

  • u_group is a reference to [sys_user_group]
  • person_add is a reference to [sys_user]

Catalog Client Script

function onSubmit() {

    g_form.clearMessages();
	
    var grp = g_form.getValue('u_group');	
    var usr = g_form.getValue('person_add');
	
    // IF adding a group member
    if (g_form.getValue('type_of_group_change') == 'add') {
        var gr = new GlideRecord('sys_user_grmember');
        gr.addQuery("group", grp);
        gr.addQuery("user", usr);
        gr.query();
        while (gr.next()) {
            g_form.addErrorMessage('Person is already in the group.');
            return false;
        }
    }
}
1 ACCEPTED SOLUTION

Jim Coyne
Kilo Patron

Pradeep beat me to it with the Reference Qualifier idea.  Basically, this will allow you to remove the current group members from the "person_add" variable so you cannot have duplicates and only show group members in the "person_remove" variable so you can't select someone who is not already in the group.

Follow these steps:

1. create a new Script Include
Name: uSCRefQualifiers (or whatever you want to call it)
Client callable: NOT checked
Accessible from: All application scopes
Script:

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

    groupMembers: function(groupId, joiner) {
        var member = [];

        var gr = new GlideRecord("sys_user_grmember");
        gr.addEncodedQuery("group=" + groupId);
        gr.query();
        while (gr.next()) {
            member.push(gr.getValue("user"));
        }

        return member.join(joiner);
    },

    type: 'uSCRefQualifiers'
};

 

2. for the "person_remove" variable, set the  "Use reference qualifier" field to "Advanced" and put the following in the "Script" field:

javascript:"sys_idIN" + new global.uSCRefQualifiers().groupMembers(current.variables.group.toString(), ",");

find_real_file.png

 

3. for the "person_add" variable, same thing, but use the following script:

javascript:"sys_id!=" + new global.uSCRefQualifiers().groupMembers(current.variables.group.toString(), "^sys_id!=");

View solution in original post

8 REPLIES 8

Hello Seth,

It is not recommended to use GlideRecord calls on the client-side. Also please note GlideRecord calls at Client-side doesn't work in a scoped app. The reason your CS is not working is mostly because of the addQuery line i.e gr.addQuery("group", grp); //Here grp value has to be sys_id but I assume you might be passing the name of the group. This depends on the lookup select box configuration setting you have selected. Of course, this is assuming the script is defined in the global scope.

That said, the best practice is to go with the Reference qualifier script shared by @jim.coyne 

 

- Pradeep Sharma

This is excellent idea @Jim Coyne .  Appreciated. 

sethhumphrey
Mega Guru

Thanks for your help Jim & Pradeep!

You are welcome!