need to dispaly pop up when firstname and lastname and email id exist in the user table.

Mansi roy
Tera Contributor

Hello Everyone,

 

Could anyone please help me on this.

 

I have three variables(first name, last name and email id) in a catalog item to create new user. If some one will enter first name and last name in the variables if first name+ last name is exist in the user table one pop up will get display in portal that user name is exist. If email id also exist it will display a message that email id exist.

 

Could anyone please help me on this.

 

 

2 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron
Tera Patron

@Mansi roy 

you will require 3 onchange catalog client script and GlideAjax for this

Script Include: Client Callable

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

    checkUserName: function() {
        var firstName = this.getParameter('sysparm_firstName');
        var lastName = this.getParameter('sysparm_lastName');

        var userGR = new GlideRecord('sys_user');
        userGR.addQuery('first_name', firstName);
        userGR.addQuery('last_name', lastName);
        userGR.query();
        if (userGR.hasNext()) {
            return 'User name exists';
        } else
            return '';
    },

    checkEmail: function() {
        var email = this.getParameter('sysparm_email');
        var emailGR = new GlideRecord('sys_user');
        emailGR.addQuery('email', email);
        emailGR.query();
        if (emailGR.hasNext()) {
            return 'Email ID exists';
        }
        return '';
    },

    type: 'UserCheck'
});

AnkurBawiskar_0-1743575833797.png

 

onChange on First Name and Last Name: use same script below, UI Type - ALL

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

    var firstName = g_form.getValue('first_name');
    var lastName = g_form.getValue('last_name');

    var ga = new GlideAjax('UserCheck');
    ga.addParam('sysparm_name', 'checkUserName');
    ga.addParam('sysparm_firstName', firstName);
    ga.addParam('sysparm_lastName', lastName);
    ga.getXMLAnswer(function(response) {
        if (answer != '') {
            alert(answer);
        }
    });
}

1 onChange on Email:

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

    var ga = new GlideAjax('UserCheck');
    ga.addParam('sysparm_name', 'checkEmail');
    ga.addParam('sysparm_email', newValue);
    ga.getXMLAnswer(function(response) {
        if (answer != '') {
            alert(answer);
        }
    });
}

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

View solution in original post

Shivalika
Mega Sage

Hello @Mansi roy 

 

PLease use below on submit client script and Script include - 

 

Change the field names and "first name" + last_name" combination as in your instance. 

 

On-Submit Client Script

function onSubmit() {
    var firstName = g_form.getValue('first_name');
    var lastName = g_form.getValue('last_name');
    var email = g_form.getValue('email');

    var userName = firstName + ' ' + lastName;

    var ga = new GlideAjax('UserCheck');
    ga.addParam('sysparm_name', 'checkUser');
    ga.addParam('sysparm_user_name', userName);
    ga.addParam('sysparm_email', email);
    ga.getXMLAnswer(function(response) {
        var result = JSON.parse(response);
        if (result.userExists) {
            alert('Username already exists.');
        }
        if (result.emailExists) {
            alert('Email ID already exists.');
        }
    });

    return false; // Prevent form submission until checks are complete
}

Script Include

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

    checkUser: function() {
        var userName = this.getParameter('sysparm_user_name');
        var email = this.getParameter('sysparm_email');

        var userExists = false;
        var emailExists = false;

        var userGR = new GlideRecord('sys_user');
        userGR.addQuery('name', userName);
        userGR.query();
        if (userGR.next()) {
            userExists = true;
        }

        var emailGR = new GlideRecord('sys_user');
        emailGR.addQuery('email', email);
        emailGR.query();
        if (emailGR.next()) {
            emailExists = true;
        }

        var result = {
            userExists: userExists,
            emailExists: emailExists
        };

        return JSON.stringify(result);
    },

    type: 'UserCheck'
};

This script will check if the username and email ID already exist in the user table and display appropriate messages. Let me know if you need any further assistance or modifications!

View solution in original post

4 REPLIES 4

