How to identify if userid already exists?

praveenKumar2
Kilo Expert

Hi All,

I have a requirement to check if a userid already exists and if it exists an error should be thrown user id already exists. There is a field on a catalog item and when a user enters the user id like pkmar00 it should check behind the scenes and throw an error and clear out the field value. 

 

Kindly please help me. 

Thanks & Regards.

Praveen.

1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

Try this. Tested it with "abraham.lincoln", "donald.trump"

Script Include:

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

    validateUser: function() {
        var user_id = this.getParameter('user_id');
        var gr = new GlideRecord("sys_user");
        gr.addQuery("user_name", user_id);
        gr.query();
        if (gr.next()) {
            return true;
        } else {
            return false;
        }
    },

    type: 'checkUserExists'
});

UI Script:

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

    var ga = new GlideAjax('checkUserExists');
    ga.addParam('sysparm_name', "validateUser");
    ga.addParam('user_id', newValue);
    ga.getXMLAnswer(function(answer) {
        if (answer == "true") {
            g_form.clearValue('user_id');
            g_form.showFieldMsg('user_id', 'User already exists.');
        }
    });
}

View solution in original post

11 REPLIES 11

Check the following article. In a nutshell, getXML will retrieve the entire xml document while getXMLAnswer will only retrieve a returned value. That's why getXML will need to be parsed to get the value while getXMLAnswer do not.

Because getXMLAnswer is returning less data and require less processing on the client side, it is preferred over getXML when just retrieving a value.

Note, that it is still necessary to use getXML to return an object.

https://community.servicenow.com/community?id=community_article&sys_id=1c10a1fedbbd4890feb1a851ca961...

Sudhanshu Talw1
Tera Guru

Hii you have to write a script include & return true/false from it if user exist return true; else return false Call it onchange script.

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

initialize:function(){

},
validate: function()
{
var user_id = this.getParameter('user_name');
var gr = new GlideRecord("sys_user"); gr.addQuery("user_name", user_id);
gr.query();
if (gr.next())
{
return true;
}
else {
return false;
}
},
type: 'isUserExists'
});