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.

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

Please replace findUser with readuserID in the script include 2nd line. See screenshot below

find_real_file.png

Regards,
Muhammad

Thank you Muhammad. I didnt catch it while changing the code. 

 

Much Appreciated 🙂

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.');
        }
    });
}

Thank You Hozawa, 

 

Can you please provide the difference between the two methods below?

 

ga.addParam('user_id', newValue);
    ga.getXMLAnswer(function(answer) {
        if (answer == "true") {

 

ga.addParam('sysparm_user_name', newValue);
ga.getXML(callBack);
 
function callBack(response) {
  var answer = response.responseXML.documentElement.getAttribute("answer");

 

I would like to understand the difference between them.

 

Thanks,

pK.

Hi PK,

Refer - https://community.servicenow.com/community?id=community_article&sys_id=c918c3b8db968c5013b5fb2439961...

Above link has worthy information around this 

Thanks & Regards,

Sharjeel

Regards,
Muhammad