Finding the duplicate user in the group

SathiskumarD
Tera Contributor

Hello,

 

I am trying to find the duplicate users in the group and adding info message in the portal. I am using scoped app (customer service). But it always pop up as "null". Not sure what I did wrong. If you could help me in spotting the error in my code, I would greatly appreciate it. Thanks.

My Script include:

var CMMS_SI = Class.create();
CMMS_SI.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    isPublic: function() {
        return true;
    },
    getDupesUser: function() {
        var r = [];
        // var usrObj = {};
        var groupID = this.getParameter('sysparm_group_id');
        var ans;
        var ga = new GlideAggregate('sys_user_grmember');
        ga.addQuery('group', groupID);
        ga.addAggregate("COUNT", 'user'); // Count how many unique values there are
        ga.query();
        while (ga.next()) {
            var c = ga.getAggregate("COUNT", 'user');
            if (c > 1) {
                r.push({
					name:ga.getValue('user').toString(),
					group:ga.getValue('groupID'),
				});
				ans=r[0].name+" already exists in the "+r[1].group;
                
            }
        }
        return JSON.stringify(ans);

    },
    type: 'CMMS_SI'
});


Catalog client script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var ga = new GlideAjax('sn_customerservice.CMMS_SI');
    ga.addParam('sysparam_name', 'getDupesUser');
    ga.addParam('sysparm_group_id', newValue);
    ga.getXML(_handleResponse);

    function _handleResponse(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");

        // answer=response;
        alert(answer);
        // ga.addInfoMessage(answer);
    }
    

}

 

7 REPLIES 7

Thanks. I fixed it.

Sujit Jadhav
Tera Guru

Hello @SathiskumarD ,

Please try this below script include and client script

Script Include:

var CMMS_SI = Class.create();
CMMS_SI.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
isPublic: function() {
return true;
},

getDupesUser: function() {
var groupID = this.getParameter('sysparm_group_id');
var ga = new GlideAggregate('sys_user_grmember');
ga.addQuery('group', groupID);
ga.addAggregate("COUNT", 'user');
ga.groupBy('user');
ga.query();

while (ga.next()) {
var count = parseInt(ga.getAggregate("COUNT", 'user'), 10);
if (count > 1) {
var userName = ga.getDisplayValue('user');
var groupName = ga.getDisplayValue('group');
return userName + " already exists in the group " + groupName;
}
}

return ""; // No duplicates found
},

type: 'CMMS_SI'
});



Catalog Client Script:

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

var ga = new GlideAjax('sn_customerservice.CMMS_SI');
ga.addParam('sysparam_name', 'getDupesUser');
ga.addParam('sysparm_group_id', newValue);
ga.getXML(_handleResponse);

function _handleResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer) {
alert(answer); // Or use g_form.addInfoMessage(answer);
}
}
}

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

Thank You,

Sujit Jadhav



I didn't get any alerts.