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

Ankur Bawiskar
Tera Patron
Tera Patron

@SathiskumarD 

both script include and client script are in same scope?

Script include is client callable?

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.groupBy('user');
        ga.addAggregate('COUNT');
        ga.query();

        var dupes = [];
        while (ga.next()) {
            var count = parseInt(ga.getAggregate('COUNT'));
            if (count > 1) {
                var user = ga.getValue('user');
                dupes.push(user);
            }
        }

        if (dupes.length > 0) {
            // Return comma-separated list of duplicate user sys_ids or user names
            return JSON.stringify('Duplicate user(s) found: ' + dupes.join(', '));
        } else {
            return JSON.stringify(null);
        }
    },

    type: 'CMMS_SI'
});

Client Script:

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

    var ga = new GlideAjax('sn_customerservice.CMMS_SI');
    ga.addParam('sysparm_name', 'getDupesUser');  // fixed param name
    ga.addParam('sysparm_group_id', newValue);
    ga.getXML(handleResponse);

    function handleResponse(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        if (answer && answer !== 'null') {
            g_form.addInfoMessage(answer);
        } else {
            g_form.clearMessages();
        }
    }
}

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 ,

 

I didn't get any info message in esc portal. 

@SathiskumarD 

what debugging did you do?

did you add gs.info() in script include and see?

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

art_jones
Kilo Sage

You have a typo in the client script:  

ga.addParam('sysparam_name', 'getDupesUser');