RAMANA MURTHY G
Mega Sage
Mega Sage

Hello @Mansi roy ,

 

You can do this with onSubmit client script with Script Include. Check conditions using firstName and lastName variables or email variable in script include and return true or false to the client script, then you can use spModel to show the popup.

 

 

Please mark my answer helpful  & correct if it helps you
Thank you

G Ramana Murthy
ServiceNow Developer

Ankur Bawiskar
Tera Patron
Tera Patron

@Mansi roy 

you will require 3 onchange catalog client script and GlideAjax for this

Script Include: Client Callable

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

    checkUserName: function() {
        var firstName = this.getParameter('sysparm_firstName');
        var lastName = this.getParameter('sysparm_lastName');

        var userGR = new GlideRecord('sys_user');
        userGR.addQuery('first_name', firstName);
        userGR.addQuery('last_name', lastName);
        userGR.query();
        if (userGR.hasNext()) {
            return 'User name exists';
        } else
            return '';
    },

    checkEmail: function() {
        var email = this.getParameter('sysparm_email');
        var emailGR = new GlideRecord('sys_user');
        emailGR.addQuery('email', email);
        emailGR.query();
        if (emailGR.hasNext()) {
            return 'Email ID exists';
        }
        return '';
    },

    type: 'UserCheck'
});

AnkurBawiskar_0-1743575833797.png

 

onChange on First Name and Last Name: use same script below, UI Type - ALL

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

    var firstName = g_form.getValue('first_name');
    var lastName = g_form.getValue('last_name');

    var ga = new GlideAjax('UserCheck');
    ga.addParam('sysparm_name', 'checkUserName');
    ga.addParam('sysparm_firstName', firstName);
    ga.addParam('sysparm_lastName', lastName);
    ga.getXMLAnswer(function(response) {
        if (answer != '') {
            alert(answer);
        }
    });
}

1 onChange on Email:

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

    var ga = new GlideAjax('UserCheck');
    ga.addParam('sysparm_name', 'checkEmail');
    ga.addParam('sysparm_email', newValue);
    ga.getXMLAnswer(function(response) {
        if (answer != '') {
            alert(answer);
        }
    });
}

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

@Mansi roy 

Hope you are doing good.

Did my reply answer your question?

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

Shivalika
Mega Sage

Hello @Mansi roy 

 

PLease use below on submit client script and Script include - 

 

Change the field names and "first name" + last_name" combination as in your instance. 

 

On-Submit Client Script

function onSubmit() {
    var firstName = g_form.getValue('first_name');
    var lastName = g_form.getValue('last_name');
    var email = g_form.getValue('email');

    var userName = firstName + ' ' + lastName;

    var ga = new GlideAjax('UserCheck');
    ga.addParam('sysparm_name', 'checkUser');
    ga.addParam('sysparm_user_name', userName);
    ga.addParam('sysparm_email', email);
    ga.getXMLAnswer(function(response) {
        var result = JSON.parse(response);
        if (result.userExists) {
            alert('Username already exists.');
        }
        if (result.emailExists) {
            alert('Email ID already exists.');
        }
    });

    return false; // Prevent form submission until checks are complete
}

Script Include

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

    checkUser: function() {
        var userName = this.getParameter('sysparm_user_name');
        var email = this.getParameter('sysparm_email');

        var userExists = false;
        var emailExists = false;

        var userGR = new GlideRecord('sys_user');
        userGR.addQuery('name', userName);
        userGR.query();
        if (userGR.next()) {
            userExists = true;
        }

        var emailGR = new GlideRecord('sys_user');
        emailGR.addQuery('email', email);
        emailGR.query();
        if (emailGR.next()) {
            emailExists = true;
        }

        var result = {
            userExists: userExists,
            emailExists: emailExists
        };

        return JSON.stringify(result);
    },

    type: 'UserCheck'
};

This script will check if the username and email ID already exist in the user table and display appropriate messages. Let me know if you need any further assistance or modifications!